In [155]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [156]:
np.set_printoptions(precision=3, suppress=True)
In [157]:
dataset = pd.read_csv('50_Startups.csv')
In [158]:
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
In [159]:
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
In [160]:
labelencoder = LabelEncoder()
X[:, 3] = labelencoder.fit_transform(X[:, 3])
In [161]:
onehotencoder = OneHotEncoder(categorical_features=[3])
X = onehotencoder.fit_transform(X).toarray()
In [162]:
X = X[:, 1:]
In [163]:
from sklearn.model_selection import train_test_split
In [164]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
In [165]:
from sklearn.linear_model import LinearRegression
In [166]:
regressor = LinearRegression()
regressor.fit(X_train, y_train)
Out[166]:
In [167]:
y_pred = regressor.predict(X_test)
pd.DataFrame([y_pred, y_test])
Out[167]:
In [168]:
import statsmodels.formula.api as sm
In [169]:
X = np.append(arr=np.ones((50, 1)).astype(int), values=X, axis=1)
In [173]:
X_opt = X[:, [0, 1, 2, 3, 4, 5]]
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
regressor_OLS.summary()
Out[173]:
In [174]:
X_opt = X[:, [0, 1, 3, 4, 5]]
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
regressor_OLS.summary()
Out[174]:
In [177]:
X_opt = X[:, [0, 3, 4, 5]]
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
regressor_OLS.summary()
Out[177]:
In [179]:
X_opt = X[:, [0, 3, 5]]
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
regressor_OLS.summary()
Out[179]:
In [180]:
X_opt = X[:, [0, 3]]
regressor_OLS = sm.OLS(endog=y, exog=X_opt).fit()
regressor_OLS.summary()
Out[180]: