# 导入必要的库
import numpy as np
import os
from PIL import Image
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, roc_curve, auc
import matplotlib.pyplot as plt
# 加载图像数据
def load_image_data(image_dir, target_size=(256, 256)):
X = []
y = []
class_labels = os.listdir(image_dir)
for label in class_labels:
class_dir = os.path.join(image_dir, label)
if os.path.isdir(class_dir):
for file_name in os.listdir(class_dir):
file_path = os.path.join(class_dir, file_name)
if file_path.endswith(('.png', '.jpg', '.jpeg')):
image = Image.open(file_path)
image = image.resize(target_size)
image = np.array(image)
if image.shape == (256, 256, 3): # 只保留形状为(256, 256, 3)的图像
X.append(image)
y.append(int(label))
return np.array(X), np.array(y)
# 加载数据
image_dir = '/media/wagnchogn/data/01/second/eye-20240622/eye' # 替换为你的图像数据路径
X, y = load_image_data(image_dir)
# 数据预处理:将图像数据展开为一维向量,并标准化数据
X = X.reshape((X.shape[0], -1))
scaler = StandardScaler()
X = scaler.fit_transform(X)
# 定义SVM模型
model = SVC(kernel='rbf', C=0.1, probability=True)
# 定义五折交叉验证
kf = KFold(n_splits=5, shuffle=True, random_state=42)
accuracy_list = []
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# 训练SVM模型
model.fit(X_train, y_train)
# 评估模型
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
accuracy = accuracy_score(y_test, y_pred)
accuracy_list.append(accuracy)
# 计算ROC曲线
fpr, tpr, _ = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
aucs.append(roc_auc)
tprs.append(np.interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
# 绘制平均ROC曲线
plt.figure(figsize=(10, 8))
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
lw=2, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
label=r'$\pm$ 1 std. dev.')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
# 绘制准确率折线图
mean_accuracy = np.mean(accuracy_list)
variance_accuracy = np.var(accuracy_list)
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(accuracy_list) + 1), accuracy_list, marker='o', linestyle='-', color='b', label='Accuracy per fold')
plt.axhline(y=mean_accuracy, color='r', linestyle='--')
plt.axhline(y=mean_accuracy + variance_accuracy, color='g', linestyle='--')
plt.axhline(y=mean_accuracy - variance_accuracy, color='g', linestyle='--')
# 标注数值
plt.text(len(accuracy_list) + 0.5, mean_accuracy, f'Mean: {mean_accuracy:.4f}', color='r', va='center')
plt.text(len(accuracy_list) + 0.5, mean_accuracy + variance_accuracy, f'Mean + Variance: {mean_accuracy + variance_accuracy:.4f}', color='g', va='center')
plt.text(len(accuracy_list) + 0.5, mean_accuracy - variance_accuracy, f'Mean - Variance: {mean_accuracy - variance_accuracy:.4f}', color='g', va='center')
plt.title('Accuracy per Fold with Mean and Variance')
plt.xlabel('Fold')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# 输出平均准确率
print(f'平均准确率: {mean_accuracy:.4f}')
/home/wagnchogn/anaconda3/envs/pytorch/bin/python /media/wagnchogn/data/lishuxin/_2_2403_sam/pre_code/second/1_SVM_ROC.py
Traceback (most recent call last):
File "/media/wagnchogn/data/lishuxin/_2_2403_sam/pre_code/second/1_SVM_ROC.py", line 66, in <module>
fpr, tpr, _ = roc_curve(y_test, y_prob)
File "/home/wagnchogn/anaconda3/envs/pytorch/lib/python3.8/site-packages/sklearn/utils/_param_validation.py", line 214, in wrapper
return func(*args, **kwargs)
File "/home/wagnchogn/anaconda3/envs/pytorch/lib/python3.8/site-packages/sklearn/metrics/_ranking.py", line 1095, in roc_curve
fps, tps, thresholds = _binary_clf_curve(
File "/home/wagnchogn/anaconda3/envs/pytorch/lib/python3.8/site-packages/sklearn/metrics/_ranking.py", line 821, in _binary_clf_curve
pos_label = _check_pos_label_consistency(pos_label, y_true)
File "/home/wagnchogn/anaconda3/envs/pytorch/lib/python3.8/site-packages/sklearn/utils/validation.py", line 2245, in _check_pos_label_consistency
raise ValueError(
ValueError: y_true takes value in {1, 2} and pos_label is not specified: either make y_true take value in {0, 1} or {-1, 1} or pass pos_label explicitly.
Process finished with exit code 1
# 导入必要的库
import numpy as np
import os
from PIL import Image
from sklearn.model_selection import KFold
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, roc_curve, auc
import matplotlib.pyplot as plt
# 加载图像数据
def load_image_data(image_dir, target_size=(256, 256)):
X = []
y = []
class_labels = os.listdir(image_dir)
for label in class_labels:
class_dir = os.path.join(image_dir, label)
if os.path.isdir(class_dir):
for file_name in os.listdir(class_dir):
file_path = os.path.join(class_dir, file_name)
if file_path.endswith(('.png', '.jpg', '.jpeg')):
image = Image.open(file_path)
image = image.resize(target_size)
image = np.array(image)
if image.shape == (256, 256, 3): # 只保留形状为(256, 256, 3)的图像
X.append(image)
y.append(int(label))
return np.array(X), np.array(y)
# 加载数据
image_dir = '/media/wagnchogn/data/01/second/eye-20240622/eye' # 替换为你的图像数据路径
X, y = load_image_data(image_dir)
# 重新编码标签为 {0, 1}
y = y - 1
# 数据预处理:将图像数据展开为一维向量,并标准化数据
X = X.reshape((X.shape[0], -1))
scaler = StandardScaler()
X = scaler.fit_transform(X)
# 定义SVM模型
model = SVC(kernel='rbf', C=0.1, probability=True)
# 定义五折交叉验证
kf = KFold(n_splits=5, shuffle=True, random_state=42)
accuracy_list = []
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
# 训练SVM模型
model.fit(X_train, y_train)
# 评估模型
y_pred = model.predict(X_test)
y_prob = model.predict_proba(X_test)[:, 1]
accuracy = accuracy_score(y_test, y_pred)
accuracy_list.append(accuracy)
# 计算ROC曲线
fpr, tpr, _ = roc_curve(y_test, y_prob)
roc_auc = auc(fpr, tpr)
aucs.append(roc_auc)
tprs.append(np.interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
# 绘制平均ROC曲线
plt.figure(figsize=(10, 8))
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Chance', alpha=.8)
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
lw=2, alpha=.8)
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic')
plt.legend(loc="lower right")
plt.show()
# 绘制准确率折线图
mean_accuracy = np.mean(accuracy_list)
variance_accuracy = np.var(accuracy_list)
std_deviation = np.sqrt(variance_accuracy)
plt.figure(figsize=(10, 5))
plt.plot(range(1, len(accuracy_list) + 1), accuracy_list, marker='o', linestyle='-', color='b', label='Accuracy per fold')
plt.axhline(y=mean_accuracy, color='r', linestyle='--')
# 标注数值
plt.text(len(accuracy_list) + 0.5, mean_accuracy, f'Mean: {mean_accuracy:.4f} ± {std_deviation:.4f}', color='r', va='center')
plt.title('Accuracy per Fold with Mean and Standard Deviation')
plt.xlabel('Fold')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# 输出平均准确率
print(f'平均准确率: {mean_accuracy:.4f}')