Learning Linear Regression and Its Application In AI:

Linear regression stands as a foundation in the realm of statistics and machine learning, providing a solid support for understanding relationships between variables. In this blog post, we will discuss the how linear regression works, exploring both its use and applications in the field of artificial intelligence. Be prepared for a journey that demystifies this powerful tool and showcases its real-world utility.

At its core, linear regression models the relationship between a dependent variable (Y) and one or more independent variables (X). The fundamental equation for simple linear regression is:

Y=mx+b

Here:

The primary objective is to find the best-fitting line that minimizes the difference between predicted and actual values.

Linear Regression Code

Import Modules


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.metrics import mean_squared_error


    

Step 1: Import the necessary modules in order to run a linear regression model.

  1. Numpy: We use numpy to generate synthetic data to use. This isn’t necessary if we had real data but numpy is also useful in dealing with arrays.
  2. Matplot: Matplot is very useful for ploting our graph once we have finished processing the data.
  3. train_test_split: importing train_test_split from Scikit-learn or sklearn is used to split the data into the training set(the set AI is trained with) and the testing set(the set used to see how accurate the model is)
  4. LinearRegression: Linear regression’s purpose is to create a model to fit our data into. It also can be used to train and make predictions with the model.
  5. mean_squared_error: mean_squared_error is used to calculate how accurate our model is using this formula, where N is the number of data points, yi is the actual target value for the i-th data point and y^i is the predicted target value for the ith data point:

Step 2: Use numpy to generate synthetic data:

Linear Regression Code

Generate Data


X = 2 * np.random.rand(100,1)
y = 4 + 3 * X + np.random.rand(100,1)


    

Step 3: Split the data into training and testing sets

Linear Regression Code

Split The Data


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

    

Step 4: Create, train, and predict a linear regression model using LinearRegression from Scikit-learn

Linear Regression Code

Create, Train, and Predict


model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

    

Step 5: Visualize the results using Mat Plot

Linear Regression Code

Visualizing The Results


plt.scatter(X_test, y_test, color='blue', label='Actual Data')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Linear Regression Model')
plt.xlabel('Independent Variable (X)')
plt.ylabel('Dependent Variable (Y)')
plt.title('Linear Regression Example')
plt.legend()
plt.show()

    

Results:

Step 6: Evaluate the model

Linear Regression Code

Evaluating The Model


mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

    

Results:

  1. Mean Squared Error: 0.6536995137170021
  2. Mean Squared Error: 0.8105016597701677
  3. Mean Squared Error: 0.7652498024342076
  4. Mean Squared Error: 0.7671338506138042

Complete Code:

Linear Regression Code

Complete Code


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.metrics import mean_squared_error

X = 2 * np.random.rand(100,1)
y = 4 + 3 * X + np.random.rand(100,1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

model = LinearRegression()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)

plt.scatter(X_test, y_test, color='blue', label='Actual Data')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Linear Regression Model')
plt.xlabel('Independent Variable (X)')
plt.ylabel('Dependent Variable (Y)')
plt.title('Linear Regression Example')
plt.legend()
plt.show()

mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

    

Linear regression finds extensive use in predictive analytics, a crucial component of artificial intelligence. By training models on historical data, AI systems can predict future trends, behaviors, and outcomes. In the housing price prediction example, the trained linear regression model can be deployed in an AI system to estimate prices for new properties based on their square footage.

While linear regression is a powerful and widely-used tool, it has certain limitations:

While linear regression is a powerful and widely-used tool, it has certain limitations:

  1. Assumption of Linearity: Linear regression assumes a linear relationship between the independent and dependent variables. If the relationship is not linear, the model may not perform well.
  2. Sensitivity to Outliers: Linear regression is sensitive to outliers, which can disproportionately influence the model’s parameters and predictions.
  3. Assumption of Independence: It assumes that the residuals (the differences between predicted and actual values) are independent. Violation of this assumption may lead to biased results.
  4. Limited to Linear Relationships: Linear regression is not suitable for capturing complex, non-linear relationships in data.

Linear regression, with its simplicity and effectiveness, serves as a stepping stone for more advanced machine learning techniques. Its application in AI spans various domains, from finance to healthcare. As you embark on your journey in data science and artificial intelligence, mastering linear regression will undoubtedly enhance your analytical prowess. The code example provided is just a glimpse into the vast possibilities that linear regression unlocks in the fascinating world of AI.

Michael Zhao Avatar

Posted by

Leave a Reply

Discover more from Walk In Market

Subscribe now to keep reading and get access to the full archive.

Continue reading