<jupyter_start><jupyter_text>First let's import the modules we're going to use for this Linear Regression problem<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Now let's load the data set<jupyter_code>data = pd.read_csv("http://bit.ly/w-data")
print("Data imported successfully")<jupyter_output>Data imported successfully<jupyter_text>Plotting the Data to get a sense of what we're dealing with<jupyter_code>data.plot(x = "Hours", y = "Scores", style = 'o')
plt.title("Hours vs Scores")
plt.xlabel("Hours")
plt.ylabel("Scores")<jupyter_output><empty_output><jupyter_text>Splitting the Data into inputs (X) and Outputs (y)<jupyter_code>X = data.iloc[:, :-1].values
y = data.iloc[:, 1].values<jupyter_output><empty_output><jupyter_text>Splitting the Dataset into training set and test set<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state = 0)<jupyter_output><empty_output><jupyter_text>Fitting the linear model (creating an instance and training)<jupyter_code>model = LinearRegression()
model.fit(X_train, y_train)
print("Training Completed")<jupyter_output>Training Completed<jupyter_text>Plotting the resulting line<jupyter_code>line = model.coef_ * X + model.intercept_
plt.scatter(X, y)
plt.plot(X, line)<jupyter_output><empty_output><jupyter_text>Testing the results<jupyter_code>y_pred = model.predict(X_test)
df = pd.DataFrame({"Actual" : y_test, "Predicted" : y_pred})
print(df)<jupyter_output>Actual  Predicted
0      20  16.884145
1      27  33.732261
2      69  75.357018
3      30  26.794801
4      62  60.491033<jupyter_text>Making predictions on our own values<jupyter_code>hours = 9.25
own_pred = model.predict([[hours]])
print(f"Number of Hours = {hours}\nPredicted Score = {own_pred[0]}")<jupyter_output>Number of Hours = 9.25
Predicted Score = 93.69173248737535