matmul:输入操作数1的核心尺寸0不匹配,具有gufunc签名(n?,k),(k,m?)->(n?,m?)(大小5与1不同)

时间:2020-09-24 23:02:59

标签: python pandas numpy scikit-learn regression

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline

df = pd.read_excel('enes.xlsx')
X=pd.DataFrame(df['hacim'])
Y=pd.DataFrame(df['delay'])
X

from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
lin_reg_2 = LinearRegression()
lin_reg_2.fit(X_poly, Y)

plt.figure(figsize=(30,20))
X_grid = np.arange(X.min()[0], X.max()[0], 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, Y, color = 'red')
plt.plot(X_grid, lin_reg_2.predict(poly_reg.fit_transform(X_grid)), color = 'blue')
plt.title('hacim vs delay)')
plt.xlabel('hacim')
plt.ylabel('delay')
plt.show()
print('R^2 for this regression is :' )
print(lin_reg_2.score(X, Y))




ValueError                                Traceback (most recent call last)
<ipython-input-29-1b714a9562f4> in <module>
      9 plt.show()
     10 print('R^2 for this regression is :' )
---> 11 print(lin_reg_2.score(X, Y))

C:\Anaconda3\lib\site-packages\sklearn\base.py in score(self, X, y, sample_weight)
    549 
    550         from .metrics import r2_score
--> 551         y_pred = self.predict(X)
    552         return r2_score(y, y_pred, sample_weight=sample_weight)
    553 

C:\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py in predict(self, X)
    234             Returns predicted values.
    235         """
--> 236         return self._decision_function(X)
    237 
    238     _preprocess_data = staticmethod(_preprocess_data)

C:\Anaconda3\lib\site-packages\sklearn\linear_model\_base.py in _decision_function(self, X)
    217 
    218         X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
--> 219         return safe_sparse_dot(X, self.coef_.T,
    220                                dense_output=True) + self.intercept_
    221 

C:\Anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs)
     71                           FutureWarning)
     72         kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
---> 73         return f(**kwargs)
     74     return inner_f
     75 

C:\Anaconda3\lib\site-packages\sklearn\utils\extmath.py in safe_sparse_dot(a, b, dense_output)
    151             ret = np.dot(a, b)
    152     else:
--> 153         ret = a @ b
    154 
    155     if (sparse.issparse(a) and sparse.issparse(b)

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 5 is different from 1)


执行最后一个块会绘制多项式回归模型并给出以下错误:matmul:输入操作数1的核心维0不匹配,具有gufunc签名(n?,k),(k,m?)->( n?,m?)(大小5与1不同) 删除有关模型得分的最后两行将消除错误。在这种情况下,我该如何解决这个问题,或者还有另一种方法来衡量回归模型的成功

0 个答案:

没有答案