image_output_1.png
from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

import pandas as pd

data = pd.read_csv('../input/salary/Salary_Data.csv')
data

df = pd.DataFrame(data)
df

x = df.YearsExperience.values.reshape(-1, 1)
y = df.Salary.values.reshape(-1, 1)

regressor = LinearRegression()
regressor.fit(x_train, y_train)

y_pred = regressor.predict(x_test)
y_pred

a = x_test
b = y_test
c = x_test
d = y_pred

a = x_test
b = y_test
c = x_test
d = y_pred

compare = pd.DataFrame({'Actual': y_test.flatten(), 'Prediction': y_pred.flatten()})
compare

a = x_train
b = y_train
c = x_test
d = y_pred

df2 = pd.DataFrame({'YearsExperience': [1.5, 2.5, 3.5, 4.5, 5], 'Salary': [1, 4, 8, 9, 10]})

df3 = df.append(df2)
df3

train = df3.iloc[:25]
test = df3.iloc[25:]

x_train = df3['YearsExperience'][:30].values.reshape(-1, 1)
y_train = df3['Salary'][:30].values.reshape(-1, 1)

x_test = df3['YearsExperience'][30:].values.reshape(-1, 1)

regressor.fit(x_train, y_train)
y_pred = regressor.predict(x_test)

a = x_train
b = y_train
c = x_test
d = y_pred
plt.scatter(a, b)
plt.scatter(c, d)
plt.grid()
plt.show()