Numpy: 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''

Created on 27 Dec 2019  ·  6Comments  ·  Source: numpy/numpy

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:

```python
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''

File "C:UsersmoranaDocumentsAIUDEMYMachine Learning A-ZPart 2 - RegressionSection 5 - Multiple Linear Regressionmultiple linear regression.py", line 46, in
regressor_OLS = sm.OLS(y,X_opt).fit()

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsregressionlinear_model.py", line 838, in __init__
hasconst=hasconst, **kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsregressionlinear_model.py", line 684, in __init__
weights=weights, hasconst=hasconst, **kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsregressionlinear_model.py", line 196, in __init__
super(RegressionModel, self).__init__(endog, exog, **kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsbasemodel.py", line 216, in __init__
super(LikelihoodModel, self).__init__(endog, exog, **kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsbasemodel.py", line 68, in __init__
**kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsbasemodel.py", line 91, in _handle_data
data = handle_data(endog, exog, missing, hasconst, **kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsbasedata.py", line 635, in handle_data
**kwargs)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsbasedata.py", line 80, in __init__
self._handle_constant(hasconst)

File "C:UsersmoranaAppDataLocalContinuumanaconda3libsite-packagesstatsmodelsbasedata.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)]

Most helpful comment

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

All 6 comments

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

Thanks @filip-stolinski for your solution

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

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

Thanks @filip-stolinski

thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmvianna picture dmvianna  ·  4Comments

keithbriggs picture keithbriggs  ·  3Comments

inducer picture inducer  ·  3Comments

qualiaa picture qualiaa  ·  3Comments

astrofrog picture astrofrog  ·  4Comments