• 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作为输入。

    1. #
    2. import numpy as np
    3. from sklearn.linear_model import LinearRegression
    4. X = np.array([1,2,3,4,5])
    5. y = X*2+3
    6. reg = LinearRegression().fit(X, y)
    7. reg.score(X, y)
    8. reg.coef_
    9. reg.intercept_

    解决:

    使用reshape函数将一维转化为二维,OK了

    1. import numpy as np
    2. from sklearn.linear_model import LinearRegression
    3. X = np.array([1,2,3,4,5])
    4. y = X*2+3
    5. reg = LinearRegression().fit(X.reshape(-1, 1), y)
    6. reg.score(X.reshape(-1, 1), y)
    7. reg.coef_
    8. reg.intercept_

    完整错误:

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
     in 
          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.
    

  • 相关阅读:
    24.Semaphore的作用和原理
    外贸网站的SEO优化该怎么做?
    [算法刷题笔记]哈希表(1)
    适用于 Oracle® 电子商务套件的 ECM(企业内容管理)软件
    如何远程通过内网穿透实现微信公众号在本地的完整调试
    Linux服务器如何设置定时器
    Win10打开软件提示comdlg32.ocx文件丢失?
    DevOps持续集成-Jenkins(3)
    MSYS2下搭建Qt开发环境
    WebRTC清晰度和流畅度
  • 原文地址:https://blog.csdn.net/zhongkeyuanchongqing/article/details/126260659