Python 获取没有匹配指定签名和转换错误的循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47838306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:24:04  来源:igfitidea点击:

Getting No loop matching the specified signature and casting error

pythonnumpymachine-learningscikit-learn

提问by Shane Ekanayake

I'm a beginner to python and machine learning . I get below error when i try to fit data into statsmodels.formula.api OLS.fit()

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

Traceback (most recent call last):

回溯(最近一次调用最后一次):

File "", line 47, in regressor_OLS = sm.OLS(y , X_opt).fit()

File "E:\Anaconda\lib\site-packages\statsmodels\regression\linear_model.py", line 190, in fit self.pinv_wexog, singular_values = pinv_extended(self.wexog)

File "E:\Anaconda\lib\site-packages\statsmodels\tools\tools.py", line 342, in pinv_extended u, s, vt = np.linalg.svd(X, 0)

File "E:\Anaconda\lib\site-packages\numpy\linalg\linalg.py", line 1404, in svd u, s, vt = gufunc(a, signature=signature, extobj=extobj)

TypeError: No loop matching the specified signature and casting was found for ufunc svd_n_s

文件“”,第 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)

类型错误:未找到与 ufunc svd_n_s 匹配的指定签名和转换的循环

code

代码

#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

数据集

dataset link

数据集链接

Couldn't find anything helpful to solve this issue on stack-overflow or google .

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

回答by Victor Sejas

try specifiying the

尝试指定

dtype = 'float'

dtype = '浮动'

When the matrix is created. Example:

创建矩阵时。例子:

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

Hope this works!

希望这有效!

回答by Muke888

As suggested previously, you need to ensure X_opt is a float type. For example in your code, it would look like this:

如前所述,您需要确保 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()