-
Notifications
You must be signed in to change notification settings - Fork 106
/
run.py
51 lines (40 loc) · 1.4 KB
/
run.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
import csv
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.metrics import precision_recall_fscore_support
import numpy as np
def convertToFloat(lst):
return np.array(lst).astype(np.float)
def fetchData(path):
labels = []
data = []
f = open(path)
csv_f = csv.reader(f)
for row in csv_f:
labels.append(convertToFloat(row[0]))
data.append(convertToFloat(row[1:]))
f.close()
return np.array(data), np.array(labels)
# Random Forest Classifier
def runForest(X_train, y_train):
forest = RandomForestClassifier(n_estimators=90, random_state=42)
forest.fit(X_train, y_train)
return forest
# Stochastic Gradient Descent Classifier
def runSGD(X_train, y_train):
sgd = SGDClassifier(n_iter=500, loss='modified_huber', penalty='elasticnet', random_state=42)
sgd.fit(X_train, y_train)
return sgd
def getScores(clf, X, y):
predictions = clf.predict(X)
scores = precision_recall_fscore_support(y, predictions, average='binary')
return scores
# Import data
X_test, y_test = fetchData('data/test.csv')
X_train, y_train = fetchData('data/train.csv')
forest = runForest(X_train, y_train)
forest_scores = getScores(forest, X_test, y_test)
print 'Random Forest Scores: ', forest_scores
sgd = runSGD(X_train, y_train)
sgd_scores = getScores(sgd, X_test, y_test)
print 'SGD Scores: ', sgd_scores