-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathml_v2.py
76 lines (62 loc) · 2.08 KB
/
ml_v2.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
import gc
import pandas as pd
import numpy as np
from sklearn import tree
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
###Training part###
# Traning data
train = pd.read_csv("train_data.csv")
print("Traning data: successfully")
# Features for trainig
column_labels = list(train.columns.values)
column_labels.remove("id")
column_labels.remove("date_recorded")
column_labels.remove("status_group")
column_labels.remove("wpt_name")
column_labels.remove("subvillage")
column_labels.remove("funder")
column_labels.remove("installer")
column_labels.remove("scheme_name")
column_labels.remove("ward")
status_group = ["functional", "non functional", "functional needs repair"]
print("Features for trainig: successfully")
# Assign data for validation
amount = int(0.8*len(train))
validation = train[amount:]
train = train[:amount]
print("Assign data for validation: successfully")
# Classifier
# clf = tree.DecisionTreeClassifier()
clf = RandomForestClassifier(n_estimators = 700, n_jobs = -1)
print("Classifier: successfully")
# Traning
clf.fit(train[column_labels], train["status_group"])
print("Traning: successfully")
# Accuracy
accuracy = accuracy_score(clf.predict(validation[column_labels]), validation["status_group"])
print("Accuracy = " + str(accuracy))
print("Accuracy: successfully")
# Free some ram
del train, validation
gc.collect()
###Testing part###
# Testing data
test = pd.read_csv("test.csv")
test = test.fillna(test.median())
print("Testing data: successfully")
# Prediction for test data
prediction = clf.predict(test[column_labels])
print("Prediction for test data: successfully")
### Making submission file###
# Dataframe as per submission format
submission = pd.DataFrame({
"id": test["id"],
"status_group": prediction
})
for i in range(len(status_group)):
submission.loc[submission["status_group"] == i, "status_group"] = status_group[i]
print("Dataframe as per submission format: successfully")
# Store submission dataframe into file
submission.to_csv("submission.csv", index = False)
print("Store submission dataframe into file: successfully")