IndexError: индекс 4 выходит за границы оси 1 размера 4 - PullRequest
0 голосов
/ 10 января 2020

Я изучаю машинное обучение онлайн. В модели множественной регрессии, когда я пишу следующий код:

# multiple linear regression
import pandas as pd
import numpy as np

dataset = pd.read_csv("50_Startups.csv")
x = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 4].values

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencoder_x = LabelEncoder()
x[:, 3] = labelencoder_x.fit_transform(x[:, 3])

ct = ColumnTransformer(
    [('one_hot_encoder', OneHotEncoder(categories="auto"), [3])],
    remainder="passthrough"
)

# avoiding the dummy variable trap
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)

# fitting multiple libnear regresion 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)
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()
regressor_ols.summary()

Я получил следующую ошибку:

Traceback (most recent call last):
  File "/home/ashutosh/Machine Learning A-Z Template Folder/Part 2 - Regression/Section 5 - Multiple Linear Regression/P14-Multiple-Linear-Regression/Multiple_Linear_Regression/mlr.py", line 35, in <module>
    x_opt = x[:, [0, 1, 2, 3, 4, 5]]
IndexError: index 4 is out of bounds for axis 1 with size 4

Я проверил несколько ответов, но у них нет той же проблемы, что и моя. Что я могу сделать?

Вы можете скачать набор данных здесь: https://sds-platform-private.s3-us-east-2.amazonaws.com/uploads/P14-Multiple-Linear-Regression.zip

...