<jupyter_start><jupyter_text>Simple Linear Regression Problem with 2 variables - Predict the percentage of marks that a student is expected to score based upon the number of hours they studied.<jupyter_code># Import python libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Import the cvs dataset using pandas
dataset = pd.read_csv("./data/student_scores.csv")
dataset.head()
# Returns number of rows and columns of dataset
dataset.shape
# Print statistical details of the dataset
dataset.describe()
dataset.plot(x='Hours', y='Scores', style='o')
plt.title('Hours vs Percentage')
plt.xlabel('Hours studied')
plt.ylabel('Percentage score')
plt.show()

# there is a positive linear relation between the number of hours studied and percentage of score.
# In a positive linear relationship, the increase in the independent variable also increases the levels of the dependent 
# variable. In a negative linear relationship, as the independent variable increases or decreases, 
# the dependent variable moves in the opposite way.
# Extract attributes and labels
# X = attributes. Specified "-1" as the range for columns since we wanted our attribute set to contain all the columns except the last one, which is "Scores"
X = dataset.iloc[:,:-1].values 

# y = labels. Specified "1" for the label column since the index for "Scores" column is 1. 
y = dataset.iloc[:, 1].values
# Split this data into training and test sets
from sklearn.model_selection import train_test_split
# test_size variable is where we specify the proportion of test set
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Training the algorithm
# Import the LinearRegression class
from sklearn.linear_model import LinearRegression

# Instantiate it
regressor = LinearRegression()

# Call the fit() method along with our training data.
regressor.fit(X_train, y_train)

# Retrieve the intercept
print(regressor.intercept_)

# Retrieve the slope
print(regressor.coef_)
# Making predictions
# y_pred is a numpy array that contains all the predicted values for the input values in the X_test series.
y_pred = regressor.predict(X_test)
# To compare the actual output values for X_test with the predicted values
df = pd.DataFrame({'Actual': y_test, 'Predicted': y_pred})
df
# Evaluate the algorithm

from sklearn import metrics

# For regression algorithms, three evaluation metrics are commonly used
# Mean Absolute Error (MAE) is the mean of the absolute value of the errors
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))

# Mean Squared Error (MSE) is the mean of the squared errors 
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))

# Root Mean Squared Error (RMSE) is the square root of the mean of the squared errors
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.598769307217406
Root Mean Squared Error: 4.647447612100367
{'fraction_non_alphanumeric': 0.07201850016518005, 'fraction_numerical': 0.0191608853650479, 'mean_word_length': 5.415254237288136, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '216417', 'license': 'Apache-2.0', 'n_tokens_mistral': 921, 'n_tokens_neox': 841, 'n_words': 376, 'path': 'regression/SimpleLinearRegression.ipynb', 'repo_name': 'surmahajan/jupyter', 'size': '3027'}