<jupyter_start><jupyter_text>THE SPARKS FOUNDATION  DATA SCIENCE AND BUSINESS ANALYTICS (GRIP JULY'21)  Task 1  Author: <NAME>  Task Title: Prediction using Supervised ML  Problem Statement:Predict the percentage of a student based on the no. of study hours.  Dataset provided: http://bit.ly/w-data  Importing Libraries<jupyter_code># Importing all libraries required in this notebook

# Linear Algebra
import pandas as pd

# Data processing
import numpy as np  

# Data Visualization
import matplotlib.pyplot as plt  
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Importing Dataset<jupyter_code># Reading data from remote link
url = "http://bit.ly/w-data"

# Creating the DataFrame 
data = pd.read_csv(url)

print("Data imported successfully")

# Viewing data
data.head(10)<jupyter_output>Data imported successfully<jupyter_text>Exploring the Data<jupyter_code>#Viewing top 5 rows of the dataset
data.head()
#Summary of DataFrame
data.info()
# Viewing Descriptive Statistic
data.describe()
data.shape
data.dtypes
# Checking for Null Values 
data.isnull()
data.isnull().sum()<jupyter_output><empty_output><jupyter_text>Data Visualization<jupyter_code># 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>**From the graph above, we can clearly see that there is a positive linear relation between the number of hours studied and percentage of score.**  Correlation Analysis<jupyter_code>corr = data.corr()
corr.style.background_gradient(cmap='coolwarm').set_precision(2)<jupyter_output><empty_output><jupyter_text>**Preparing the data**The next step is to divide the data into "attributes" (inputs) and "labels" (outputs).<jupyter_code>X = data.iloc[:, :-1].values  
y = data.iloc[:, 1].values<jupyter_output><empty_output><jupyter_text>Now that we have our attributes and labels, the next step is to split this data into training and test sets. We'll do this by using Scikit-Learn's built-in train_test_split() method:<jupyter_code>#Using train_test_split method
from sklearn.model_selection import train_test_split  
X_train, X_test, y_train, y_test = train_test_split(X, y, 
                            test_size=0.2, random_state=0)<jupyter_output><empty_output><jupyter_text>The data has been splitted into training and testing data in 70-30 ratio. 70% of the data has been given for model building and the rest 30% for testing the model.  **Training the Algorithm**We have split our data into training and testing sets, and now is finally the time to train our algorithm.<jupyter_code>from sklearn.linear_model import LinearRegression  
regressor = LinearRegression()  
regressor.fit(X_train, y_train) 
regressor.coef_
print("Training complete.")
print("Coeeficient:-", regressor.coef_)
print("Intercept:-", regressor.intercept_)
# Plotting the regression line
line = regressor.coef_*X+regressor.intercept_

# Plotting for the test data
plt.scatter(X, y)

plt.plot(X, line, color="black", label="Linear Refression Line");
plt.title('Hours vs Percentage')  
plt.xlabel('Hours Studied')  
plt.ylabel('Percentage Score')  
plt.show()<jupyter_output><empty_output><jupyter_text>**Making Predictions**Now that we have trained our algorithm, it's time to make some predictions.<jupyter_code># Testing data - In Hours
print(X_test) 

 # Predicting the scores
y_pred = regressor.predict(X_test)
# Comparing Actual vs Predicted
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})  
df 
df.plot(kind = "bar" , figsize=(7,7))
plt.show()
#Training and testing with our own data
print("Testing Score:",regressor.score(X_train,y_train))
print("Training Score:",regressor.score(X_test,y_test))<jupyter_output>Testing Score: 0.9515510725211552
Training Score: 0.9454906892105356<jupyter_text>Predicting the score if a student studies for 9.25 hrs/ day<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 = 93.69173248737538<jupyter_text>**Hence our model shows that if a student studies for 9.25 hrs/day, the predicted score will be around 93.69.**  **Evaluating the 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.<jupyter_code>def get_cv_scores(model):
    scores = cross_val_score(model,
                             X_train,
                             y_train,
                             cv=10,
                             scoring='r2')
    
    print('CV Mean: ', np.mean(scores))
    print('STD: ', np.std(scores))
    print('\n')
import sklearn 
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)))<jupyter_output>Mean Absolute Error: 4.183859899002975
Mean Squared Error: 21.5987693072174
Root Mean Squared Error: 4.6474476121003665<jupyter_text>*Mean Absolute Error is 4.1838, which is the mean of the absolute difference between the actual score and predicted score. *Mean squared error is the mean of square of the differences between the actual and predicted scores. Our MSE is 21.5987.*Root Mean Squared error is the most efficient way to find out the average difference between the predicted score by the model and the actual score. RMSE is the square root of MSE, which is 4.6474 in our case. This means that our model's predictions has about 46 deviation from the actual price.<jupyter_code>import statistics
SSE = sum((y_pred -  y_test)**2)
SST = sum((statistics.mean(y_pred)-y_test)**2)
R2=1-SSE/SST
R2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09759759759759759, 'fraction_numerical': 0.02769436102769436, 'mean_word_length': 5.104887983706721, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '479106', 'license': 'Apache-2.0', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1871, 'n_words': 591, 'path': 'Task 1_GRIP VEDANT MISHRA.ipynb', 'repo_name': 'Vedant2123/The-Sparks-Foundation-', 'size': '6001'}