<jupyter_start><jupyter_text>**Task:** **To find out the score of a student based on the study hours.**<jupyter_code>import pandas as pd  
import numpy as np  
import matplotlib.pyplot as plt  
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression  
from sklearn import metrics 
%matplotlib inline
#reading csv file

url ='http://bit.ly/w-data'
df = pd.read_csv(url)
print("Data imported successfully.")
df.head()<jupyter_output>Data imported successfully.<jupyter_text>Now, Plotting a graph to see how the 'Hours' and 'Scores' are correlated to each other.<jupyter_code>#Scatter plot of distribution

plt.scatter(df.Hours, df.Scores, color='red', marker='*')
plt.xlabel('Hours Studies')
plt.ylabel('Percentage Secured')
plt.title('Hours vs Percentage')
plt.grid(linestyle='--')
plt.show()<jupyter_output><empty_output><jupyter_text>We can see that there is a **positive linear relation** between the number of hours studied and percentage of score.  **Preparing the data:**Now, we divide the data into "features" (inputs) and "labels" (outputs).<jupyter_code>#diviving into attributes and labels

X = df.iloc[:, :-1].values  
y = df.iloc[:, 1].values<jupyter_output><empty_output><jupyter_text>Next, we split this data into training and testing sets via Scikit-Learn's built-in train_test_split() method.<jupyter_code>#dividing into training and testing

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>**Training the Algorithm:**After splitting the data we train the Algorithm.<jupyter_code>#Training the model

reg = LinearRegression()
reg.fit(X_train, y_train)
print("Training complete.")<jupyter_output>Training complete.<jupyter_text>**Plotting the Regression Line:**<jupyter_code># Plotting data
plt.scatter(X,y, color='red', marker='*')

# Plotting the regression line
line = reg.coef_*df.Hours + reg.intercept_
plt.plot(X, line)

plt.xlabel('Hours Studies')
plt.ylabel('Percentage Secured')
plt.title('Hours vs Percentage', fontsize=14)
plt.grid(linestyle='--')<jupyter_output><empty_output><jupyter_text>**Making Predictions:**Since we have trained our algorithm, we can make predictions.<jupyter_code>#Predicting values for y via x test values

y_pred = reg.predict(X_test)
y_pred
#Comparing the values of y predicted vs y actual

df = pd.DataFrame({'Y actual ': y_test, 'Y predicted': y_pred}) 
df<jupyter_output><empty_output><jupyter_text>**Required prediction:**<jupyter_code>#testing with our data

hours= 9.25
pred = reg.predict([[hours]])

print("No of Hours = {}".format(hours))
print("Predicted Score = {}".format(pred[0]))<jupyter_output>No of Hours = 9.25
Predicted Score = 93.69173248737539<jupyter_text>**Evaluting Model:**Finally, to evaluate the performance of our algorithm, we can choose any metrics from the 3 main metrics for model evaluation in regression. (i.e. R Square or Adjusted R Square, Mean Square Error (MSE) or Root Mean quare Error (RMSE), Mean Absolute Error (MAE) ) - We have chosen Mean Absolute Error.<jupyter_code>print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))<jupyter_output>Mean Absolute Error: 4.183859899002982
{'fraction_non_alphanumeric': 0.11949099937926753, 'fraction_numerical': 0.014587212911235258, 'mean_word_length': 6.7105263157894735, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '457030', 'license': 'Apache-2.0', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1057, 'n_words': 310, 'path': 'Beginner_task1.ipynb', 'repo_name': 'asmaumer/simple-linear-regression', 'size': '3222'}