-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
93 lines (79 loc) · 3.36 KB
/
template.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#PLEASE WRITE THE GITHUB URL BELOW!
#https://github.com/jgw1202/OSSPJ
import sys
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.pipeline import make_pipeline
def load_dataset(dataset_path):
#To-Do: Implement this function
data_df = pd.read_csv(dataset_path)
return data_df
def dataset_stat(dataset_df):
#To-Do: Implement this function
feature = len(dataset_df.columns) -1
class0 = len(dataset_df.iloc[:,-1][dataset_df.target == 0].index)
class1 = len(dataset_df.iloc[:,-1][dataset_df.target == 1].index)
return feature, class0, class1
def split_dataset(dataset_df, testset_size):
#To-Do: Implement this function
x= data_df.drop(columns="target", axis=1)
y = data_df["target"]
x_train, x_test, y_train, y_test = train_test_split(x,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_pipe = make_pipeline(
StandardScaler(),
SVC()
)
svm_pipe.fit(x_train, y_train)
acc = accuracy_score(y_test, svm_pipe.predict(x_test))
prec = precision_score(y_test, svm_pipe.predict(x_test))
recall = recall_score(y_test, svm_pipe.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)