pandas DataConversionWarning 在 Scikit 中拟合 RandomForestRegressor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29263099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataConversionWarning fitting RandomForestRegressor in Scikit
提问by sapo_cosmico
I'm trying to fit a RandomForestRegressor to my training set,
我正在尝试将 RandomForestRegressor 拟合到我的训练集,
rfr.fit(train_X , train_y)
but keep getting the following warning:
但不断收到以下警告:
/usr/local/lib/python2.7/dist-packages/IPython/kernel/main.py:1: 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(). if name== 'main':
/usr/local/lib/python2.7/dist-packages/IPython/kernel/ main.py:1: DataConversionWarning: 当需要一维数组时,传递了列向量 y。请将 y 的形状更改为 (n_samples, ),例如使用 ravel()。如果名称== ' main':
I am using Pandas, and therefore assumed that the training set might need to be in numpy arrays, so called .values:
我正在使用 Pandas,因此假设训练集可能需要在 numpy 数组中,即所谓的 .values:
train_y = train[label].values
train_X = train[features].values
Checking to see the type, and shape:
检查以查看类型和形状:
print type(train_X), train_X.shape
print type(train_y), train_y.shape
Returns:
返回:
<type 'numpy.ndarray'> (20457, 44)
<type 'numpy.ndarray'> (20457, 1)
Not really sure what to do next, only found this answerbut it wasn't much help.
不太确定接下来要做什么,只找到了这个答案,但没有太大帮助。
It does actually output a result, but I have no idea if it is the right one. With cross validation, it keeps creating that warning over and over again.
它确实输出了一个结果,但我不知道它是否正确。通过交叉验证,它会一遍又一遍地创建警告。
采纳答案by Andreas Mueller
The warning tells you exactly what to do, right? What is the question? If the results are correct despite the warning? Yes they are, because what you mean is using a 1d vector y.
警告会准确地告诉您该怎么做,对吗?问题是什么?尽管有警告,结果是否正确?是的,它们是,因为您的意思是使用一维向量 y。
How to get rid of the warning? If you meant y to be a 1d vector and not a column of a matrix, use y.ravel() as the warning says.
如何摆脱警告?如果您的意思是 y 是一维向量而不是矩阵的列,请使用 y.ravel() 作为警告说。
回答by user7219594
You can have a try like y.shape=y.shape[0], because you should make y.shapelike (20457,), not (20457,1)
你可以试一试 y.shape=y.shape[0],因为你应该y.shape喜欢(20457,),而不是(20457,1)
回答by madhur shalini
As the warning says, we wanna change the dimension from (N,1) to (N,). we can use numpy's ravel function.
正如警告所说,我们想将维度从 (N,1) 更改为 (N,)。我们可以使用 numpy 的 ravel 函数。
Use np.ravel(train_y) instead of train_y.
使用 np.ravel(train_y) 而不是 train_y。
you may Try train_y.reshape((-1,)) as well.
你也可以试试 train_y.reshape((-1,)) 。

