Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
machine-learnig/linear regression.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (30 sloc)
1.32 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_squared_error | |
import matplotlib.pyplot as plt | |
# Load your dataset | |
file_path = "C:/Users/wilso/OneDrive/Desktop/uk_renewable_energy.csv" | |
df = pd.read_csv(file_path) | |
X = df[['Energy from renewable & waste sources']].values | |
y = df['Total energy consumption of primary fuels and equivalents'].values | |
# 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, random_state=42) | |
# Create a linear regression model | |
model = LinearRegression() | |
# Train the model on the training set | |
model.fit(X_train, y_train) | |
# Make predictions on the test set | |
y_pred = model.predict(X_test) | |
# Evaluate the model | |
mse = mean_squared_error(y_test, y_pred) | |
print(f'Mean Squared Error: {mse}') | |
# Plot the training data and the regression line | |
plt.scatter(X_train, y_train, color='blue', label='Training Data') | |
plt.scatter(X_test, y_test, color='green', label='Test Data') | |
plt.plot(X_test, y_pred, color='red', linewidth=3, label='Regression Line') | |
plt.xlabel('Energy from renewable & waste sources') | |
plt.ylabel('Total energy consumption of primary fuels and equivalents') | |
plt.legend() | |
plt.show() |