-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS-P6.py
67 lines (39 loc) · 1.17 KB
/
DS-P6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
data = [
['Low', 'Low', 2, 'No', 'Yes'],
['Low', 'Med', 4, 'Yes', 'Yes'],
['Low', 'Low', 4, 'No', 'Yes'],
['Low', 'Med', 4, 'No', 'No'],
['Low', 'High', 4, 'No', 'No'],
['Med', 'Med', 4, 'No', 'No'],
['Med', 'Med', 4, 'Yes', 'Yes'],
['Med', 'High', 2, 'Yes', 'No'],
['Med', 'High', 5, 'No', 'Yes'],
['High', 'Med', 4, 'Yes', 'Yes'],
['High', 'Med', 2, 'Yes', 'Yes'],
['High', 'High', 2, 'Yes', 'No'],
['High', 'High', 5, 'Yes', 'Yes']
]
# In[ ]:
import pandas as pd
df = pd.DataFrame(data, columns=['Price', 'Maintenance', 'Capacity', 'Airbag', 'Profitable'])
# In[ ]:
label_encoders = {}
for column in ['Price', 'Maintenance', 'Airbag', 'Profitable']:
le = LabelEncoder()
df[column] = le.fit_transform(df[column])
label_encoders[column] = le
# In[ ]:
X = df.drop(columns=['Profitable'])
y = df['Profitable']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# In[ ]:
clf = DecisionTreeClassifier(criterion='entropy')
clf.fit(X_train, y_train)
# In[ ]:
y_pred = clf.predict(X_test)
# In[ ]:
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)