<jupyter_start><jupyter_text>Simple linear regression  Import the relevant libraries<jupyter_code># For these lessons we will need NumPy, pandas, matplotlib and seaborn
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()

# and of course the actual regression (machine learning) module
from sklearn.linear_model import LinearRegression<jupyter_output><empty_output><jupyter_text>Load the data<jupyter_code># We start by loading the data
data = pd.read_csv('1.01. Simple linear regression.csv')

# Let's explore the top 5 rows of the df
data.head()<jupyter_output><empty_output><jupyter_text>Create the regression  Declare the dependent and independent variables<jupyter_code># There is a single independent variable: 'SAT'
x = data['SAT']

# and a single depended variable: 'GPA'
y = data['GPA']
# Often it is useful to check the shapes of the features
x.shape
y.shape
# In order to feed x to sklearn, it should be a 2D array (a matrix)
# Therefore, we must reshape it 
# Note that this will not be needed when we've got more than 1 feature (as the inputs will be a 2D array by default)

# x_matrix = x.values.reshape(84,1)
x_matrix = x.values.reshape(-1,1)

# Check the shape just in case
x_matrix.shape<jupyter_output><empty_output><jupyter_text>Regression itselfFull documentation: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html<jupyter_code># We start by creating a linear regression object
reg = LinearRegression()
# The whole learning process boils down to fitting the regression
# Note that the first argument is the independent variable, while the second - the dependent (unlike with StatsModels)
reg.fit(x_matrix,y)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07652769845802398, 'fraction_numerical': 0.006853226727584237, 'mean_word_length': 5.7384615384615385, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '645467', 'license': 'Apache-2.0', 'n_tokens_mistral': 555, 'n_tokens_neox': 533, 'n_words': 205, 'path': '15 - Advanced Statistical Methods in Python/3_Linear Regression with sklearn/3_Simple Linear Regression with sklearn (5:38)/sklearn - Simple Linear Regression_with_comments.ipynb', 'repo_name': 'olayinka04/365-data-science-courses', 'size': '1751'}