python中的一维数组

时间:2018-09-01 08:10:50

标签: python arrays dimensions

尝试运行简单的线性回归模型来预测数据中给出的Highway-mpg的汽车价格。当我将它们放入线性模型函数中时,python表示其得到一维答案,而预期为二维。我该如何解决。我无法获得python错误中给出的(-1,1)和(1,-1)的概念。

    ValueError                                Traceback (most recent call last)
<ipython-input-44-f4055173756b> in <module>()
----> 1 lm.fit(x,y)

~\Anaconda3\lib\site-packages\sklearn\linear_model\base.py in fit(self, X, y, sample_weight)
    480         n_jobs_ = self.n_jobs
    481         X, y = check_X_y(X, y, accept_sparse=['csr', 'csc', 'coo'],
--> 482                          y_numeric=True, multi_output=True)
    483 
    484         if sample_weight is not None and np.atleast_1d(sample_weight).ndim > 1:

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    571     X = check_array(X, accept_sparse, dtype, order, copy, force_all_finite,
    572                     ensure_2d, allow_nd, ensure_min_samples,
--> 573                     ensure_min_features, warn_on_dtype, estimator)
    574     if multi_output:
    575         y = check_array(y, 'csr', force_all_finite=True, ensure_2d=False,

~\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    439                     "Reshape your data either using array.reshape(-1, 1) if "
    440                     "your data has a single feature or array.reshape(1, -1) "
--> 441                     "if it contains a single sample.".format(array))
    442             array = np.atleast_2d(array)
    443             # To ensure that array flags are maintained

ValueError: Expected 2D array, got 1D array instead:
array=[27 27 26 30 22 25 25 25 20 22 29 29 28 28 25 22 22 20 53 43 43 41 38 30
 38 38 38 30 30 24 54 38 42 34 34 34 34 33 33 33 33 28 31 29 43 43 29 19
 19 17 31 38 38 38 38 23 23 23 23 32 32 32 32 42 32 27 39 25 25 25 25 18
 18 16 16 24 41 38 38 30 30 32 24 24 24 32 32 30 30 37 50 37 37 37 37 37
 37 37 37 34 34 22 22 25 25 23 25 24 33 24 25 24 33 24 25 24 33 24 41 30
 38 38 38 30 24 27 25 25 25 28 31 31 28 28 28 28 26 26 36 31 31 37 33 32
 25 29 32 31 29 23 39 38 38 37 32 32 37 37 36 47 47 34 34 34 34 29 29 30
 30 30 30 30 30 34 33 32 32 32 24 24 24 24 46 34 46 34 34 42 32 29 29 24
 38 31 28 28 28 28 22 22 28 25 23 27 25].
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.

1 个答案:

答案 0 :(得分:0)

Scikit学习希望输入具有形状(n_samples,n_features)。就列表而言,这将是[[x_11, ..., x_1N], ..., [x_M1, ..., xMN]],其中M = n_samplesN = n_features 如果放入一维数组(或只是一个列表),则不知道您有1个具有n_features的样本还是n_samples具有1个特征的样本。因此,如果您使用

来转换列表/数组x
x_2d = np.asarray(x).reshape(-1, 1)

它将把形状数组(n_samples,)转换为(n_samples,1)。整形中的(-1,1)基本上说最后一个轴的大小应为1,而第一个轴的大小应不改变数组的总大小。