<jupyter_start><jupyter_text>Prediction using Supervised ML  Predict the percentage of marks of an student based on the number of study hours<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
#Importing libraries
import warnings
warnings.filterwarnings('ignore')
data=pd.read_csv('https://raw.githubusercontent.com/AdiPersonalWorks/Random/master/student_scores%20-%20student_scores.csv')
data.head()
data.info()
data.describe()
data.shape<jupyter_output><empty_output><jupyter_text>Missing Value Check<jupyter_code>round(100 * (data.isnull().sum()/len(data)),2)
round(100 * (data.isnull().sum(axis=1)/len(data)),2).sort_values(ascending=False)[:10]<jupyter_output><empty_output><jupyter_text>No missing values in row & columns  Duplicate Check<jupyter_code>data_d=data.copy()
data_d.drop_duplicates(subset=None, inplace=True)
data_d.shape
del data_d
data.shape<jupyter_output><empty_output><jupyter_text>EXPLORATORY DATA ANALYSIS<jupyter_code>sns.pairplot(data, diag_kind='kde')
plt.show()
plt.figure(figsize=(10,5))
sns.scatterplot(x='Hours',y='Scores',data=data)
plt.grid()
plt.show()
plt.figure(figsize=(10,5))
sns.violinplot(x='Hours',data=data)
plt.show()<jupyter_output><empty_output><jupyter_text>No Outliers found<jupyter_code>plt.figure(figsize=(10,5))
sns.violinplot(x='Scores',data=data)
plt.show()<jupyter_output><empty_output><jupyter_text>No Outliers found<jupyter_code>plt.figure(figsize=(10,5))
sns.regplot(x='Hours',y='Scores',data=data)
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>Hours are directly proportional to Scores<jupyter_code>data.Hours.skew()<jupyter_output><empty_output><jupyter_text>Data is not skewed.   Test Train Split<jupyter_code># Creating X and Y sets 
X = data['Hours']
y = data['Scores']
from sklearn.model_selection import train_test_split

# We should specify 'random_state' so that the train and test data set always have the same rows, respectively

np.random.seed(0)
X_train, X_test, y_train, y_test = train_test_split(X,y, train_size = 0.70, random_state = 5)

# Checking the shapes for all the sets
print('X_train: ', X_train.shape)
print('X_test: ', X_test.shape)
print('y_train: ', y_train.shape)
print('y_test: ', y_test.shape)<jupyter_output>X_train:  (17,)
X_test:  (8,)
y_train:  (17,)
y_test:  (8,)<jupyter_text>Training the model<jupyter_code># Reshaping X train set
X_train = X_train.values.reshape(-1,1)
X_test = X_test.values.reshape(-1,1)
from sklearn.linear_model import LinearRegression
lr= LinearRegression()
lr.fit(X_train,y_train)
# Displaying model parameters
print('Intercept of line: ',round(lr.intercept_,2))
print('Coefficent of Hours: ',lr.coef_)
from sklearn.metrics import r2_score
y_train_pred = lr.predict(X_train)
r2score_train = r2_score(y_train,y_train_pred)*100
print('Train R2_score: ', round(r2score_train,2))
y_test_pred = lr.predict(X_test)
r2score_test = r2_score(y_test,y_test_pred)*100
print('Train R2_score: ', round(r2score_test,2))<jupyter_output>Train R2_score:  92.49<jupyter_text>Validating Linear Regression assumptions Error terms should be normally distributed with mean 0<jupyter_code>#Error terms are normally distributed
res = y_train - y_train_pred
plt.figure(figsize=(10,5))
sns.distplot(res)
plt.title('Error terms',fontweight = 'bold', fontsize = 18)
plt.show()<jupyter_output><empty_output><jupyter_text>It can be seen that the error terms are normally distributed with mean 0. Hence validated one of the assumptions of Linear Regression  Error terms should be randomly distributed<jupyter_code># Error terms should be randomly distributed
plt.figure(figsize=(10,5))
plt.scatter(X_train, res)
plt.title('Randomly distributed error terms',fontweight = 'bold', fontsize = 18)
plt.show()<jupyter_output><empty_output><jupyter_text>Error terms/ residuals dont tend to follow any particular pattern as such. They seem to be distributed randomly. With this, validated another assumption for Linear Regression<jupyter_code>#Visualizing the fit on the train set
plt.figure(figsize=(10,5))
plt.scatter(X_train, y_train)
plt.plot(X_train, y_train_pred, 'r')
plt.grid()
plt.xlabel('Hours studied', fontweight = 'bold')
plt.ylabel('Scores obtained', fontweight = 'bold')
plt.title('Best fit line for train set', fontweight = 'bold', fontsize = 16)
plt.show()
predicted_df = pd.DataFrame({'Actual': y_test, 'Predicted' : y_test_pred})
predicted_df.head()<jupyter_output><empty_output><jupyter_text>Evaluation<jupyter_code>import sklearn.metrics
score_lr=lr.score(X_test,y_test)
evs_lr=sklearn.metrics.explained_variance_score(y_test,lr.predict(X_test))
me_lr=sklearn.metrics.max_error(y_test,lr.predict(X_test))
mae_lr=sklearn.metrics.mean_absolute_error(y_test,lr.predict(X_test))
mse_lr=sklearn.metrics.mean_squared_error(y_test,lr.predict(X_test))
msle_lr=sklearn.metrics.mean_squared_log_error(y_test,lr.predict(X_test))
Mae_lr=sklearn.metrics.median_absolute_error(y_test,lr.predict(X_test))
r2_lr=sklearn.metrics.r2_score(y_test,lr.predict(X_test))
mpd_lr=sklearn.metrics.mean_poisson_deviance(y_test,lr.predict(X_test))
mgd_lr=sklearn.metrics.mean_gamma_deviance(y_test,lr.predict(X_test))
mtd_lr=sklearn.metrics.mean_tweedie_deviance(y_test,lr.predict(X_test))
print('Score for Linear Regression                   :',score_lr)
print('Explained Variance Score for Linear Regression:',evs_lr)
print('Max Error for Ridge Rigression                :',me_lr)
print('Mean Absolute Error for Linear Regression     :',mae_lr)
print('Mean Square Error for Linear Regression       :',mse_lr)
print('Mean Squared Log Error for Linear Regression  :',msle_lr)
print('Median Absolute Error for Linear Regression   :',Mae_lr)
print('R2 Score for Linear Regression                :',r2_lr)
print('Mean Poisson Deviance for Linear Regression   :',mpd_lr)
print('Mean Gamma Deviance for Linear Regression     :',mgd_lr)
print('Mean Tweedie Deviance for Linear Regression   :',mtd_lr)
#Objective is to find out scores for studied hour 9.25

hours = 9.25
new_pred = lr.predict([[hours]])
print("           Number of hours = {}".format(hours))
print("Predicted percentage score = {}".format(round(new_pred[0],2)))<jupyter_output>Number of hours = 9.25
Predicted percentage score = 90.96
{'fraction_non_alphanumeric': 0.12435897435897436, 'fraction_numerical': 0.016185897435897437, 'mean_word_length': 7.668055555555555, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '568508', 'license': 'Apache-2.0', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2240, 'n_words': 454, 'path': 'understanding-linear-regression-step-by-step.ipynb', 'repo_name': 'GDGauravDutta/Sparks-Foundation-TSF', 'size': '6240'}