下多项式学习这一步报错

zertaul

2 回复4 浏览

来自课程:人工智能 - 机器学习进阶 - 线性回归

polyModel.set_params(Poly__degree=3)
# 带入数据 学习
polyModel.fit(X,Y)

这段代码运行后出现错误,错误提示好像是说不存在的方法,是不是新版本被替换掉了

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\ADMINI~1\AppData\Local\Temp/ipykernel_16728/722939405.py in <module>
      3 polyModel.set_params(Poly__degree=3)
      4 # 带入数据 学习
----> 5 polyModel.fit(X,Y)

D:\ProgramData\Anaconda3\lib\site-packages\sklearn\pipeline.py in fit(self, X, y, **fit_params)
    339         """
    340         fit_params_steps = self._check_fit_params(**fit_params)
--> 341         Xt = self._fit(X, y, **fit_params_steps)
    342         with _print_elapsed_time('Pipeline',
    343                                  self._log_message(len(self.steps) - 1)):

D:\ProgramData\Anaconda3\lib\site-packages\sklearn\pipeline.py in _fit(self, X, y, **fit_params_steps)
    301                 cloned_transformer = clone(transformer)
    302             # Fit or load from cache the current transformer
--> 303             X, fitted_transformer = fit_transform_one_cached(
    304                 cloned_transformer, X, y, None,
    305                 message_clsname='Pipeline',

D:\ProgramData\Anaconda3\lib\site-packages\joblib\memory.py in __call__(self, *args, **kwargs)
    347 
    348     def __call__(self, *args, **kwargs):
--> 349         return self.func(*args, **kwargs)
    350 
    351     def call_and_shelve(self, *args, **kwargs):

D:\ProgramData\Anaconda3\lib\site-packages\sklearn\pipeline.py in _fit_transform_one(transformer, X, y, weight, message_clsname, message, **fit_params)
    752     with _print_elapsed_time(message_clsname, message):
    753         if hasattr(transformer, 'fit_transform'):
--> 754             res = transformer.fit_transform(X, y, **fit_params)
    755         else:
    756             res = transformer.fit(X, y, **fit_params).transform(X)

D:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py in fit_transform(self, X, y, **fit_params)
    700         else:
    701             # fit method of arity 2 (supervised transformation)
--> 702             return self.fit(X, y, **fit_params).transform(X)
    703 
    704 

D:\ProgramData\Anaconda3\lib\site-packages\sklearn\preprocessing\_data.py in fit(self, X, y)
   1702             Fitted transformer.
   1703         """
-> 1704         n_samples, n_features = self._validate_data(
   1705             X, accept_sparse=True).shape
   1706         combinations = self._combinations(n_features, self.degree,

D:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    419             out = X
    420         elif isinstance(y, str) and y == 'no_validation':
--> 421             X = check_array(X, **check_params)
    422             out = X
    423         else:

D:\ProgramData\Anaconda3\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:\ProgramData\Anaconda3\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)
    692             # If input is 1D raise error
    693             if array.ndim == 1:
--> 694                 raise ValueError(
    695                     "Expected 2D array, got 1D array instead:\narray={}.\n"
    696                     "Reshape your data either using array.reshape(-1, 1) if "

ValueError: Expected 2D array, got 1D array instead:
array=[-0.94153096  0.13539865  1.61793307  1.42073262  2.55422232  2.56356284
 -0.05056873  3.56327884  4.1048943   1.649225    2.03326123  2.82074851
  2.90819082  3.23658082  5.46515347  6.24401084  5.03502684  5.56534057
  7.34693255  4.18677621].
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.

这个是错误提示信息

全部回复(2)

  • 老师_Trigger 1 楼

    可以参考一下:

    这个错误提示表明在使用fit方法时,期望的输入是一个2D数组(矩阵),但实际上提供的是一个1D数组。解决这个问题的方法是使用reshape方法将数据变为2D数组。

    你可以按照错误提示中的建议,使用reshape(-1, 1)将输入数据变为列向量。具体来说,你的代码应该像这样:

    
     

    pythonCopy code

    polyModel.set_params(Poly__degree=3) # 带入数据 学习 polyModel.fit(X.reshape(-1, 1), Y.reshape(-1, 1))

    这里假设X和Y是你的输入数据。确保使用reshape(-1, 1)将它们转换为列向量,以满足fit方法的输入要求。

  • zertaul 楼主 2 楼

    运行没有出错了,谢谢老师

发表回复

登录后可以回复