Johnnyjohnny2001 2021-08-29 06:29 采纳率: 71.4%
浏览 45
已结题

SVC分类后为什么结果只有一个

我想对比PCA降维前后模型拟合时间和得分,结果不进行降维运算后分类的结果只有一个,不知道是哪里出了问题,还是确实只能得到一个结果。(想象中应该是将test集图片分类成各个人的,但是结果出来以后都是小布什)

               precision    recall  f1-score   support

 Ariel Sharon       0.00      0.00      0.00        13
 Colin Powell       0.00      0.00      0.00        60
 Donald Rumsfeld    0.00      0.00      0.00        27
George W Bush       0.45      1.00      0.62       146
Gerhard Schroeder   0.00      0.00      0.00        25
  Hugo Chavez       0.00      0.00      0.00        15
   Tony Blair       0.00      0.00      0.00        36

     accuracy                           0.45       322
    macro avg       0.06      0.14      0.09       322
 weighted avg       0.21      0.45      0.28       322

[[ 0 0 0 13 0 0 0]
[ 0 0 0 60 0 0 0]
[ 0 0 0 27 0 0 0]
[ 0 0 0 146 0 0 0]
[ 0 0 0 25 0 0 0]
[ 0 0 0 15 0 0 0]
[ 0 0 0 36 0 0 0]]

from time import time
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import fetch_lfw_people
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.decomposition import PCA
from sklearn.svm import SVC

lfw_people = fetch_lfw_people(min_faces_per_person=70, resize=0.4)
n_samples, h, w = lfw_people.images.shape
X = lfw_people.data
n_features = X.shape[1]
y = lfw_people.target
target_names = lfw_people.target_names
n_classes = target_names.shape[0]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)

t0 = time()
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
clf.fit(X_train, y_train)
print("done in %0.3fs" % (time() - t0))

y_pred=clf.predict(X_test)

print(classification_report(y_test, y_pred, target_names=target_names))
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))
print(classification_report(y_test, y_pred, target_names=target_names))
print(confusion_matrix(y_test, y_pred, labels=range(n_classes)))

  • 写回答

2条回答 默认 最新

  • CSDN专家-kaily 2021-08-30 16:32
    关注

    一共是7个人的图片是超过了70张,你打印一下这些人的图片数量:

    target = pd.DataFrame(Y)
    print(target.value_counts())
    

    输出结果为:
    3 530
    1 236
    6 144
    2 121
    4 109
    0 77
    5 71
    dtype: int64
    这说明这是一共数据分布不均匀的数据集,总样本数量为1288,用530/1288=0.41,你最终的accuracy为0.45,说明你的模型将所有的图片都划分为了George W Bush
    或者是你看

    img
    只有George W Bush的召回率不等于0,且为1,而其余的均为0,这也说明了模型只是简单的将所有的图片都分为了数量最多的George W Bush
    如果不进行降维的话,就是一个像素一个特征,就是50*37=1850,样本数才1288,特征数位1850,这是机器学习,不是深度学习啊,这里必须进行降维

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月8日
  • 已采纳回答 8月31日
  • 创建了问题 8月29日

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波