-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomForest.py
More file actions
53 lines (41 loc) · 1.59 KB
/
Copy pathRandomForest.py
File metadata and controls
53 lines (41 loc) · 1.59 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
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import statistics
# Creating a DataFrame of dataset
data = pd.read_csv('featureExtractionFinal.csv', low_memory=False)
X = data[['AmountSent', 'Income', 'Neighbours', 'CoAddresses', 'Successors']] # Features
Day_index = data['Day']
y = data['IsRansome'] # Labels
# Organize the data to index it based on when the days change
# This way, we run random forest on each day
previous_day = 1
indexes_of_next_days = []
for i in range(len(Day_index)):
if Day_index[i] != previous_day:
previous_day = Day_index[i]
indexes_of_next_days.append(i)
# Now, we simulate 'time' by training and testing on the next address incrementally
print(len(indexes_of_next_days))
accuracy = []
print_every_x = 5000
# Create a Classifier
clf = RandomForestClassifier(n_estimators=50)
day = 0
for i in indexes_of_next_days:
day += 1
subSetX = X[:i]
subSetY = y[:i]
# Split dataset into training set and test set
X_train, X_test, y_train, y_test = train_test_split(subSetX, subSetY, test_size=0.3)
# Train the model using the training sets
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
accuracy.append(metrics.accuracy_score(y_test, y_pred))
if day % 30 == 0:
print('Progress report: on day ' + str(day))
print('average accuracy: ' + str(statistics.mean(accuracy)))
print('~~~~ printing accuracies for all days ~~~~')
for i in range(len(accuracy)):
print('Day: ' + str(i) + ', accuracy = ' + str(accuracy[i]))