解决DataConversionWarning: 'A column-vector y was passed when a 1d array was expected.' 错误
这个警告是因为在使用某些机器学习算法时,输入的标签数据y被认为是列向量,但实际上是一维数组。因此,需要使用ravel()函数将y转换为一维数组,或在传递y时将其转换为列向量。可以按照以下方式解决该问题:
- 使用ravel()将y转换为一维数组:
from sklearn import svm
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=100, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)
clf = svm.SVC(kernel='linear', C=1)
clf.fit(X, y.ravel()) # 注意使用ravel()将y转换为一维数组
- 将y转换为列向量:
from sklearn import svm
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=100, n_features=4,
n_informative=2, n_redundant=0,
random_state=0, shuffle=False)
clf = svm.SVC(kernel='linear', C=1)
clf.fit(X, np.reshape(y, (-1, 1))) # 将y转换为列向量
原文地址: https://www.cveoy.top/t/topic/mXEv 著作权归作者所有。请勿转载和采集!