-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchuck2.py
168 lines (136 loc) · 4.34 KB
/
chuck2.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#I'm going to want the forest, a fitter (logistic regression? elastic net?), and whatever this is averaged
import csv as csv
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from collections import Counter
def cabin_letter(x):
return{
'A':1,
'B':2,
'C':3,
'D':4,
'E':5,
'F':6,
'G':7,
}.get(x,0)
def num (s):
try:
return int(s)
except ValueError:
return float(s)
#put the training data into a big variable
csv_file_object = csv.reader(open('train.csv', 'rb')) #Load in the csv file
header = csv_file_object.next() #Skip the fist line as it is a header
train_data=[] #Creat a variable called 'train_data'
for row in csv_file_object: #Skip through each row in the csv file
train_data.append(row) #adding each row to the data variable
train_data = np.array(train_data) #Then convert from a list to an array
def fixdata (data):
#split the cabin column to find the last split (the last if there are multiple cabins)
#then assign a number based on the cabin letter
cabin_letter_list = []
for i in data[0::,9]:
splitter = i.rsplit(' ',1)[-1]
if splitter:
splitter = cabin_letter(splitter[0])
else:
splitter = 0
cabin_letter_list.append(splitter)
data[0::,9] = cabin_letter_list
#print data[0,9]
#Male = 1, female = 0:
data[data[0::,3]=='male',3] = 1
data[data[0::,3]=='female',3] = 0
#embark c=0, s=1, q=2
data[data[0::,10] =='C',10] = 0
data[data[0::,10] =='S',10] = 1
data[data[0::,10] =='Q',10] = 2
data[data[0::,10] == '',10] = 3
#find the most common embark point, put it in the blanks
data[data[0::,10] == 3,10] = max(Counter(data[data[0::,10] != 3,10]))
#take means for age for each class
firstclassage = []
firstclassfare = []
secondclassage = []
secondclassfare = []
thirdclassage = []
thirdclassfare = []
for x in data:
if x[1] == '1' and x[4]:
firstclassage.append(np.float(x[4]))
if x[1] == '1' and x[8]:
firstclassfare.append(np.float(x[8]))
if x[1] == '2' and x[4]:
secondclassage.append(np.float(x[4]))
if x[1] == '2' and x[8]:
secondclassfare.append(np.float(x[8]))
if x[1] == '3' and x[4]:
thirdclassage.append(np.float(x[4]))
if x[1] == '3' and x[8]:
thirdclassfare.append(np.float(x[8]))
firstclassageaverage = int(np.mean(firstclassage))
secondclassageaverage = int(np.mean(secondclassage))
thirdclassageaverage = int(np.mean(thirdclassage))
firstclassfareaverage = int(np.mean(firstclassfare))
secondclassfareaverage = int(np.mean(secondclassfare))
thirdclassfareaverage = int(np.mean(thirdclassfare))
#put those averages back into the '' ages
for i in xrange(np.size(data[0::,0])):
try:
float(data[i,4])
except ValueError:
if data[i,1] == '1':
data[i,4] = firstclassageaverage
if data[i,1] == '2':
data[i,4] = secondclassageaverage
if data[i,1] == '3':
data[i,4] = thirdclassageaverage
try:
float(data[i,8])
except ValueError:
if data[i,1] == '1':
data[i,8] = firstclassfareaverage
if data[i,1] == '2':
data[i,8] = secondclassfareaverage
if data[i,1] == '3':
data[i,8] = thirdclassfareaverage
#clean up the name and ticket elements
data = np.delete(data,[2,7],1)
#change strings to float
for i in xrange(np.size(data[0::,0])):
for y in range(9):
try:
data[i,y] = num(data[i,y])
except ValueError:
print y
print data[i,y]
print '--'
#data[i,y] = num(0)
return data
#now do the same for the test data
test_file_object = csv.reader(open('test.csv','rb'))
header = test_file_object.next()
test_data = []
for row in test_file_object:
test_data.append(row)
test_data = np.array(test_data)
test_data = np.insert(test_data,[0], 0, axis=1)
#fix the data
train_data = fixdata(train_data)
test_data = fixdata(test_data)
#FOREST IT UP
forest = RandomForestClassifier(n_estimators=80)
print "Fitting RForest"
forest = forest.fit(train_data[0::,1::], train_data[0::,0])
print "Prediciting RForest"
output = forest.predict(test_data[0::,1::])
open_file_object = csv.writer(open("ericsforest.csv", "wb"))
test_file_object = csv.reader(open('test.csv', 'rb'))
test_file_object.next()
i = 0
for row in test_file_object:
row.insert(0,output[i].astype(np.uint8))
open_file_object.writerow(row)
print row
print i
i += 1