-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_GeneratePredictionError.py
114 lines (88 loc) · 3.49 KB
/
main_GeneratePredictionError.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
"""
"""
print(__doc__)
import pandas as pd
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
import DyMMMSettings as settings
communityDir=communitiesDir=settings.simSettings["communitiesDir"]+"/error/"+sys.argv[1]+"/"
X_train=pd.read_csv(communityDir+"X_train.csv")
FEATURE_NAMES = X_train.columns.tolist()
y_train=pd.read_csv(communityDir+"y_train.csv")
print(y_train.shape)
print("-----------------------------------------------------------")
print(X_train.shape)
print(y_train.shape)
y_train.loc[y_train['CSI'] < 0.9] = 0
y_train.loc[y_train['CSI'] >= 0.9] = 1
encoder = LabelEncoder()
encoder.fit(y_train)
encoded_Y = encoder.transform(y_train)
y_train=encoded_Y
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.model_selection import cross_val_score
from sklearn.metrics import accuracy_score
h = .02 # step size in the mesh
# names = ["Nearest Neighbors", "Linear SVM", "RBF SVM", "Gaussian Process",
# "Decision Tree", "Random Forest", "Neural Net", "AdaBoost",
# "Naive Bayes", "QDA"]
names = ["Random Forrest"]
classifiers = [
# KNeighborsClassifier(3),
# SVC(kernel="linear", C=0.025),
# SVC(gamma=2, C=1),
#GaussianProcessClassifier(1.0 * RBF(1.0)),
#DecisionTreeClassifier(max_depth=2,random_state=0),
RandomForestClassifier(max_depth=2, n_estimators=100, max_features=X_train.shape[1], random_state=0),
# MLPClassifier(alpha=1, max_iter=1000),
# AdaBoostClassifier(),
# GaussianNB(),
# QuadraticDiscriminantAnalysis()
]
trainRows=int(sys.argv[2])
testRows=int(sys.argv[3])
print("----------------------------------------------------------------------------")
# iterate over classifiers
for name, clf in zip(names, classifiers):
print(name)
X_trainrows=X_train[:trainRows]
y_trainrows=y_train[:trainRows]
score = cross_val_score(clf, X_trainrows, y_trainrows, cv=int(X_trainrows.shape[0]/500))
clf.fit(X_trainrows, y_trainrows)
X_testrows=X_train[trainRows:trainRows+testRows]
y_testrows=y_train[trainRows:trainRows+testRows]
print("----------------------------------------------------------------------------")
print(trainRows)
print(trainRows+testRows)
print(X_train.shape)
print(y_train.shape)
print(X_testrows)
print(y_testrows)
y_pred=clf.predict(X_testrows)
y_pred_prb=clf.predict_proba(X_testrows)
accuracy=accuracy_score(y_testrows, y_pred, normalize=True)
file1 = open(communityDir+"/modelError.txt", "a") # append mode
file1.write("Accuracy for %s: %0.1f%% %0.1f%% \n" % (name, score.mean()*100 , accuracy * 100))
file1.close()
trainRows+=testRows