【问题标题】:Getting No loop matching the specified signature and casting error获取没有匹配指定签名和转换错误的循环
【发布时间】:2018-05-30 00:22:48
【问题描述】:

我是 python 和机器学习的初学者。当我尝试将数据拟合到 statsmodels.formula.api OLS.fit() 时出现以下错误

Traceback(最近一次调用最后一次):

文件“”,第 47 行,在 regressor_OLS = sm.OLS(y , X_opt).fit()

文件 "E:\Anaconda\lib\site-packages\statsmodels\regression\linear_model.py", 第 190 行,合适 self.pinv_wexog,singular_values = pinv_extended(self.wexog)

文件 "E:\Anaconda\lib\site-packages\statsmodels\tools\tools.py", 第 342 行,在 pinv_extended u, s, vt = np.linalg.svd(X, 0)

文件“E:\Anaconda\lib\site-packages\numpy\linalg\linalg.py”,行 1404, 在 svd u, s, vt = gufunc(a, signature=signature, extobj=extobj)

TypeError: 没有循环匹配指定的签名和强制转换是 找到 ufunc svd_n_s

代码

#Importing Libraries
import numpy as np # linear algebra
import pandas as pd # data processing
import matplotlib.pyplot as plt #Visualization


#Importing the dataset
dataset = pd.read_csv('Video_Games_Sales_as_at_22_Dec_2016.csv')
#dataset.head(10) 

#Encoding categorical data using panda get_dummies function . Easier and straight forward than OneHotEncoder in sklearn
#dataset = pd.get_dummies(data = dataset , columns=['Platform' , 'Genre' , 'Rating' ] , drop_first = True ) #drop_first use to fix dummy varible trap 


dataset=dataset.replace('tbd',np.nan)

#Separating Independent & Dependant Varibles
#X = pd.concat([dataset.iloc[:,[11,13]], dataset.iloc[:,13: ]] , axis=1).values  #Getting important  variables
X = dataset.iloc[:,[10,12]].values
y = dataset.iloc[:,9].values #Dependant Varible (Global sales)


#Taking care of missing data
from sklearn.preprocessing import Imputer
imputer =  Imputer(missing_values = 'NaN' , strategy = 'mean' , axis = 0)
imputer = imputer.fit(X[:,0:2])
X[:,0:2] = imputer.transform(X[:,0:2])


#Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.2 , random_state = 0)

#Fitting Mutiple Linear Regression to the Training Set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train,y_train)

#Predicting the Test set Result
y_pred = regressor.predict(X_test)


#Building the optimal model using Backward Elimination (p=0.050)
import statsmodels.formula.api as sm
X = np.append(arr = np.ones((16719,1)).astype(float) , values = X , axis = 1)

X_opt = X[:, [0,1,2]]
regressor_OLS = sm.OLS(y , X_opt).fit()
regressor_OLS.summary() 

数据集

dataset link

在 stack-overflow 或 google 上找不到任何有助于解决此问题的方法。

【问题讨论】:

  • 看看你的X_opt。这应该是一个只有数值的 DataFrame,最好是 float64。回溯意味着很可能其中一种 dtype 是错误的。 X_opt.dtypes 或 X_opt.dtype 如果它是一个 numpy ndarray。

标签: python numpy machine-learning scikit-learn


【解决方案1】:

尝试指定

dtype = 'float'

创建矩阵时。 示例:

a=np.matrix([[1,2],[3,4]], dtype='float')

希望这行得通!

【讨论】:

  • 我喜欢我可以快速来到 Stackoverflow,并且大多数时候可以快速解决困扰我很长时间的问题...
【解决方案2】:

面临类似的问题。解决了我提到 dtype 并展平数组的问题。

numpy 版本:1.17.3

a = np.array(a, dtype=np.float)
a = a.flatten()

【讨论】:

  • flatten 为我工作,没有 dtype 演员。谢谢。你能解释一下你为什么建议flatten 以及它在做什么。
【解决方案3】:

如前所述,您需要确保 X_opt 是浮点类型。 例如,在您的代码中,它看起来像这样:

X_opt = X[:, [0,1,2]]
X_opt = X_opt.astype(float)
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
regressor_OLS.summary()

【讨论】:

    【解决方案4】:

    遇到类似问题,我用df.values[]

    y = df.values[:, 4]
    

    使用df.iloc[].values 函数修复了该问题。

    y = dataset.iloc[:, 4].values
    

    df.values[]函数返回对象数据类型

    array([192261.83, 191792.06, 191050.39, 182901.99, 166187.94, 156991.12,
       156122.51, 155752.6, 152211.77, 149759.96, 146121.95, 144259.4,
       141585.52, 134307.35, 132602.65, 129917.04, 126992.93, 125370.37,
       124266.9, 122776.86, 118474.03, 111313.02, 110352.25, 108733.99,
       108552.04, 107404.34, 105733.54, 105008.31, 103282.38, 101004.64,
       99937.59, 97483.56, 97427.84, 96778.92, 96712.8, 96479.51,
       90708.19, 89949.14, 81229.06, 81005.76, 78239.91, 77798.83,
       71498.49, 69758.98, 65200.33, 64926.08, 49490.75, 42559.73,
       35673.41, 14681.4], dtype=object)
    

    但是

    df.iloc[:, 4].values returns floats array
    

    这是什么

    regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
    

    OLS() 有趣accepts

    或

    您可以在将 y 插入有趣的 OLS() 之前更改其数据类型

    y = np.array(y, dtype = float)
    

    【讨论】:

      【解决方案5】:

      从 NumPy 1.18.4 降级到 1.15.2 对我有用: pip install --upgrade numpy==1.15.2

      【讨论】:

        猜你喜欢
        • 2021-11-16
        • 2020-09-06
        • 2018-05-26
        • 1970-01-01
        • 2020-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-20
        相关资源
        最近更新 更多