-
Notifications
You must be signed in to change notification settings - Fork 0
/
using_sklearn.py
104 lines (84 loc) · 2.86 KB
/
using_sklearn.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import csv
import numpy as np
### I had only used numpy , as this is when I had just dived in
### this universe
### So , for beginners go search for pandas you will reduce the lines of code and it
### is awesome !!
### read data
with open('../Data/train.csv') as f:
reader = csv.reader(f, delimiter=',')
data = []
for row in reader:
data.append(row)
### labels
data_headers = data[0]
### get some fields only
for i in ["Name", "PassengerId", "Survived", "Ticket", "Fare", "Cabin"]:
data_headers.remove(i)
### preprocessing and encoding
data = np.array(data[1:])
data = np.delete(data, [0, 3], 1)
order = ['Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Embarked']
data[data == ''] = '01111110'
train_result = data[:, 0]
data = np.delete(data, [0, 6], 1)
data = np.delete(data, 5, 1)
data = np.delete(data, 5, 1)
print(data_headers, data[0])
data[data == "male"] = 0
data[data == "female"] = 1
data[data == "S"] = 1
data[data == "Q"] = 0
data[data == "C"] = 2
### using various classifiers
# from sklearn.naive_bayes import GaussianNB
# clf=GaussianNB()
# from sklearn.tree import DecisionTreeClassifier
# clf=DecisionTreeClassifier()
# from sklearn.svm import SVC
# clf=SVC()
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
clf = RandomForestClassifier(max_depth=5, n_estimators=10, max_features=2)
# clf=AdaBoostClassifier()
# from sklearn.neighbors import KNeighborsClassifier
# clf=KNeighborsClassifier()
# print(np.array(['','1']).astype(np.float),"jsbd")
# print(len(data.astype(np.float)),"############",len(train_result.astype(np.float)))
### fit data to classifier
clf.fit(data.astype(np.float), train_result.astype(np.float))
# Testing data
data = []
with open('../Data/test.csv') as f:
reader = csv.reader(f, delimiter=',')
data = []
for row in reader:
data.append(row)
# print(len(data))
data_headers = data[0]
### preprocessing for test data
for i in ["Name", "PassengerId", "Ticket", "Fare", "Cabin"]:
data = np.delete(data, data_headers.index(i), 1)
data_headers.remove(i)
data = np.array(data[1:])
data[data == ''] = '01111110'
data[data == "male"] = 0
data[data == "female"] = 1
data[data == "S"] = 1
data[data == "Q"] = 0
data[data == "C"] = 2
# print(len(data),len(order),data,"end data")
test_data = np.array(data[:, data_headers.index(order[0])])
for i in order[1:]:
test_data = np.vstack((test_data, data[:, data_headers.index(i)]))
# print(data_headers,"jdbfue",test_data,"jdbueb")
with open('../Data/gender_submission.csv') as f:
reader = csv.reader(f, delimiter=',')
test_labels = []
for row in reader:
test_labels.append(row[1])
print(len(test_labels))
test_labels = np.array(test_labels[1:])
ans = clf.predict(test_data.astype(np.float).T)
ans1 = np.array([range(892, 1310)])
ans = np.vstack((ans1.astype(np.int), ans.astype(np.int))).T
np.savetxt("fo1o.csv", ans, delimiter=",", fmt='%d')