I am trying multiple Regression

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

# Importing Dataset
dataset = pd.read_csv(
    'C:/Users/Rupali Singh/Desktop/ML A-Z/Machine Learning A-Z Template Folder/Part 2 - Regression/Section 5 - Multiple Linear Regression/50_Startups.csv')
X = dataset.iloc[:, :-1].values
Y = dataset.iloc[:, 4].values

# Categorical Data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

labelencoder = LabelEncoder()
X[:, 3] = labelencoder.fit_transform(X[:, 3])
onehotencoder = OneHotEncoder(categorical_features=[3])
X = onehotencoder.fit_transform(X).toarray()

# Splitting the dataset into 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)

# Fitting Multiple Linear Regression

from sklearn.linear_model import LinearRegression

regressor = LinearRegression()
regressor.fit(X_train, Y_train)

# predicting the test result
Y_pred = regressor.predict(X_test)

This is the part where error is occuring

# Building the optimal model with Backward Elimination
import statsmodels.formula.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(endog=Y, exog=X_opt).fit()

This is the error message

Traceback (most recent call last):
  File "C:/Users/Rupali Singh/PycharmProjects/Machine_Learning/Muliple_Linear_Regression.py", line 39, in <module>
    import statsmodels.formula.api as sm
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\statsmodels\formula\api.py", line 15, in <module>
    from statsmodels.discrete.discrete_model import MNLogit
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\statsmodels\discrete\discrete_model.py", line 45, in <module>
    from statsmodels.distributions import genpoisson_p
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\statsmodels\distributions\__init__.py", line 2, in <module>
    from .edgeworth import ExpandedNormal
  File "C:\Users\Rupali Singh\PycharmProjects\Machine_Learning\venv\lib\site-packages\statsmodels\distributions\edgeworth.py", line 7, in <module>
    from scipy.misc import factorial
ImportError: cannot import name 'factorial'

Process finished with exit code 1

2 Answers 2


See https://stackoverflow.com/a/56284155/9524424

You need to have a matching scipy version (1.2 instead of 1.3)


This is essentially an incompatibility in statsmodels with the version of scipy that it uses: statsmodels 0.9 is not compatible with scipy 1.3.0. I would call that a bug. It has been reported already. If you upgrade to the latest development version of statsmodels, the problem will disappear:

pip install --upgrade Cython
pip install --upgrade git+https://github.com/statsmodels/statsmodels

For me, this fixed the problem. An alternative would be to downgrade scipy to version 1.2.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.