- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrossValidate.py
More file actions
Latest commit
29 lines (23 loc) · 1.1 KB
/
Copy pathcrossValidate.py
File metadata and controls
29 lines (23 loc) · 1.1 KB
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
fromsklearn.ensembleimportRandomForestClassifier
fromsklearnimportcross_validation
importlogloss
importnumpyasnp
defmain():
#read in data, parse into training and target sets
dataset=np.genfromtxt(open('Data/train.csv','r'), delimiter=',', dtype='f8')[1:]
target=np.array([x[0] forxindataset])
train=np.array([x[1:] forxindataset])
#In this case we'll use a random forest, but this could be any classifier
cfr=RandomForestClassifier(n_estimators=100)
#Simple K-Fold cross validation. 5 folds.
cv=cross_validation.KFold(len(train), k=5, indices=False)
#iterate through the training and test cross validation segments and
#run the classifier on each one, aggregating the results into a list
results= []
fortraincv, testcvincv:
probas=cfr.fit(train[traincv], target[traincv]).predict_proba(train[testcv])
results.append( logloss.llfun(target[testcv], [x[1] forxinprobas]) )
#print out the mean of the cross-validated results
print"Results: "+str( np.array(results).mean() )
if__name__=="__main__":
main()