-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifiers.py
More file actions
53 lines (42 loc) · 1.97 KB
/
classifiers.py
File metadata and controls
53 lines (42 loc) · 1.97 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
from kmean import movies_meta_data
import pandas as pd
import sklearn
from sklearn import tree
from sklearn import preprocessing
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import MinMaxScaler
import graphviz
# Read in the movies_meta_data from csv
# movies_meta_data = pd.read_csv("movies_meta_data_after_preprocessing.csv")
# Equal frequency binning of ROI
# levels = pd.qcut(movies_meta_data['return_on_investment'], 5, labels=['Very Low', 'Low', 'Average', 'High', 'Very High'])
levels = movies_meta_data['return_on_investment_label']
x_train = movies_meta_data.copy()
del x_train['return_on_investment']
del x_train['return_on_investment_label']
# Split the dataset into train/test with 2/3 being training data and 1/3 being testing data.
X_train, X_test, y_train, y_test = sklearn.model_selection.train_test_split(x_train, levels, shuffle=True, test_size=0.2)
# Normalization
# scaler = MinMaxScaler()
# X_train = scaler.fit_transform(X_train)
# X_test = scaler.transform(X_test)
# Conclusion: Normalization does not improve the accuracy for DT and RF
# Decision Tree
clf = tree.DecisionTreeClassifier(max_depth=8, min_samples_split=15)
clf = clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print("Accuracy of Decision Tree classifier: ")
print(clf.score(X_test, y_test))
cv_score = cross_val_score(clf, x_train, levels, cv=10)
print("Accuracy of Decision Tree classifier after cross validation: ")
print("Accuracy: %0.2f (+/- %0.2f)" % (cv_score.mean(), cv_score.std() * 2))
dot_data = tree.export_graphviz(clf, out_file=None)
graph = graphviz.Source(dot_data)
graph.render("movie_tree")
# Random Forest
clf_random_forest = RandomForestClassifier().fit(X_train, y_train)
print("Accuracy of Random Forest classifier: ")
print(clf_random_forest.score(X_test, y_test))
print("Accuracy of Decision Tree classifier after cross validation: ")
print(cross_val_score(clf_random_forest, x_train, levels, cv=10))