ValueError:找到样本数不一致的数组[6 1786]

2024-04-25 03:57:12 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码:

from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import KFold
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import datasets
import numpy as np

newsgroups = datasets.fetch_20newsgroups(
                subset='all',
                categories=['alt.atheism', 'sci.space']
         )
X = newsgroups.data
y = newsgroups.target

TD_IF = TfidfVectorizer()
y_scaled = TD_IF.fit_transform(newsgroups, y)
grid = {'C': np.power(10.0, np.arange(-5, 6))}
cv = KFold(y_scaled.size, n_folds=5, shuffle=True, random_state=241) 
clf = SVC(kernel='linear', random_state=241)

gs = GridSearchCV(estimator=clf, param_grid=grid, scoring='accuracy', cv=cv)
gs.fit(X, y_scaled) 

我犯了错误,我不明白为什么。回溯:

Traceback (most recent call last): File
"C:/Users/Roman/PycharmProjects/week_3/assignment_2.py", line 23, in

gs.fit(X, y_scaled) #TODO: check this line File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\grid_search.py",
line 804, in fit
return self._fit(X, y, ParameterGrid(self.param_grid)) File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\grid_search.py",
line 525, in _fit
X, y = indexable(X, y) File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\utils\validation.py",
line 201, in indexable
check_consistent_length(*result) File "C:\Users\Roman\AppData\Roaming\Python\Python35\site-packages\sklearn\utils\validation.py",
line 176, in check_consistent_length
"%s" % str(uniques))

ValueError: Found arrays with inconsistent numbers of samples: [ 6 1786]

有人能解释一下为什么会发生这种错误吗?在


Tags: infrompyimportlinesklearnroamingusers
0条回答
网友
1楼 · 发布于 2024-04-25 03:57:12

我想你对你的Xy有点困惑。您需要将X转换为tf-idf向量,并使用此向量对y进行训练。见下文

from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.cross_validation import KFold
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import datasets
import numpy as np

newsgroups = datasets.fetch_20newsgroups(
                subset='all',
                categories=['alt.atheism', 'sci.space']
         )
X = newsgroups.data
y = newsgroups.target

TD_IF = TfidfVectorizer()
X_scaled = TD_IF.fit_transform(X, y)
grid = {'C': np.power(10.0, np.arange(-1, 1))}
cv = KFold(y_scaled.size, n_folds=5, shuffle=True, random_state=241) 
clf = SVC(kernel='linear', random_state=241)

gs = GridSearchCV(estimator=clf, param_grid=grid, scoring='accuracy', cv=cv)
gs.fit(X_scaled, y)

相关问题 更多 >