# ---
# 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
# ---

# #  The Sparks foundation Task-1

# Importing all the library which will be needed

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline

# Read the Data and make a dataframe

url="http://bit.ly/w-data"
df=pd.read_csv(url)
print(df.shape)
df.head(10)

# To check linearity in the data, Plot the scatter plot 

# %matplotlib inline
df.plot(x="Hours", y="Scores", style='o')
plt.xlabel('Hours of study')
plt.ylabel('Score')
plt.show()

# yes, our data has lineariry so we can apply linear regression model to our Data. Here we have single variable so that we use simple linear regression.

# Before spliting the data reshape, convert into array (Give reason) and store data in X and Y varible 

x=np.array(df['Hours']).reshape(-1,1)
y=np.array(df['Scores']).reshape(-1,1)
print('Hours =\n',x)
print('Score =\n',y)

# ## Split the Data

from sklearn.model_selection import train_test_split
xtrain, xtest, ytrain, ytest = train_test_split(x,y,test_size=0.2,random_state=101)
print("Training Data")
print('X train=\n',xtrain)
print('Y train=\n',ytrain)

# ## Training the Model

from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
regressor.fit(xtrain,ytrain)
print('Training completed')

# ## Visualisation on Trained Model

plt.scatter(xtrain, ytrain, color='red', label='Training data')
plt.plot(xtrain,regressor.predict(xtrain), color='blue', label='Regression line')
plt.title('Visuals for Trained Model')
plt.xlabel("Hours of Study")
plt.ylabel('Scores')
plt.legend()
plt.show()

# ## Visualisation on Testing Data

plt.scatter(xtest, ytest, color='red', label='Testing data')
plt.plot(xtrain,regressor.predict(xtrain), color='blue', label='Regression line')
plt.title('Visuals for Testing Data')
plt.xlabel("Hours of Study")
plt.ylabel('Scores')
plt.legend()
plt.show()

# ### Making predictions

ypred= regressor.predict(xtest)
print('xtest value=\n',xtest)
print('Absolute value=\n',ytest)
print('Predicted value=\n',ypred) 

# What will be predicted score if a student studies for 9.25 hrs/ day?

# You can also test with your own data
hours = [[9.25]]
own_pred = regressor.predict(hours)
print("No of Hours = {}".format(hours))
print("Predicted Score = {}".format(own_pred[0]))

# ## Evaluating  Model

# The 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. For simplicity here, we have chosen the mean square error. There are many such metrics.

# ### R-score value

from sklearn.metrics import r2_score
r2_score(ytest, ypred)

# ### Error

from sklearn import metrics
print('MAE=',metrics.mean_absolute_error(ytest,ypred),'(Mean absolute error)')
print('MSE',metrics.mean_squared_error(ytest,ypred),'(Mean squared error)')
print('RMSE',np.sqrt(metrics.mean_squared_error(ytest,ypred)),'(Root mean squared error)')

StarcoderdataJupyterScriptsDedupFiltered