Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dsTypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' #15188

Closed
moranavni opened this issue Dec 27, 2019 · 8 comments

Comments

@moranavni
Copy link

I am running a program on Python and I try to generate statistics outputs from an array.
The code line:
regressor_OLS = sm.OLS(y,X_opt).fit()
is given an elaborate error.

This is the code

Multiple Linear Regression

Importing Libraries

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

#importing the dataset
dataset = pd.read_csv('50_Startups.csv')

#Getting the independent variables
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,4].values
print (dataset)

Encoding categorical data

Encoding the Independent Variable

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([("Country", OneHotEncoder(), [3])], remainder = 'passthrough')
X = ct.fit_transform(X)

Avoiding the Dummy Variable Trap

X = X[:, 1:]

Splitting the dataset into the Training set and Test set

from sklearn.model_selection 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 Multiple Linear Regression Model to the Training Set

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

Predicting the Test set results

y_pred = regressor.predict(X_test)

#Building the Optimal Model using Backward Elimination
import statsmodels.api as sm

#Add columns of 1
X= np.append(arr = np.ones((50,1)).astype(int), values = X, axis =1)
X_opt = X[:,[0,1,2,3,4,5]]

#Multiple Linear Regression Model --- OLS
regressor_OLS = sm.OLS(y,X_opt).fit()
regressor_OLS.summary()

Reproducing code example:

import numpy as np

### Error message:
ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

<!-- Full error message, if any (starting from line Traceback: ...) -->


  File "C:\Users\morana\Documents\AI\UDEMY\Machine Learning A-Z\Part 2 - Regression\Section 5 - Multiple Linear Regression\multiple linear regression.py", line 46, in <module>
    regressor_OLS = sm.OLS(y,X_opt).fit()

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\regression\linear_model.py", line 838, in __init__
    hasconst=hasconst, **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\regression\linear_model.py", line 684, in __init__
    weights=weights, hasconst=hasconst, **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\regression\linear_model.py", line 196, in __init__
    super(RegressionModel, self).__init__(endog, exog, **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\base\model.py", line 216, in __init__
    super(LikelihoodModel, self).__init__(endog, exog, **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\base\model.py", line 68, in __init__
    **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\base\model.py", line 91, in _handle_data
    data = handle_data(endog, exog, missing, hasconst, **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\base\data.py", line 635, in handle_data
    **kwargs)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\base\data.py", line 80, in __init__
    self._handle_constant(hasconst)

  File "C:\Users\morana\AppData\Local\Continuum\anaconda3\lib\site-packages\statsmodels\base\data.py", line 125, in _handle_constant
    if not np.isfinite(ptp_).all():

TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

### Numpy/Python version information:
Spyder 4.0.0
1.17.4 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
@moranavni moranavni changed the title TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' dsTypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'' Dec 27, 2019
@filip-stolinski
Copy link

Hey!
Your X_opt array has a dtype of object and this may be causing an error. Try changing it to float. For example you can use this:
X= np.append(arr = np.ones((50,1)).astype(int), values = X, axis =1)
X_opt = X[:,[0,1,2,3,4,5]]
X_opt = np.array(X_opt, dtype=float)

Have fun :D

@riderman10000
Copy link

Thanks @filip-stolinski for your solution

@aliraza-aa
Copy link

@filip-stolinski Thank you very much for your solution. It definitely Works

@mattip
Copy link
Member

mattip commented Apr 15, 2020

Closing. Please reopen or open a new issue if needed.

@mattip mattip closed this as completed Apr 15, 2020
@Dev-Gaju
Copy link

Thanks @filip-stolinski

@shashankchakrawarty98
Copy link

thanks

@sidalikharef
Copy link

@filip-stolinski thank you that works for me <3

@SamuelOsondu
Copy link

Thanks so much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants