-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearning.py
More file actions
71 lines (58 loc) · 2.52 KB
/
Copy pathlearning.py
File metadata and controls
71 lines (58 loc) · 2.52 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
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
import pandas as pd
import numpy as np
import pickle
import sklearn.ensemble as ske
from sklearn import tree, linear_model
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectFromModel
# from sklearn.externals import joblib
import joblib
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import confusion_matrix
data = pd.read_csv('data.csv', sep='|', encoding_errors='ignore')
X = data.drop(['Name', 'b_suspicious'], axis=1).values
y = data['b_suspicious'].values
print('Researching important feature based on %i total features in %d rows\n' % (X.shape[1], len(y)))
# Feature selection using Trees Classifier
fsel = ske.ExtraTreesClassifier().fit(X, y)
model = SelectFromModel(fsel, prefit=True)
X_new = model.transform(X)
nb_features = X_new.shape[1]
X_train, X_test, y_train, y_test = train_test_split(X_new, y, test_size=0.2)
features = []
print('%i features identified as important:' % nb_features)
indices = np.argsort(fsel.feature_importances_)[::-1][:nb_features]
for f in range(nb_features):
print("%d. feature %s (%f)" % (f + 1, data.columns[2 + indices[f]], fsel.feature_importances_[indices[f]]))
# XXX : take care of the feature order
for f in sorted(np.argsort(fsel.feature_importances_)[::-1][:nb_features]):
features.append(data.columns[2 + f])
# Algorithm comparison
algorithms = {
"DecisionTree" : tree.DecisionTreeClassifier(max_depth=10),
"RandomForest" : ske.RandomForestClassifier(n_estimators=50),
"GradientBoosting": ske.GradientBoostingClassifier(n_estimators=50),
"AdaBoost" : ske.AdaBoostClassifier(n_estimators=100),
"GNB" : GaussianNB()
}
results = {}
print("\nNow testing algorithms")
for algo in algorithms:
clf = algorithms[algo]
clf.fit(X_train, y_train)
score = clf.score(X_test, y_test)
print(f"{algo} : {score * 100:.2f}%")
results[algo] = score
winner = max(results, key=results.get)
print(f'\nWinner algorithm is {winner} with a {results[winner] * 100:.2f} %% success')
# Save the algorithm and the feature list for later predictions
print('Saving algorithm and feature list in classifier directory...')
joblib.dump(algorithms[winner], 'classifier/classifier.pkl')
w = open('classifier/features.pkl', 'wb').write(pickle.dumps(features))
print(f'Saved {w}')
# Identify false and true positive rates
clf = algorithms[winner]
res = clf.predict(X_test)
mt = confusion_matrix(y_test, res)
print(f"False positive rate : {(mt[0][1] / float(sum(mt[0]))) * 100:.2f}%")
print(f'False negative rate : {(mt[1][0] / float(sum(mt[1]))) * 100:.2f}%')