<jupyter_start><jupyter_text>Author: <NAME> Task 1: Prediction using Supervised ML To Predict the percentage of marks of the students based on the number of hours they studied In supervised learning, models are trained using labelled dataset, where the model learns about each type of data. Once the training process is completed, the model is tested on the basis of test data (a subset of the training set), and then it predicts the output.  1.Importing the Libraries  and the data Pandas to import and analyze data, NumPy to perform the multi-dimensional operation, and matplotlib to perform graphical plot.<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt 
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error<jupyter_output><empty_output><jupyter_text>Importing the data from .csv file<jupyter_code>data = pd.read_csv ('http://bit.ly/w-data')<jupyter_output><empty_output><jupyter_text>Viewing the data that we imported to pandas dataframe object<jupyter_code>data<jupyter_output><empty_output><jupyter_text>2.Viewing and Describing the dataNow we view the Head and Tail of the data using head() and tail() respectively.<jupyter_code>data.head()

data.tail()<jupyter_output><empty_output><jupyter_text>Checking the dimensions/shape of the dataset using shape.<jupyter_code>data.shape<jupyter_output><empty_output><jupyter_text>Viewing Column names of the dataset using columns<jupyter_code>data.columns<jupyter_output><empty_output><jupyter_text>View datatypes of each column in the dataset using dtype.<jupyter_code>data.dtypes<jupyter_output><empty_output><jupyter_text>Describing the data as basic statistics using describe()<jupyter_code>data.describe()

data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 25 entries, 0 to 24
Data columns (total 2 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   Hours   25 non-null     float64
 1   Scores  25 non-null     int64  
dtypes: float64(1), int64(1)
memory usage: 528.0 bytes<jupyter_text>3.Checking the data for inconsistencies<jupyter_code>data.isnull().sum()<jupyter_output><empty_output><jupyter_text>4.Data Visualization<jupyter_code># Plotting the distribution of scores
sns.set_style('darkgrid')
data.plot(x='Hours', y='Scores', style='o')  
plt.title('Hours vs Percentage',size=20)  
plt.xlabel('Hours Studied',size=15)  
plt.ylabel('Percentage Score',size=15)  
plt.show()<jupyter_output><empty_output><jupyter_text>From the graph we can safely assume a positive linear relation between the number of hours studied and percentage of score.  Correlation<jupyter_code>data.corr()<jupyter_output><empty_output><jupyter_text>Regression Plot<jupyter_code>sns.set_style('darkgrid')
sns.regplot(x=data['Hours'],y=data['Scores'])
plt.xlabel('Hours studied')
plt.ylabel('Percentage Score')
plt.title('Regression Plot',size=20)
plt.show()
print(data.corr())<jupyter_output><empty_output><jupyter_text>5.Data Preprocessing  Dividing the data into attributes and labels<jupyter_code>X=data.iloc[:,:-1].values
y=data.iloc[:,-1].values<jupyter_output><empty_output><jupyter_text>No of Hours Studied<jupyter_code>X<jupyter_output><empty_output><jupyter_text>Scores obtained<jupyter_code>y<jupyter_output><empty_output><jupyter_text>6.Traning the Model<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0) 
regressor = LinearRegression()  
regressor.fit(X_train.reshape(-1,1), y_train) 

print("Training complete.")<jupyter_output>Training complete.<jupyter_text>Plotting the Line of regression since our model is trained now, its the time to visualize the best-fit line of regression.<jupyter_code># Plotting the regression line
line = regressor.coef_*X+regressor.intercept_

# Plotting for the test data
plt.scatter(X, y)
plt.plot(X, line,color='red');
plt.show()<jupyter_output><empty_output><jupyter_text>Making Predictions Now that we have trained our algorithm, it's time to test the model by making some predictions.For this we will use our test-set data<jupyter_code># Testing data
print(X_test)
# Model Prediction 
y_pred = regressor.predict(X_test)<jupyter_output>[[1.5]
 [3.2]
 [7.4]
 [2.5]
 [5.9]]<jupyter_text>7.Comparing Actual result to the Predicted Model result<jupyter_code># Comparing Actual vs Predicted
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred}) 
df
#Estimating training and test score
print("Training Score:",regressor.score(X_train,y_train))
print("Test Score:",regressor.score(X_test,y_test))
# Plotting the Bar graph to depict the difference between the actual and predicted value

df.plot(kind='bar',figsize=(5,5))
plt.grid(which='major', linewidth='0.5', color='red')
plt.grid(which='minor', linewidth='0.5', color='blue')
plt.show()<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code># Testing the model with our own data
hours = 9.25
test = np.array([hours])
test = test.reshape(-1, 1)
own_pred = regressor.predict(test)
print("No of Hours = {}".format(hours))
print("Predicted Score = {}".format(own_pred[0]))<jupyter_output>No of Hours = 9.25
Predicted Score = 93.69173248737538<jupyter_text>8.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. Here different errors have been calculated to compare the model performance and predict the accuracy.<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('R-2:', metrics.r2_score(y_test, y_pred))<jupyter_output>Mean Absolute Error: 4.183859899002975
Mean Squared Error: 21.5987693072174
Root Mean Squared Error: 4.6474476121003665
R-2: 0.9454906892105356
{'fraction_non_alphanumeric': 0.10897965933520753, 'fraction_numerical': 0.026294030097569043, 'mean_word_length': 6.968379446640316, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 0, '>': 82, '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': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '132113', 'license': 'MIT', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2001, 'n_words': 530, 'path': 'Task1.ipynb', 'repo_name': 'vishnupachunuri/thesparksfoundinternship', 'size': '6057'}