-
Notifications
You must be signed in to change notification settings - Fork 49
/
decision_tree_sklearn.py
60 lines (45 loc) · 1.36 KB
/
decision_tree_sklearn.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
#coding:utf-8
#Author:codewithzichao
#E-mail:lizichao@pku.edu.cn
'''
数据集:mnist
accuaracy:0.8659.
time:14.435183763504028.
'''
import pandas as pd
import numpy as np
from sklearn import tree
import time
def loadData(fileName):
#从文件中读取数据
data=pd.read_csv(fileName,header=None)
# 将数据从dataframe转化为ndarray
data=data.values
#数据第一行为分类结果
y_label=data[:,0]
x_label=data[:,1:]
y_label=np.array(y_label).reshape(-1)
x_label=np.array(x_label)
#数据二值化,返回数据
#因为xi的取值范围为0-255,则计算p(X=xi\Y=y)的时候可能性过多,计算过于繁杂
# 所以进行二值化
# y_label为np.ndarray,x_label为np.ndarray
x_label[x_label<128]=0
x_label[x_label>=128]=1
# mp.ndarray
return x_label,y_label
if __name__=="__main__":
# 获取当前时间
start = time.time()
# 读取训练文件
print("load train data")
X_train,y_train = loadData('../MnistData/mnist_train.csv')
# 读取测试文件
print('load test data')
X_test,y_test = loadData('../MnistData/mnist_test.csv')
clf = tree.DecisionTreeClassifier()
clf.fit(X_train,y_train)
test_accuracy=clf.score(X_test, y_test)
print(f"the test_accuracy is {test_accuracy}.")
end=time.time()
print(f"the total time is {end-start}.")