# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# <b>Importing necessary packages</b>

import numpy as np
from numpy import nan
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline
import seaborn as sns

db = pd.read_csv('http://bit.ly/w-data')
db.head()

db.info()

# <b>The dataset doesn't contain any null objects.</b>

db.describe()

sns.scatterplot(data=db, x='Hours', y='Scores', palette='bright')
plt.xlabel('Hours Studied')  
plt.ylabel('Percentage Score')
plt.show()

print('min hours:',db['Hours'].min())
print('max hours:',db['Hours'].max())

print('min score:',db['Hours'].min())
print('max score:',db['Hours'].max())

db.boxplot(['Scores'])

db.boxplot(['Hours'])

# <b>there are no outliers in the given dataset.</b>

db["Hours"].value_counts(bins=4).sort_index()

db["Scores"].value_counts(bins=4).sort_index()

# <b>The hours and scores are normally distributed hence we can easily perform Linear Regression.</b>

sns.heatmap(db.corr(),annot=True)

# <b>Hours and Scores are highly Positively correlated to each other</b>

sns.pairplot(db)

# <b>Splitting the dataset for training and testing</b>

from sklearn.model_selection import train_test_split

x=db.iloc[:,:-1].values
y=db.iloc[:,1].values
x_train, x_test, y_train, y_test= train_test_split(x, y,train_size=0.60,test_size=0.40,random_state=0)

# <b>Training Linear Regression Model</b>

from sklearn.linear_model import LinearRegression
model= LinearRegression()
model.fit(x_train, y_train)


line = model.coef_*x+model.intercept_
# Plotting for the test data
plt.scatter(x, y)
plt.plot(x, line);
plt.show()

y_pred = model.predict(x_test)
y_pred

# <b>Predicting Scores</b>

print('Test Score')
print(model.score(x_test, y_test))
print('Training Score')
print(model.score(x_train, y_train))

# +

output = pd.DataFrame({'Actual Score': y_test,'Predicted Score': y_pred, 'Residual':y_test-y_pred })
print(output.head())
# -

# <b>What will be predicted score if a student study for 9.25 hrs in a day?</b>

print('Score of student who studied for 9.25 hours a day', model.predict([[9.25]]))

# <h1>Model Evaluation</h1>

from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error
from sklearn import metrics

print('Mean absolute error : ', metrics.mean_absolute_error(y_test, y_pred))

print('Root mean square error : ', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

metrics.r2_score(y_test,y_pred)

from scipy import stats
t_statistic, pvalues =stats.ttest_ind(y_test, y_pred)
print('t-statistic -->',t_statistic)
print('P-value -->',pvalues)

f_statistic, pvalues =stats.f_oneway(y_test, y_pred)
print('f-statistic -->',f_statistic)
print('P-value -->',pvalues)

# <h3>summary</h3>
#
# The datset given to us is a clean dataset with 2 attributes Hours and Scores and no null values present.
# With the help of seaborn and matplotlib we visualized the data and found it to be normally distributed.
#
# We performed Linear Regression operation on the given dataset and the model had an accuracy of 95%. Thus, the model could predict the score for a student who studies for 9.25hrs in a day which is 92.65%. 
#
# In test sample R-square ,T-test, F-test were performed to measure the model performance in terms of goodness of fit & randomness of variance between actual and the predicted values.



StarcoderdataJupyterScriptsDedupFiltered