-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.py
More file actions
42 lines (29 loc) · 801 Bytes
/
DecisionTree.py
File metadata and controls
42 lines (29 loc) · 801 Bytes
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
import pandas
import numpy as np
from sklearn.tree import DecisionTreeClassifier
data = pandas.read_csv('titanic.csv', index_col='PassengerId')
fout = open('DecisionTreeAnswer.txt', 'w')
newData = []
y = []
Pclass = list(data['Pclass'])
Fare = list(data['Fare'])
Age = list(data['Age'])
Sex = list(data['Sex'])
Y = list(data['Survived'])
for i in range(len(data)):
nowSex = 1
if Sex[i] == 'male':
nowSex = 0
nowPass = [Pclass[i], Fare[i], Age[i], nowSex]
good = True
for x in nowPass:
if np.isnan(x):
good = False
if good:
newData.append(nowPass)
y.append(Y[i])
clf = DecisionTreeClassifier(random_state=241)
clf.fit(newData, y)
x = zip(['Pclass', 'Fare', 'Age', 'Sex'], clf.feature_importances_)
print(*x)
fout.close()