Python学习笔记5-MOSH-6小时-项目( 三 )


写错了不懂为啥为了解释的挺好:
Theusing model.([[21, 1]]) and model.([21, 1]) lies in the shape andof the input data that the.
在 learn中,当使用机器学习模型进行预测时,输入数据应该被结构化为二维数组(或矩阵),即使你是针对单个实例进行预测 。原因是学习模型被设计为一次处理多个样本,并且使用二维数组可以保持一致性 。
Here's why model.([[21, 1]]) is , and model.([21, 1]) is wrong:
1.`model.([[21,1]])`:这正确地提供了一个具有一个样本和两个特征的二维数组 。外括号“[[]]”创建列表列表,其中内列表“[21,1]”表示单个实例的特征 。这种形式符合 learn的期望 。
2.“model.([21,1])”:这是一个一维列表,包含两个值,“21”和“1” 。它缺少创建二维数组所需的外括号“[]” 。因此,learn会将其解释为两个独立的样本,每个样本都有一个功能,这不是您想要的 。
总之,learn的“预测”方法希望输入数据被结构化为二维数组,即使是单个实例 。因此,在进行预测时,重要的是以正确的格式提供输入数据,即“[[]]”来包装实例的特征值 。
Step6:ATree
1.把二叉树写出来,生成.dot文件
import pandas as pdfrom sklearn.tree import DecisionTreeClassifierfrom sklearn import treemusic_data = http://www.kingceram.com/post/pd.read_csv('music.csv')X = music_data.drop(columns=['genre'])Y = music_data['genre']model=DecisionTreeClassifier()model.fit(X,Y)tree.export_graphviz(model,out_file='music-recommender.dot',feature_names= ['age','gender'],class_names =sorted(Y.unique()),label='all',rounded=True,filled=True)
2.文件内容如下:
digraph Tree {node [shape=box, style="filled, rounded", color="black", fontname="helvetica"] ;edge [fontname="helvetica"] ;0 [label="age <= 30.5\ngini = 0.778\nsamples = 18\nvalue = http://www.kingceram.com/post/[3, 6, 3, 3, 3]/nclass = Classical", fillcolor="#e5fad7"] ;1 [label="gender <= 0.5\ngini = 0.75\nsamples = 12\nvalue = http://www.kingceram.com/post/[3, 0, 3, 3, 3]/nclass = Acoustic", fillcolor="#ffffff"] ;0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;2 [label="age <= 25.5\ngini = 0.5\nsamples = 6\nvalue = http://www.kingceram.com/post/[3, 0, 3, 0, 0]/nclass = Acoustic", fillcolor="#ffffff"] ;1 -> 2 ;3 [label="gini = 0.0\nsamples = 3\nvalue = http://www.kingceram.com/post/[0, 0, 3, 0, 0]/nclass = Dance", fillcolor="#39e5c5"] ;2 -> 3 ;4 [label="gini = 0.0\nsamples = 3\nvalue = http://www.kingceram.com/post/[3, 0, 0, 0, 0]/nclass = Acoustic", fillcolor="#e58139"] ;2 -> 4 ;5 [label="age <= 25.5\ngini = 0.5\nsamples = 6\nvalue = http://www.kingceram.com/post/[0, 0, 0, 3, 3]/nclass = HipHop", fillcolor="#ffffff"] ;1 -> 5 ;6 [label="gini = 0.0\nsamples = 3\nvalue = http://www.kingceram.com/post/[0, 0, 0, 3, 0]/nclass = HipHop", fillcolor="#3c39e5"] ;5 -> 6 ;7 [label="gini = 0.0\nsamples = 3\nvalue = http://www.kingceram.com/post/[0, 0, 0, 0, 3]/nclass = Jazz", fillcolor="#e539c0"] ;5 -> 7 ;8 [label="gini = 0.0\nsamples = 6\nvalue = http://www.kingceram.com/post/[0, 6, 0, 0, 0]/nclass = Classical", fillcolor="#7be539"] ;0 -> 8 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;}
和gpt搏斗半天,可以使用在线工具或软件:将你的.dot文件复制到在线可视化工具(例如 )中,然后获取图形预览 。
= ['age',''],生成了决策树的age、
=(Y.()), 生成了上图的class
label='all',箭头
=True,生成了矩形框
【Python学习笔记5-MOSH-6小时-项目】=True)给每个矩形框填充颜色