当期望一维数组时,传递了列向量y

问题:当期望一维数组时,传递了列向量y

我需要适应RandomForestRegressorsklearn.ensemble

forest = ensemble.RandomForestRegressor(**RF_tuned_parameters)
model = forest.fit(train_fold, train_y)
yhat = model.predict(test_fold)

该代码始终有效,直到我对数据进行了一些预处理(train_y)。错误消息显示:

DataConversionWarning:当期望1d数组时,传递了列向量y。请将y的形状更改为(n_samples,),例如使用ravel()。

模型= forest.fit(train_fold,train_y)

以前train_y是一个Series,现在是numpy数组(它是列向量)。如果我应用train_y.ravel(),则它成为行向量,并且没有错误消息出现,通过预测步骤需要很长时间(实际上,它永远不会完成…)。

RandomForestRegressor我发现的文档中,train_y应将其定义为“ y : array-like, shape = [n_samples] or [n_samples, n_outputs] 如何解决此问题的想法?”。

I need to fit RandomForestRegressor from sklearn.ensemble.

forest = ensemble.RandomForestRegressor(**RF_tuned_parameters)
model = forest.fit(train_fold, train_y)
yhat = model.predict(test_fold)

This code always worked until I made some preprocessing of data (train_y). The error message says:

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().

model = forest.fit(train_fold, train_y)

Previously train_y was a Series, now it’s numpy array (it is a column-vector). If I apply train_y.ravel(), then it becomes a row vector and no error message appears, through the prediction step takes very long time (actually it never finishes…).

In the docs of RandomForestRegressor I found that train_y should be defined as y : array-like, shape = [n_samples] or [n_samples, n_outputs] Any idea how to solve this issue?


回答 0

更改此行:

model = forest.fit(train_fold, train_y)

至:

model = forest.fit(train_fold, train_y.values.ravel())

编辑:

.values将给出数组中的值。(形状:[n,1)

.ravel 会将数组形状转换为(n,)

Change this line:

model = forest.fit(train_fold, train_y)

to:

model = forest.fit(train_fold, train_y.values.ravel())

Edit:

.values will give the values in an array. (shape: (n,1)

.ravel will convert that array shape to (n, )


回答 1

当我尝试训练KNN分类器时,我也遇到了这种情况。但似乎在警告不见了,我改变之后:
knn.fit(X_train,y_train)

knn.fit(X_train, np.ravel(y_train,order='C'))

在这行之前,我用过import numpy as np

I also encountered this situation when I was trying to train a KNN classifier. but it seems that the warning was gone after I changed:
knn.fit(X_train,y_train)
to
knn.fit(X_train, np.ravel(y_train,order='C'))

Ahead of this line I used import numpy as np.


回答 2

我有同样的问题。问题在于标签是按列格式的,而它却希望连续显示。用np.ravel()

knn.score(training_set, np.ravel(training_labels))

希望这能解决。

I had the same problem. The problem was that the labels were in a column format while it expected it in a row. use np.ravel()

knn.score(training_set, np.ravel(training_labels))

Hope this solves it.


回答 3

使用以下代码:

model = forest.fit(train_fold, train_y.ravel())

如果仍然像下面一样错误地打耳光?

Unknown label type: %r" % y

使用此代码:

y = train_y.ravel()
train_y = np.array(y).astype(int)
model = forest.fit(train_fold, train_y)

use below code:

model = forest.fit(train_fold, train_y.ravel())

if you are still getting slap by error as identical as below ?

Unknown label type: %r" % y

use this code:

y = train_y.ravel()
train_y = np.array(y).astype(int)
model = forest.fit(train_fold, train_y)

回答 4

另一种方法是使用 ravel

model = forest.fit(train_fold, train_y.values.reshape(-1,))

Another way of doing this is to use ravel

model = forest.fit(train_fold, train_y.values.reshape(-1,))

回答 5

使用neuraxle,您可以轻松解决此问题:

p = Pipeline([
   # expected outputs shape: (n, 1)
   OutputTransformerWrapper(NumpyRavel()), 
   # expected outputs shape: (n, )
   RandomForestRegressor(**RF_tuned_parameters)
])

p, outputs = p.fit_transform(data_inputs, expected_outputs)

Neuraxle是类似于sklearn的框架,用于深度学习项目中的超参数调整和AutoML!

With neuraxle, you can easily solve this :

p = Pipeline([
   # expected outputs shape: (n, 1)
   OutputTransformerWrapper(NumpyRavel()), 
   # expected outputs shape: (n, )
   RandomForestRegressor(**RF_tuned_parameters)
])

p, outputs = p.fit_transform(data_inputs, expected_outputs)

Neuraxle is a sklearn-like framework for hyperparameter tuning and AutoML in deep learning projects !


回答 6

format_train_y=[]
for n in train_y:
    format_train_y.append(n[0])
format_train_y=[]
for n in train_y:
    format_train_y.append(n[0])

回答 7

Y = y.values [:,0]

Y-formated_train_y

y-train_y

Y = y.values[:,0]

Y – formated_train_y

y – train_y