-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.py
More file actions
90 lines (74 loc) · 3.11 KB
/
template.py
File metadata and controls
90 lines (74 loc) · 3.11 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#PLEASE WRITE THE GITHUB URL BELOW!
# https://github.com/fivedasol/OSS_Project2.git
import sys
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score
def load_dataset(dataset_path):
#To-Do: Implement this function
pop = pd.read_csv(dataset_path)
frame = pd.DataFrame(pop)
return frame
def dataset_stat(dataset_df):
#To-Do: Implement this function
n_feats = dataset_df.shape[1] - 1
n_class0 = len(dataset_df.loc[dataset_df['target'] == 0])
n_class1 = len(dataset_df.loc[dataset_df['target'] == 1])
return n_feats, n_class0, n_class1
def split_dataset(dataset_df, testset_size):
#To-Do: Implement this function
df_x = dataset_df.drop(['target'], axis = 1)
df_y = dataset_df['target']
x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=testset_size)
return x_train, x_test, y_train, y_test
def decision_tree_train_test(x_train, x_test, y_train, y_test):
#To-Do: Implement this function
dt_cls = DecisionTreeClassifier()
dt_cls.fit(x_train, y_train)
acc = accuracy_score(y_test, dt_cls.predict(x_test))
prec = precision_score(y_test, dt_cls.predict(x_test))
recall = recall_score(y_test, dt_cls.predict(x_test))
return acc, prec, recall
def random_forest_train_test(x_train, x_test, y_train, y_test):
#To-Do: Implement this function
rf_cls = RandomForestClassifier()
rf_cls.fit(x_train, y_train)
acc = accuracy_score(y_test, rf_cls.predict(x_test))
prec = precision_score(y_test, rf_cls.predict(x_test))
recall = recall_score(y_test, rf_cls.predict(x_test))
return acc, prec, recall
def svm_train_test(x_train, x_test, y_train, y_test):
#To-Do: Implement this function
svm_cls = SVC()
svm_cls.fit(x_train, y_train)
acc = accuracy_score(y_test, svm_cls.predict(x_test))
prec = precision_score(y_test, svm_cls.predict(x_test))
recall = recall_score(y_test, svm_cls.predict(x_test))
return acc, prec, recall
def print_performances(acc, prec, recall):
#Do not modify this function!
print ("Accuracy: ", acc)
print ("Precision: ", prec)
print ("Recall: ", recall)
if __name__ == '__main__':
#Do not modify the main script!
data_path = sys.argv[1]
data_df = load_dataset(data_path)
n_feats, n_class0, n_class1 = dataset_stat(data_df)
print ("Number of features: ", n_feats)
print ("Number of class 0 data entries: ", n_class0)
print ("Number of class 1 data entries: ", n_class1)
print ("\nSplitting the dataset with the test size of ", float(sys.argv[2]))
x_train, x_test, y_train, y_test = split_dataset(data_df, float(sys.argv[2]))
acc, prec, recall = decision_tree_train_test(x_train, x_test, y_train, y_test)
print ("\nDecision Tree Performances")
print_performances(acc, prec, recall)
acc, prec, recall = random_forest_train_test(x_train, x_test, y_train, y_test)
print ("\nRandom Forest Performances")
print_performances(acc, prec, recall)
acc, prec, recall = svm_train_test(x_train, x_test, y_train, y_test)
print ("\nSVM Performances")
print_performances(acc, prec, recall)