<jupyter_start><jupyter_text>SIMPLE LINEAR REGRESSIONIn given task we have to predict the percentage of marks expected by the student based upon the number of hours they studied. In this task only two variables are involved.<jupyter_code># Importing all libraries required in this notebook
import pandas as pd
import numpy as np  
import matplotlib.pyplot as plt  
%matplotlib inline
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error
#Import the data
url="http://bit.ly/w-data"
data=pd.read_csv(url)
data1=data
print("The data is imported successfully")
data
data.describe()<jupyter_output><empty_output><jupyter_text>**Insights**1.   From the hours column, total count is 25 hrs and minimum students spents for studying is 1.1 hrs and 9.2 maximum.2.   From the scores column, maximum student scor is 95 and mimnimum is 17  DATA VISUALIZATIONNow let's plot a graph of our data so that it will give us clear idea about data.<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>Linear Regression ModelNow we prepare the data and split it in test data<jupyter_code>#Splitting training and testing data
x=data.iloc[:,:-1].values
y=data.iloc[:,1].values
x_train, x_test, y_train, y_test= train_test_split(x, y,train_size=0.80,test_size=0.20,random_state=0)<jupyter_output><empty_output><jupyter_text>Training the model<jupyter_code>from sklearn.linear_model import LinearRegression
linearRegressor= LinearRegression()
linearRegressor.fit(x_train, y_train)
y_predict= linearRegressor.predict(x_train)<jupyter_output><empty_output><jupyter_text>Training the Algorithm Now the spliting of our data into training and testing sets is done, now it's time to train our algorithm.<jupyter_code>regressor = LinearRegression()  
regressor.fit(x_train, y_train) 

print("Training complete.")
# Plotting the regression line
line = regressor.coef_*x+regressor.intercept_
# Plotting for the test data
plt.scatter(x, y)
plt.plot(x, line);
plt.show()<jupyter_output><empty_output><jupyter_text>Checking the accuracy scores for training and test set<jupyter_code>print('Test Score')
print(regressor.score(x_test, y_test))
print('Training Score')
print(regressor.score(x_train, y_train))<jupyter_output>Test Score
0.9454906892105356
Training Score
0.9515510725211553<jupyter_text>Now we make predictions<jupyter_code>data= pd.DataFrame({'Actual': y_test,'Predicted': y_predict})
data
#Let's predict the score for 9.25 hpurs
print('Score of student who studied for 9.25 hours a dat', regressor.predict([[9.25]]))<jupyter_output>Score of student who studied for 9.25 hours a dat [93.69173249]<jupyter_text>Model Evaluation Metrics<jupyter_code>#Checking the efficiency of model
mean_squ_error = mean_squared_error(y_test, y_predict)
mean_abs_error = mean_absolute_error(y_test, y_predict)
print("Mean Squred Error:",mean_squ_error)
print("Mean absolute Error:",mean_abs_error)<jupyter_output>Mean Squred Error: 21.5987693072174
Mean absolute Error: 4.183859899002975
{'fraction_non_alphanumeric': 0.09046454767726161, 'fraction_numerical': 0.03361858190709047, 'mean_word_length': 7.349489795918367, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '438995', 'license': 'MIT', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1063, 'n_words': 309, 'path': 'Task 2 To Explore Supervised Machine Learning/Linear_regression_model_Task_1.ipynb', 'repo_name': 'heroorkrishna/The-spark-Foundation-tasks', 'size': '3272'}