-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomForestEntropyDepths.py
More file actions
53 lines (48 loc) · 2.1 KB
/
Copy pathrandomForestEntropyDepths.py
File metadata and controls
53 lines (48 loc) · 2.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pandas as pd
import numpy as np
import timeit
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import tree
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier, Perceptron
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
heldout = [0.95, 0.90, 0.75, 0.50, 0.01]
rounds = 20
columnNames = ["ID","Malicious"]
columnNames.extend(range(1,532))
virus = pd.read_csv('/Users/lerin/Documents/Uni/TUWien/Machine Learning/Exercise 1/VirusDataset/train.csv', names = columnNames)
X, y = virus.drop('Malicious',axis=1).drop('ID',axis=1)[1:].as_matrix(), virus['Malicious'][1:].values
dep = range(1,10)
def my_range(start, end, step):
while start <= end:
yield start
start += step
for z in dep:
classifiers = [("Random forest: entropy "+ str(z) +" depths no bootstrap", RandomForestClassifier(criterion='entropy',max_depth=z,bootstrap=False))]
xx = 1. - np.array(heldout)
for name, clf in classifiers:
print("training %s" % name)
rng = np.random.RandomState(42)
yy = []
start = timeit.default_timer()
for i in heldout:
yy_ = []
for r in range(rounds):
X_train, X_test, y_train, y_test = \
train_test_split(X, y, test_size=i, random_state=rng)
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
stop = timeit.default_timer()
#prediction_dataframe = pd.DataFrame(data=y_pred, index=y_test.index, columns=['Malicious'])
#test_dataframe = pd.DataFrame(data=y_test, index=y_test.index, columns=['Malicious'])
#prediction_dataframe.to_csv("solution-" + str(i) + "-" + str(r) + ".csv")
yy_.append(1 - np.mean(y_pred == y_test))
yy.append(np.mean(yy_))
plt.plot(xx, yy, label=name + " " + str(stop - start) + "s")
plt.legend(loc="upper right")
plt.xlabel("Proportion train")
plt.ylabel("Test Error Rate")
plt.show()