<jupyter_start><jupyter_text>THE SPARKS FOUNDATION  DATA SCIENCE AND BUSINESS ANALYTICS JUNE'21 **AUTHOR** : <NAME> Task: 1 Prediction using Supervised ML**Objective:** From given dataset, predict the percentage of an student based on the no. of study hours.**Dataset:** http://bit.ly/w-data  Importing Libraries<jupyter_code># Importing all libraries required in this notebook
import pandas as pd
import numpy as np  
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Importing Data<jupyter_code># Loading Dataset
df = pd.read_csv("http://bit.ly/w-data")
print("Data Imported")

# Printing the first 10 rows of the dataset
df.head(10)<jupyter_output>Data Imported<jupyter_text>Explore the Data<jupyter_code># Checking for NaN Values

df.isnull()
# Getting to know more about the dataset
df.info()
df.shape
df.describe()<jupyter_output><empty_output><jupyter_text>Visualize the Data<jupyter_code>import matplotlib.pyplot as plt
plt.scatter(df.Hours, df.Scores,color = "purple", marker="*")
plt.title("Hours Vs Scores")
plt.xlabel(" Hours -->")
plt.ylabel(" Scores -->")
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>From above graph, it's very much clear that Hours and Scores have a linear relation. Thus, a linear regression model will be a perfect fit for solving this problem.  Preparing the data Here we are using **train_test_split()** to divide the data, assigning 20% of data for Testing while the remaining 80% data is for Training.<jupyter_code>train = df.iloc[:, :-1].values  
test = df.iloc[:, 1].values 
# Using train_test_split() method

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(train,test, test_size = 0.2, random_state = 0)<jupyter_output><empty_output><jupyter_text>Building and Training the Model<jupyter_code>from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(train,test)
print ("Coefficient:-", regressor.coef_)
print ("Intercept:-", regressor.intercept_)
# defining regression line
line = regressor.coef_*train+regressor.intercept_

# plotting for the test data
plt.scatter(X_train, y_train, label='Train Data',color='maroon')
plt.scatter(X_test, y_test, label='Test Data',color='aqua')

plt.plot(train, line, color='orange', label='Linear Regression Line')
plt.title('Hours vs Scores')
plt.xlabel('Hours')
plt.ylabel('Scores')

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Actual vs Predicated<jupyter_code>Y_pred = regressor.predict(X_test)
print(Y_pred)
df = pd.DataFrame({'Actual':y_test, 'Predicted':Y_pred})
df<jupyter_output><empty_output><jupyter_text>Testing Model Fit<jupyter_code># Training and Testing Score
print("Training Score: ", regressor.score(X_train, y_train))
print("Testing Score: ", regressor.score(X_test,y_test))
df.plot(kind='bar',figsize=(7,7))
plt.grid()
plt.show()<jupyter_output><empty_output><jupyter_text>Predicting score For 9.25 hrs/day of study<jupyter_code># Testing with our own data

hours = 9.25
pred_score = regressor.predict([[9.25]])
print("No of Hours = {}".format(hours))
print("Predicted Score = {}".format(pred_score[0]))<jupyter_output>No of Hours = 9.25
Predicted Score = 92.90985477015731<jupyter_text>**Conclusion:** For 9.25 hrs/day of study, student will score 92.9 i.e. 93 Marks   Evaluation of Model<jupyter_code>from sklearn import metrics

print(" Mean Absolute Error:-", metrics.mean_absolute_error(y_test, Y_pred))
print(" Mean Squared Error:-",metrics.mean_squared_error(y_test,Y_pred))
print(" Root Mean Squared Error:-",np.sqrt(metrics.mean_squared_error(y_test,Y_pred)))
print(" Explained Variance Score:-",metrics.explained_variance_score(y_test,Y_pred))
print(" R-2:-", metrics.r2_score(y_test, Y_pred))<jupyter_output>Mean Absolute Error:- 4.071877793635608
 Mean Squared Error:- 20.1389481299402
 Root Mean Squared Error:- 4.487643939746134
 Explained Variance Score:- 0.951522433518808
 R-2:- 0.9491748734859171
{'fraction_non_alphanumeric': 0.12550100200400802, 'fraction_numerical': 0.034569138276553106, 'mean_word_length': 7.099391480730223, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '105622', 'license': 'Apache-2.0', 'n_tokens_mistral': 1540, 'n_tokens_neox': 1377, 'n_words': 342, 'path': 'Task_1_Prediction_Using_Supervised_ML.ipynb', 'repo_name': 'BhoomiVarshney/The_Sparks_Foundation_Tasks', 'size': '4001'}