AttributeError: 'float' object has no attribute 'shape' when using linregress


I want to use LinearRegression and linregress to caculate Intercept,X_Variable_1,R_Square,Significance_F just like regression analysis in Excel.

When I use this code to do it, there is no mistake.

from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
from scipy.stats import linregress
from decimal import *

def calculate_parameters():
    list_a=[['2018', '3', 'aa', 'aa', 93,1884.7746222667, 165.36153386251098], ['2018', '3', 'bb', 'bb', 62, 665.6392779848, 125.30386609565328], ['2018', '3', 'cc', 'cc', 89, 580.2259903521, 160.19280253775514]]
    df = pd.DataFrame(list_a)
    X = df.iloc[:, 5]
    y = df.iloc[:, 6]
    X1 = X.values.reshape(-1, 1)
    y1 = y.values.reshape(-1, 1)
    clf = LinearRegression()
    clf.fit(X1, y1)
    yhat = clf.predict(X1)
    para_Intercept = clf.intercept_[0]
    para_X_Variable_1 = clf.coef_[0][0]
    SS_Residual = sum((y1 - yhat) ** 2)
    SS_Total = sum((y1 - np.mean(y1)) ** 2)
    para_R_Square = 1 - (float(SS_Residual)) / SS_Total
    adjusted_r_squared = 1 - (1 - para_R_Square) * (len(y1) - 1) / (len(y1) - X1.shape[1] - 1)
    para_a = linregress(X, y)
    para_Significance_F = para_a[3]
    print("Intercept:"+str(para_Intercept))
    print("X_Variable_1:"+str(para_X_Variable_1))
    print("R_Square:" + str(para_R_Square[0]))
    print("Significance_F:" + str(para_Significance_F))

if __name__ == "__main__":
    calculate_parameters()

The output is:

Intercept:133.10871357512195

X_Variable_1:0.016460552337949654

R_Square:0.3039426453800934

Significance_F:0.6282563718649847

But in fact,list_a likes this:

list_a = [['2018', '3', 'aa', 'aa', 93, Decimal('1884.7746222667'), 165.36153386251098],
          ['2018', '3', 'bb', 'bb', 62, Decimal('665.6392779848'), 125.30386609565328],
          ['2018', '3', 'cc', 'cc', 89, Decimal('580.2259903521'), 160.19280253775514]]

The 6th column is decimal type.

When I change list_a,likes this:

from sklearn.linear_model import LinearRegression
import pandas as pd
import numpy as np
from scipy.stats import linregress
from decimal import *

def calculate_parameters():
    # list_a=[['2018', '3', 'aa', 'aa', 93,1884.7746222667, 165.36153386251098], ['2018', '3', 'bb', 'bb', 62, 665.6392779848, 125.30386609565328], ['2018', '3', 'cc', 'cc', 89, 580.2259903521, 160.19280253775514]]
    list_a=[['2018', '3', 'aa', 'aa', 93,Decimal('1884.7746222667'), 165.36153386251098], ['2018', '3', 'bb', 'bb', 62, Decimal('665.6392779848'), 125.30386609565328], ['2018', '3', 'cc', 'cc', 89, Decimal('580.2259903521'), 160.19280253775514]]
    df = pd.DataFrame(list_a)
    X = df.iloc[:, 5]
    y = df.iloc[:, 6]
    X1 = X.values.reshape(-1, 1)
    y1 = y.values.reshape(-1, 1)
    clf = LinearRegression()
    clf.fit(X1, y1)
    yhat = clf.predict(X1)
    para_Intercept = clf.intercept_[0]
    para_X_Variable_1 = clf.coef_[0][0]
    SS_Residual = sum((y1 - yhat) ** 2)
    SS_Total = sum((y1 - np.mean(y1)) ** 2)
    para_R_Square = 1 - (float(SS_Residual)) / SS_Total
    adjusted_r_squared = 1 - (1 - para_R_Square) * (len(y1) - 1) / (len(y1) - X1.shape[1] - 1)
    para_a = linregress(X, y)
    para_Significance_F = para_a[3]
    print("Intercept:"+str(para_Intercept))
    print("X_Variable_1:"+str(para_X_Variable_1))
    print("R_Square:" + str(para_R_Square[0]))
    print("Significance_F:" + str(para_Significance_F))

if __name__ == "__main__":
    calculate_parameters()

The error is:

Traceback (most recent call last):

File "E:/test_opencv/MyTest.py", line 32, in calculate_parameters()

File "E:/test_opencv/MyTest.py", line 24, in calculate_parameters para_a = linregress(X, y)

File "E:\Anaconda3\lib\site-packages\scipy\stats_stats_mstats_common.py", line 79, in linregress ssxm, ssxym, ssyxm, ssym = np.cov(x, y, bias=1).flat

File "E:\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 3085, in cov avg, w_sum = average(X, axis=1, weights=w, returned=True)

File "E:\Anaconda3\lib\site-packages\numpy\lib\function_base.py", line 1163, in average if scl.shape != avg.shape:

AttributeError: 'float' object has no attribute 'shape'

How to fix the error?


Answer

You can achieve this by simply casting X to float:

para_a = linregress(X.astype(float), y)
>>> para_a
LinregressResult(slope=0.016460552337949654, intercept=133.10871357512195, rvalue=0.5513099358619372, pvalue=0.6282563718649847, stderr=0.024909849163985552)