在实验一中,已经下载好莺尾花数据集和对数据集进行整理,在实验二中,接着对莺尾花数据集进行分析实验
任务一:
用Python编程实现训练数据集与测试数据集
– 留出法:
• 百分比自己设定,合理即可
• 须进行100次随机划分(这里代码给出循坏10次)
–k折交叉验证法:
• k自己设定(k设定为10)
• p次k折交叉验证
#留出法:
selected_features = data.drop('class', axis=1)
target = data['class']
label_encoder = LabelEncoder()
target = label_encoder.fit_transform(target)
for i in range(10):
# 分割训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(selected_features, target, test_size=0.2, random_state=42)
print(X_train, X_test)
print("训练集数据大小:", X_train.shape[0])
print("测试集数据大小:", X_test.shape[0])
print("+++++++++++++++++++++++++++++++++++++++++++++++++++")
print(y_train, y_test)
print("---------------------------------------------------")
#k折交叉验证法:
selected_features = data.drop('class',axis=1)
target = data['class']
label_encoder = LabelEncoder()
target = label_encoder.fit_transform(target)
#
k = 10
p = 5
kf = StratifiedKFold(n_splits=k)
for train_index, test_index in kf.split(selected_features, target):
X_train, X_test = selected_features.iloc[train_index], selected_features.iloc[test_index]
y_train, y_test = target[train_index], target[test_index]
# 输出交叉验证结果
print(X_train, X_test)
print("训练集数据大小:", X_train.shape[0])
print("测试集数据大小:", X_test.shape[0])
print("+++++++++++++++++++++++++++++++++++++++++++++++++++")
print(y_train, y_test)
print("---------------------------------------------------")
任务二:
编程实现性能度量
– 均方误差MSE
– 错误率与精度
– 查准率与查全率
– F1指数
– ROC曲线,并计算AUC
• 假定输入数据为原数据经过分类器之后的一个0到1之间的实数
由于实验二未进行模型训练,所以在任务二中只是进行赋值操作,所赋的值,为原数据经过分类器之后的一个0到1之间的实数
# 假设原数据经过分类器后的预测结果(0到1之间的实数)
y_true = np.array([0, 1, 0, 1, 0])
y_pred = np.array([0.1, 0.7, 0.3, 0.8, 0.2])
# 均方误差(Mean Squared Error)
mse = mean_squared_error(y_true, y_pred)
print("均方误差(MSE):", mse)
# 错误率与精度(Accuracy)
accuracy = accuracy_score(y_true, np.round(y_pred))
error_rate = 1 - accuracy
print("错误率:", error_rate)
print("精度:", accuracy)
# 查准率与查全率(Precision和Recall)
precision = precision_score(y_true, np.round(y_pred))
recall = recall_score(y_true, np.round(y_pred))
print("查准率(Precision):", precision)
print("查全率(Recall):", recall)
# F1指数
f1 = f1_score(y_true, np.round(y_pred))
print("F1指数:", f1)
# ROC曲线和AUC
fpr, tpr, thresholds = roc_curve(y_true, y_pred)
auc_score = auc(fpr, tpr)
print("AUC:", auc_score)
# 绘制ROC曲线
plt.plot(fpr, tpr, color='blue', label='ROC Curve (AUC = %0.2f)' % auc_score)
plt.plot([0, 1], [0, 1], color='red', linestyle='--', label='Random')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic (ROC) Curve')
plt.legend(loc="lower right")
plt.show()