<jupyter_start><jupyter_text>**Regression**In the previous chapter we’ve talked about Unsupervised and SupervisedLearning, including a bit about Linear Regression. In this chapter let’s focus on Regression (predicting an output based on a new input and previous learning).Basically, Regression Analysis allows us to discover if there’s a relationship between an independent variable/s and a dependent variable (the target). For example, in a Simple Linear Regression we want to know if there’s a relationship between x and y. This is very useful in forecasting (e.g. where is the trend going) and time series modelling (e.g. temperature levels by year and if global warming is true). **Simple Linear Regression**Here we’ll be dealing with one independent variable and one dependent. Lateron we’ll be dealing with multiple variables and show how can they be used to predict the target (similar to what we talked about predicting something based on several features/attributes).For now, let’s see an example of a Simple Linear Regression wherein we analyze Salary Data (Salary_Data.csv). Here’s the dataset (comma-separated values and the columns are years, experience, and salary): YearsExperience,Salary1.1,39343.001.3,46205.001.5,37731.002.0,43525.002.2,39891.002.9,56642.003.0,60150.003.2,54445.003.2,64445.003.7,57189.003.9,63218.004.0,55794.004.0,56957.004.1,57081.004.5,61111.004.9,67938.005.1,66029.005.3,83088.005.9,81363.006.0,93940.006.8,91738.007.1,98273.007.9,101302.008.2,113812.008.7,109431.009.0,105582.009.5,116969.009.6,112635.0010.3,122391.0010.5,121872.00<jupyter_code>#Here’s the Python code for fitting Simple Linear Regression to the Training Set:
# Importing the libraries
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('/content/Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 1].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 1/3,
random_state = 0)

# Fitting Simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

# Predicting the Test set results
y_pred = regressor.predict(X_test)

# Visualising the Training set results
plt.scatter(X_train, y_train, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Training set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
#After the line is created, we then apply that same line to the Test Set (the
#remaining 30% or 1/3 of the dataset).
# Visualising the Test set results
plt.scatter(X_test, y_test, color = 'red')
plt.plot(X_train, regressor.predict(X_train), color = 'blue')
plt.title('Salary vs Experience (Test set)')
plt.xlabel('Years of Experience')
plt.ylabel('Salary')
plt.show()
#The overall goal here is to create a model that will predict Salary
#based on Years of Experience. First, we create a model using the Training Set
#(70% of the dataset). It will then fit a line that is close as possible with most of
#the data points.
#Notice that the line performed well both on the Training Set and the Test Set. As
#a result, there’s a good chance that the line or our model will also perform well
#on new data.<jupyter_output><empty_output><jupyter_text>Let’s have a recap of what happened. First, we imported the necessary libraries (pandas for processing data, matplotlib for data visualization). Next, we imported the dataset and assigned X (the independent variable) to Years of Experience and y (the target) to Salary. We then split the dataset into Training Set (2⁄3) and Test Set (1⁄3). Then, we apply the Linear Regression model and fitted a line (with the help ofscikit-learn, which is a free software machine learning library for the Python programming language). This is accomplished through the following lines of code: from sklearn.linear_model import LinearRegressionregressor = LinearRegression() regressor.fit(X_train, y_train) After learning from the Training Set (X_train and y_train), we then apply that regressor to the Test Set (X_test) and compare the results using data visualization (matplotlib). It’s a straightforward approach. Our model learns from the Training Set and then applies that to the Test Set (and see if the model is good enough). This is the essential principle of Simple Linear Regression.<jupyter_code>import math
math.fsum([8/2*(2 + 2)])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0840958605664488, 'fraction_numerical': 0.06514161220043573, 'mean_word_length': 5.358725761772853, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '555155', 'license': 'MIT', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1351, 'n_words': 570, 'path': 'Simple_Linearregression203.ipynb', 'repo_name': 'gndede/python', 'size': '4590'}