5行×25列 2行×24列

5行×25列 2行×24列featurespe=['cap-sha,'cap-surface','cap-color']target=['class']X=dataset[features]y=dataset[target]dataset.shape#比官方文档(8122,25)dataset少了两个蘑菇.groupby('class').count()#每个我们数据的特征提取,包括目标参数,是分类数据。为了使用机器学习,我们需要将这些值转换成数值数据。要从数据集中提取它,我们必须使用Scikit-Learn的转换器将输入数据集转换为适合模型的数据集。幸运的是,Sckit-Learn提供了一个将分类标签转换为整数的转换器:sklearn.preprocessing.LabelEncoder。不幸的是,它一次只能转换一个向量,所以我们必须调整它以将其应用于多列。有疑问,这个蘑菇分类就是一个向量吗?def__init__(self,columns=None):self.columns=[colforcolincolumns]self.encoders=Nonedeffit(self,data,target=None):"""期望具有命名列的数据框进行编码."""#EncodeallcolumnsifcolumnsisNoneifself.columnsisNone:self.columns=data.columns#为数据框中的每一列设置一个标签编码器self.encoders={column:LabelEncoder().fit(data[column])forcolumninself.columns}returnselfdeftransform(self,data):"""使用编码器转换数据帧。"""output=data.copy()forcolumn,encoderinself.encoders.items():output[column]=encoder.transform(data[column])返回输出建模和评估评估分类器的常用指标精度(Precision)对正结果正确计数除以所有阳性的数量(例如,我们预测实际有多少可食用蘑菇?)召回率是正确阳性的数量除以应该返回的阳性数量(例如,我们是否准确预测了多少有毒蘑菇是有毒的?)F1分数(F1score)是衡量测试准确度的指标。它同时考虑了测试的精确率和召回率来计算分数。F1分数可以解释为精度和召回率的加权平均值,其中F1分数在1时达到最佳值,在0时达到最差值。精度=真阳性/(真阳性+假阳性)召回=真阳性/(falsenegatives+truepositives)F1score=2((precisionrecall)/(precision+recall))现在我们准备好做一些预测了!让我们构建一种评估多个估计器的方法-首先使用传统的数字分数(稍后我们将与Yellowbrick库中的一些视觉诊断进行比较)。fromsklearn.metricsimportf1_scorefromsklearn.pipelineimportPipelinedefmodel_selection(X,y,estimator):"""测试各种估计器。"""y=LabelEncoder().fit_transform(y.values.ravel())model=Pipeline([('label_encoding',EncodeCategorical(X.keys())),('one_hot_encoder',OneHotEncoder(categories='auto')),#此处增加自动分类,否则有warning('estimator',estimator)])#实例化分类模型和可视化器model.fit(X,y)expected=ypredicted=model.predict(X)#计算并返回F1分数(精度和召回率的调和平均值)return(f1_score(expected,predicted))fromsklearn.svmimportLinearSVC,NuSVC,SVCfromsklearn.neighborsimportKNeighborsClassifierfromsklearn.linear_modelimportLogisticRegressionCV,LogisticRegression,SGDClassifierfromsklearn.ensembleimportBaggingClassifier,ExtraTreesClassifier,RandomForestClassifiermodel_selection(X,y,Linea)rSVC())0.6582119537920643importwarningswarnings.filterwarnings("ignore",category=FutureWarning,module="sklearn")#忽略警告model_selection(X,y,NuSVC())0.6878837238441299model_selection(X,y,SVC())0.66251405971model195(X,y,SGDClassifier())0.5738408700629649model_selection(X,y,KNeighborsClassifier())0.6856846473029046model_selection(X,y,LogisticRegressionCV())0.6582119537920643model_selection(X,y,LogisticRegression())0.6578749058025622model_selection(X,y,BaggingClassifier())0.6873901878632248model_selection(X,y,ExtraTreesClassifier())0.6872294372294372model_selection(X,y,RandomForestClassifier())0.6992081007399714初步模型评估根据以上F1分数的结果,哪个模型表现最好?可视化模型评估现在,让我们重构模型评估函数以使用Yellowbrick的ClassificationReport类,这是一个显示精度、召回率和F1分数的模型可视化工具。这种可视化模型分析工具集成了数字分数和颜色编码的热图,以支持轻松解释和检测,尤其是与我们的用例非常相关(危及生命!)的I类错误和II类错误的细微差别。第一种错误(或“误报”)是检测到不存在的影响(例如蘑菇有毒,但实际上可以食用)。第二种类型的错误(或“假阴性”)是未能检测到存在的影响(例如,当蘑菇实际上有毒时却认为它可以食用)。fromsklearn.pipelineimportPipelinefromyellowbrick.classifierimportClassificationReportdefvisual_model_selection(X,y,estimator):"""测试各种估计器。"""y=LabelEncoder().fit_transform(y.values.ravel())model=Pipeline([('label_encoding',EncodeCategorical(X.keys())),('one_hot_encoder',OneHotEncoder()),('estimator',estimator)])#实例化分类模型和可视化器visualizer=ClassificationReport(model,classes=['edible','poisonous'])visualizer.fit(X,y)visualizer.score(X,y)visualizer.poof()visual_model_selection(X,y,LinearSVC())#其他分类器的可视化略visual_model_selection(X,y,RandomForestClassifier())test现在,哪个模型看起来最好?为什么?哪种型号最有可能挽救您的生命?视觉模型评估与数值模型评估有何不同?Precision和recallRecall和综合评价指标F1-Measurehttp://www.makaidong.com/%E5%...f1-score综合考虑准确率和召回率。可视化直观,逃避~作者简介知道yeayee,Py5岁,擅长Flask+MongoDB+SKlearn+Bokeh