-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
33 lines (33 loc) · 1007 Bytes
/
main.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
import numpy as np
from sklearn import svm,naive_bayes,ensemble,linear_model
from sklearn.metrics import accuracy_score
import time
import pickle
filename = "ApneaData.pkl"
testPercent=20
features = []
classes = []
t = time.time()
f = open(filename,'rb')
data = pickle.load(f)
f.close()
np.random.shuffle(data)
for row in data:
features.append(row[:-1])
classes.append(row[-1])
inputLength = len(features)
testLength = int(inputLength*0.2)
train_features, train_classes=features[:-testLength], classes[:-testLength]
test_features,test_classes = features[-testLength:],classes[-testLength:]
print("preprocessing time:",(time.time()-t))
t=time.time()
clf=ensemble.RandomForestClassifier(n_estimators=30)
clf.fit(train_features,train_classes)
print("fitting time:",(time.time()-t))
t=time.time()
pred_classes=[]
for e in test_features:
pred_classes.append(clf.predict([e])[0])
score = accuracy_score(pred_classes,test_classes)*100
print("predicting time:",(time.time()-t))
print("Accuracy:",score)