机器学习过程中的第一个应用:
from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import preprocessing
from sklearn import tree
all_data = open (r'E:\python资料\2018Python大礼包\机器学习与深度学习视频教程\02_深度学习基础\代码与素材(1)\01DTree\AllElectronics.csv','r'
)
#csv.reader(f,delimiter(分隔符),quoting(引用符))
reader = csv.reader(all_data,delimiter = ',',quoting = csv.QUOTE_NONE)
header = next(reader)
#print (header)
#第一阶段,将标记和特征向量组成的dict分别保存在不同的list中
feature_list = []
label_list = []
#目的为了将[{}]转换成sklearn模块可以识别的格式
#通过前面的next()调用,header将不会参与以下循环
for line in reader:
label_list.append(line[len(line)-1])
d = {}
for i in range(1,len(line)-1):
d[ header[i] ] = line[i]
feature_list.append(d)
print (feature_list)
#-------------第二阶段----------------------
#数据用字典格式存储,通过调用DictVectorizer类可将其转换成特征
vec = DictVectorizer()
dummyX = vec.fit_transform(feature_list).toarray()
feature_name = vec.get_feature_names()
print("dummyX: " , dummyX)
print(feature_name)
print("labelList: " , label_list)
#标签二值化(Label binarization)
lb = preprocessing.LabelBinarizer()
dummyY = lb.fit_transform(label_list)
print (dummyY)
#
clf = tree.DecisionTreeClassifier(criterion = 'entropy')
#fit(x,y)有监督学习下应用,无监督学习下用fit(x)
#构建模型
clf = clf.fit(dummyX,dummyY)
print ('clf:',clf)
# #标签二进制器
# print (lb.fit([1,2,3]))
# #标签二值化器并将多类标签转换为二元标签
# print (lb.fit_transform([1,2,3]))
# #获取参数
# print (lb.get_params())
# print (lb.transform([1,2,3,2,5]))
#------------------可视化决策树模型--------------------
with open (r'E:\python资料\2018Python大礼包\机器学习与深度学习视频教程\02_深度学习基础\代码与素材(1)\01DTree\AllElectronics.dot','w') as f:
f = tree.export_graphviz(clf,feature_names = feature_name,out_file = f)
#在命令行指定路径下输入dot -Tpdf file.dot -o output.pdf,得到可视化文件
#----------------第三阶段,预测模型-----------------
lineX = dummyX[0]
print (lineX)
lineX[0] = 1
lineX[2] = 0
predictY = clf.predict(lineX)
print (predictY)
#-----------------------------新技能get------------------------------