text_plain_output_1.png
from sklearn.model_selection import train_test_split

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

df = pd.read_csv('../input/batchgradientdescent/ex1data1.csv', header=None)
df

df.columns = ['Population of City in 10,000s', 'Profit in £10,000s']

X = df.iloc[:, :-1]
y = df.iloc[:, 1]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1 / 4, random_state=42)
X_train = X_train.reset_index(drop=True)
y_train = y_train.reset_index(drop=True)
X_test = X_test.reset_index(drop=True)
y_test = y_test.reset_index(drop=True)

m, n = X_train.values.shape
o, p = X_test.values.shape
X_train = pd.concat((pd.DataFrame(np.ones((m, 1)), columns=['Bias']), X_train), axis=1)
X_test = pd.concat((pd.DataFrame(np.ones((o, 1)), columns=['Bias']), X_test), axis=1)

pop_prof_model = LinearRegression()

pop_prof_model.fit(X_train, y_train)
y_pred_train = pop_prof_model.predict(X_train)
MSE_train = pop_prof_model.LR_Cost(X_train, y_train)

pop_prof_model.fit(X_test, y_test)
y_pred_test = pop_prof_model.predict(X_test)
MSE_test = pop_prof_model.LR_Cost(X_test, y_test)
print('Theta estimates are: {}'.format(pop_prof_model.theta))
print('Test dataset mean squared error: {}'.format(MSE_test))