<jupyter_start><jupyter_text>Simple Linear RegressionIn this regression task (Task2) we will predict the percentage of marks that a student is expected to score based upon the number of hours they studied. This is a simple linear regression task as it involves just two variables i.e. there is a single predictor<jupyter_code>import pandas as pd
import numpy as np
data = pd.read_excel("C:\\Users\\<NAME>\\Desktop\\GRIP\\Task2.xlsx")
data.head()
data.tail()
data.info()
data.describe()
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline<jupyter_output><empty_output><jupyter_text>We can use pairplot from matplotlib library to see the relationship between scores and number of hours. It shows that there is a positive linear relationship between hours studies and percentage scores<jupyter_code>sns.pairplot(data)
# Plotting the distribution of scores
data.plot(x ='Hours', y ='Scores', style ='o')  
plt.title('Hours vs Percentage')  
plt.xlabel('Hours Studied')  
plt.ylabel('Percentage Score')  
plt.show()<jupyter_output><empty_output><jupyter_text>Preparing the dataThe next step is to divide the data into "attributes" (inputs) and "labels" (outputs) wherein we have classified x and y<jupyter_code>X = data.iloc[:, :-1].values  
y = data.iloc[:, 1].values<jupyter_output><empty_output><jupyter_text>Training the AlgorithmWe have split our data into training and testing sets and will train the algorithm<jupyter_code>from sklearn.model_selection import train_test_split  
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                            train_size = 0.7, random_state = 0) 
from sklearn.linear_model import LinearRegression  
regressor = LinearRegression()  
regressor.fit(X_train, y_train) 

print("Training complete.")
# Plotting the regression line
line = regressor.coef_* X + regressor.intercept_

# Plotting for the test data
plt.scatter(X, y)
plt.plot(X, line);
plt.show()<jupyter_output><empty_output><jupyter_text>Making PredictionsNow that we have trained our algorithm we will make predictions and try to answer the questions at hand.<jupyter_code>print(X_test) # Testing data - In Hours
y_pred = regressor.predict(X_test) # Predicting the scores
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})  
df 
# You can also test with your own data
own_pred = regressor.predict([[9.25]])
print('Predicted Percentage Score, if a student studies for 9.25 hours is: ', own_pred)<jupyter_output>Predicted Percentage Score, if a student studies for 9.25 hours is:  [92.91505723]<jupyter_text>Evaluating the modelThe final step is to evaluate the performance of algorithm. This step is particularly important to compare how well different algorithms perform on a particular dataset. There are many metrics like Mean_Squared_Error, Root_Mean_Squared_Error, Mean Absolute Error etc.<jupyter_code>from sklearn import metrics  
mae = metrics.mean_absolute_error(y_test, y_pred)
from sklearn.metrics import mean_squared_error, r2_score
mse = mean_squared_error(y_test, y_pred)
r_squared = r2_score(y_test, y_pred)
from math import sqrt
rmse = sqrt(mse)
print('Mean Absolute Error:', mae)
print('Mean Squared Error :' ,mse)
print('Root Mean Squared Error :' ,rmse)
print('R Square Value :',r_squared)<jupyter_output>Mean Absolute Error: 4.419727808027652
Mean Squared Error : 22.96509721270043
Root Mean Squared Error : 4.792191274636315
R Square Value : 0.9568211104435257
StarcoderdataJupyterStructuredCleanDedup