def title(y_pred, y_test, target_names, i): pred_name = target_names[y_pred[i]].rsplit(' ', 1)[-1] true_name = target_names[y_test[i]].rsplit(' ', 1)[-1] return 'predicted: %s\ntrue: %s' % (pred_name, true_name)
时间: 2024-04-27 12:23:11 浏览: 152
这段代码定义了一个名为title()的函数,该函数用于生成一个图像的标题,该标题包括该图像的真实标签和预测标签。该函数接受以下参数作为输入:
1. y_pred:预测结果向量数组。
2. y_test:测试集的标签向量数组。
3. target_names:标签名称数组。
4. i:要生成标题的图像索引。
该函数返回一个字符串,作为该图像的标题。
具体的操作包括:
1. 从目标名称中提取预测标签和真实标签的名称。
2. 格式化字符串并返回结果。
需要注意的是,该函数假定标签名称遵循一定的命名规则,即标签名称包含一个空格,空格之后是标签的名称。
相关问题
import pandas as pd from sklearn.datasets import load_wine from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.feature_selection import SelectKBest, f_classif from sklearn.decomposition import PCA from sklearn.metrics import accuracy_score, classification_report from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score from sklearn.ensemble import RandomForestClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.naive_bayes import GaussianNB from sklearn.linear_model import LogisticRegression from sklearn.svm import SVC data = load_wine() # 导入数据集 X = pd.DataFrame(data.data, columns=data.feature_names) y = pd.Series(data.target) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) # 构建分类模型 model = LogisticRegression() model.fit(X_train, y_train) # 预测测试集结果 y_pred = model.predict(X_test) #评估模型性能 accuracy = accuracy_score(y_test, y_pred) report = classification_report(y_test, y_pred) print('准确率:', accuracy) # 特征选择 selector = SelectKBest(f_classif, k=6) X_new = selector.fit_transform(X, y) print('所选特征:', selector.get_support()) # 模型降维 pca = PCA(n_components=2) X_new = pca.fit_transform(X_new) # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X_new, y, test_size=0.2, random_state=0) def Sf(model,X_train, X_test, y_train, y_test,modelname): mode = model() mode.fit(X_train, y_train) y_pred = mode.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(modelname, accuracy) importance = mode.feature_importances_ print(importance) def Sf1(model,X_train, X_test, y_train, y_test,modelname): mode = model() mode.fit(X_train, y_train) y_pred = mode.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print(modelname, accuracy) modelname='支持向量机' Sf1(SVC,X_train, X_test, y_train, y_test,modelname) modelname='逻辑回归' Sf1(LogisticRegression,X_train, X_test, y_train, y_test,modelname) modelname='高斯朴素贝叶斯算法训练分类器' Sf1(GaussianNB,X_train, X_test, y_train, y_test,modelname) modelname='K近邻分类' Sf1(KNeighborsClassifier,X_train, X_test, y_train, y_test,modelname) modelname='决策树分类' Sf(DecisionTreeClassifier,X_train, X_test, y_train, y_test,modelname) modelname='随机森林分类' Sf(RandomForestClassifier,X_train, X_test, y_train, y_test,modelname)加一个画图展示
抱歉,由于我是一个纯文本AI,无法进行画图展示。但是,你可以使用Python中的matplotlib库进行数据可视化。例如,你可以使用以下代码绘制特征重要性条形图:
import matplotlib.pyplot as plt
importance = mode.feature_importances_
features = list(X.columns)
plt.barh(features, importance)
plt.title('Feature Importance')
plt.xlabel('Importance')
plt.show()
这段代码可以在Sf函数中的print(importance)下方添加,并在运行时展示特征重要性条形图。
阅读全文