Skip to content

Commit

Permalink
Create linear_regression.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 19, 2024
1 parent 83e2a5f commit 5f32201
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline

class LinearRegressionModel:
def __init__(self, data, target_variable, test_size=0.2, random_state=42):
self.data = data
self.target_variable = target_variable
self.test_size = test_size
self.random_state = random_state
self.model = None
self.X_train = None
self.X_test = None
self.y_train = None
self.y_test = None

def preprocess_data(self):
# Drop missing values
self.data.dropna(inplace=True)

# Scale features using StandardScaler
scaler = StandardScaler()
self.data[['feature1', 'feature2', ...]] = scaler.fit_transform(self.data[['feature1', 'feature2', ...]])

# Split data into training and testing sets
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(self.data.drop([self.target_variable], axis=1),
self.data[self.target_variable],
test_size=self.test_size,
random_state=self.random_state)

def train_model(self):
# Create pipeline with linear regression model
pipeline = Pipeline([
('linear_regression', LinearRegression())
])

# Train model
pipeline.fit(self.X_train, self.y_train)

# Set model
self.model = pipeline

def evaluate_model(self):
# Make predictions on test set
y_pred = self.model.predict(self.X_test)

# Calculate mean squared error
mse = mean_squared_error(self.y_test, y_pred)

# Calculate R-squared score
r2 = r2_score(self.y_test, y_pred)

return mse, r2

def make_predictions(self, input_features):
# Make predictions using trained model
predictions = self.model.predict(input_features)

return predictions

0 comments on commit 5f32201

Please sign in to comment.