ValueError: Expected 2D array, got 1D array instead:
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ValueError: Expected 2D array, got 1D array instead:相关的知识,希望对你有一定的参考价值。
ValueError: Expected 2D array, got 1D array instead:
目录
ValueError: Expected 2D array, got 1D array instead:
问题:
构建简单线性回归模型,特征只有一个的情况。使用一维numpy作为输入。
#
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([1,2,3,4,5])
y = X*2+3
reg = LinearRegression().fit(X, y)
reg.score(X, y)
reg.coef_
reg.intercept_
解决:
使用reshape函数将一维转化为二维,OK了
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.array([1,2,3,4,5])
y = X*2+3
reg = LinearRegression().fit(X.reshape(-1, 1), y)
reg.score(X.reshape(-1, 1), y)
reg.coef_
reg.intercept_
完整错误:
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-70c2c07f9344> in <module> 3 X = np.array([1,2,3,4,5]) 4 y = X*2+3 ----> 5 reg = LinearRegression().fit(X, y) 6 reg.score(X, y) 7 D:\\anaconda\\lib\\site-packages\\sklearn\\linear_model\\_base.py in fit(self, X, y, sample_weight) 517 518 X, y = self._validate_data(X, y, accept_sparse=accept_sparse, --> 519 y_numeric=True, multi_output=True) 520 521 if sample_weight is not None: D:\\anaconda\\lib\\site-packages\\sklearn\\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params) 431 y = check_array(y, **check_y_params) 432 else: --> 433 X, y = check_X_y(X, y, **check_params) 434 out = X, y 435 D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 if extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0 D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator) 876 ensure_min_samples=ensure_min_samples, 877 ensure_min_features=ensure_min_features, --> 878 estimator=estimator) 879 if multi_output: 880 y = check_array(y, accept_sparse='csr', force_all_finite=True, D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 if extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0 D:\\anaconda\\lib\\site-packages\\sklearn\\utils\\validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator) 696 "Reshape your data either using array.reshape(-1, 1) if " 697 "your data has a single feature or array.reshape(1, -1) " --> 698 "if it contains a single sample.".format(array)) 699 700 # make sure we actually converted to numeric: ValueError: Expected 2D array, got 1D array instead: array=[1 2 3 4 5]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
以上是关于ValueError: Expected 2D array, got 1D array instead:的主要内容,如果未能解决你的问题,请参考以下文章
ValueError: Expected 2D array, got 1D array instead:
ValueError: Expected 2D array, got 1D array instead:
如何在 sklearn/python 中修复“ValueError: Expected 2D array, got 1D array”?
ValueError: Expected 2D array, got 1D array instead , euclidean distance
sklearn中报错ValueError: Expected 2D array, got 1D array instead:
添加层到 resnet[keras] 的顶部:ValueError: Input 0 is incompatible with layer conv2d_transpose_1: expected n