SVM ValueError: y should be a 1d array, got an array of shape (1, 250) instead. Found input variable

阅读前请看一下:我是一个热衷于记录的人,每次写博客会反复研读,尽量不断提升博客质量。文章设置为仅粉丝可见,是因为写博客确实花了不少精力。希望互相进步谢谢!!

文章目录

1、问题描述:

背景:机器学习时关于SVM的学习,之前线性回归、逻辑回归切分数据集时均是将X切分为(输入特征维数,样本数),例如(5,250)代表输入特征是5维,样本数是250个。那是因为之前手敲线性、逻辑回归时用到的数学公式要求维度是这样。但是如果直接使用sklearn库里的现成函数,需要的恰恰相反,即需要(样本数,输入特征维数)这样的格式作为参数的输入。否则报错。

函数:svm.SVC中的 fit() 函数

bug:

  • ValueError: y should be a 1d array, got an array of shape (1, 250) instead.

  • ValueError: Found input variables with inconsistent numbers of samples: [5, 250].

  • ValueError: X has 250 features, but SVC is expecting 5 features as input.

  • DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().

code:


res = svm.SVC(C=svm_C, kernel=svm_kernel)
res.fit(train_set_X, train_set_y)
train_predict_y = res.predict(train_set_X)
test_predict_y = res.predict(test_set_X)

2、分析与解决办法:

1、首先运行上方原代码,会报错:

ValueError: y should be a 1d array, got an array of shape (1, 250) instead。

查看变量列表如下图

SVM ValueError: y should be a 1d array, got an array of shape (1, 250) instead. Found input variable

可以看到train_set_y的shape是(1,250),而这里期望的shape是(250,)。

所以解决办法:

二维转一维即可,我习惯用reshape(-1),当然还有flatten()、ravel()、squeeze()函数都能实现。


res = svm.SVC(C=svm_C, kernel=svm_kernel)
res.fit(train_set_X, train_set_y.reshape(-1))

train_predict_y = res.predict(train_set_X)
test_predict_y = res.predict(test_set_X)

2、但运行上述代码又报错:

ValueError: Found input variables with inconsistent numbers of samples: [5, 250]

意思是”样本数和输入变量不一致”。想不通,就去查看源码的说明,如下:

SVM ValueError: y should be a 1d array, got an array of shape (1, 250) instead. Found input variable

可以看到上图,当我们将train_set_y由 (1,250) 变为 (250,) 并将其作为y传入时,250会被视作样本个数,自己也确实是250个样本的标注,这一点符合。

但是请注意,这里train_set_X的shape是(5,250),按照上图注释的意思,5会被视作样本数,250被实作输入特征的维数,而后面train_set_y是被视作有250个样本,故自然报错不匹配。

所以解决办法:

1、使用.T属性,将train_set_X的shape由(5,250)转为(250,5);
2、既然train_set_X使用了.T属性,为了好看,train_set_y也先使用.T属性有(1,250)转为(250,1),再使用.reshape(-1)即可;


res = svm.SVC(C=svm_C, kernel=svm_kernel)
res.fit(train_set_X.T, train_set_y.T.reshape(-1))

train_predict_y = res.predict(train_set_X)
test_predict_y = res.predict(test_set_X)

其实吧,不用这么麻烦,直接最初切分数据集时变成需要的不就行了?不然之后用一次还要.T转一次。

2、但运行上述代码还报错:

    • -> 67 train_predict_y = res.predict(train_set_X) #训练集上的预测
      ValueError: X has 250 features, but SVC is expecting 5 features as input.

那这里的分析就不难了,再使用.T就行了。故修正code如下:


res = svm.SVC(C=svm_C, kernel=svm_kernel)
res.fit(train_set_X.T, train_set_y.T.reshape(-1))

train_predict_y = res.predict(train_set_X.T)
test_predict_y = res.predict(test_set_X.T)

3、DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().

自己后来看老师标准代码时,老师传入的X是(250,5),y是(250,1)。自己产生了疑惑,定义里不是说了y应该为(250,)。于是自己尝试按照老师的运行了下,发现给了个警告。所以虽然不用管也能运行,但还是按照规范吧。

解决方法:

见之前写的文章《DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the》

码字不易,谢谢点赞!!!
码字不易,谢谢点赞!!!
码字不易,谢谢点赞!!!

Original: https://blog.csdn.net/qq_40967086/article/details/127437453
Author: 一只菜得不行的鸟
Title: SVM ValueError: y should be a 1d array, got an array of shape (1, 250) instead. Found input variable

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/723115/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球