forked from Arkham/jack-dies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_forest.py
40 lines (30 loc) · 1.11 KB
/
random_forest.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
import utils
import numpy as np
import pandas as pd
from sklearn import ensemble, model_selection
train = pd.read_csv("./data/train.csv")
test = pd.read_csv("./data/test.csv")
print "\nCleaning up some data"
utils.clean_data(train)
utils.clean_data(test)
print "\nExtracting target and features"
print(train.shape)
target = train["Survived"].values
features_forest = train[["Pclass", "Age", "Sex", "Fare", "SibSp", "Parch", "Embarked"]].values
print "\nUse Random Forest classifier"
forest = ensemble.RandomForestClassifier(
max_depth = 7,
min_samples_split = 4,
n_estimators = 1000,
random_state = 1,
n_jobs = -1
)
forest = forest.fit(features_forest, target)
print(forest.feature_importances_)
print(forest.score(features_forest, target))
scores = model_selection.cross_val_score(forest, features_forest, target, scoring='accuracy', cv=10)
print scores
print scores.mean()
test_features_forest = test[["Pclass", "Age", "Sex", "Fare", "SibSp", "Parch", "Embarked"]].values
prediction_forest = forest.predict(test_features_forest)
utils.write_prediction(prediction_forest, "results/random_forest.csv")