If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In this final part of the Linear Regression from scratch implementation, let's add the visualization of the model's line of best fit and the scatter plot of the data points to get a better understanding of how well the model fits the data.
To visualize the model's line of best fit, we'll use the matplotlib library in Python.
pythonCopy code
import matplotlib.pyplot as plt
def mean(values):
return sum(values) / float(len(values))
def simple_linear_regression(x, y):
# ... (same as previous implementation) ...
def predict(x, m, b):
return [m * xi + b for xi in x]
def mean_squared_error(y_true, y_pred):
# ... (same as previous implementation) ...
def gradient_descent(x, y, m, b, learning_rate, epochs):
# ... (same as previous implementation) ...
# Data preparation and model evaluation
def train_test_split(x, y, test_size=0.2):
# ... (same as previous implementation) ...
# Example usage:
x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 2, 3]
# Split the data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
# Find coefficients using simple linear regression on the training set
m, b = simple_linear_regression(x_train, y_train)
# Make predictions on the test set
predictions = predict(x_test, m, b)
# Calculate Mean Squared Error (MSE) on the test set
mse_test = mean_squared_error(y_test, predictions)
print("Mean Squared Error (MSE) on Test Set:", mse_test)
# Apply Gradient Descent to further optimize the model on the training set
learning_rate = 0.01
epochs = 1000
m_optimized, b_optimized = gradient_descent(x_train, y_train, m, b, learning_rate, epochs)
print("Optimized Slope (m):", m_optimized)
print("Optimized Y-Intercept (b):", b_optimized)
# Visualization of the Model's Line of Best Fit and Data Points
plt.scatter(x_train, y_train, color='blue', label='Training Data')
plt.scatter(x_test, y_test, color='green', label='Testing Data')
plt.plot(x_train, predict(x_train, m_optimized, b_optimized), color='red', label='Model')
plt.xlabel('Independent Variable (x)')
plt.ylabel('Dependent Variable (y)')
plt.title('Linear Regression from Scratch')
plt.legend()
plt.show()
In this final part, we used the matplotlib.pyplot
module to create a scatter plot of the training and testing data points, and we plotted the line of best fit generated by the linear regression model on the training data.
The scatter plot shows the distribution of the data points, and the red line represents the line of best fit obtained from the trained model. By visualizing the model's line, we can get an idea of how well the model fits the data and how accurately it captures the relationship between the variables.
Keep in mind that this is a basic implementation of linear regression for educational purposes. In real-world scenarios, libraries like scikit-learn should be used for more sophisticated tasks, such as cross-validation, hyperparameter tuning, and handling more complex regression problems. Additionally, other visualization techniques and model evaluation metrics may be applied for a comprehensive analysis of the model's performance.
Comments: 0