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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('50_Startups.csv')
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,4].values
print (dataset)
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([("Country", OneHotEncoder(), [3])], remainder = 'passthrough')
X = ct.fit_transform(X)
X = X[:, 1:]
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)
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
y_pred = regressor.predict(X_test)
import statsmodels.api as sm
X= np.append(arr = np.ones((50,1)).astype(int), values = X, axis =1)
X_opt = X[:,[0,1,2,3,4,5]]
regressor_OLS = sm.OLS(y,X_opt).fit()
regressor_OLS.summary()
```python
import numpy as np
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''
Spyder 4.0.0
1.17.4 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
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
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