LR

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

# Import necessary libraries

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error, r2_score

# Example: Load data

# Replace 'your_dataset.csv' with the path to your dataset

data = pd.read_csv('your_dataset.csv')

# Assume 'target_column' is the column we want to predict

X = data.drop(columns=['target_column']) # Features

y = data['target_column'] # Target variable

# Step 1: Splitting the dataset 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)

# Step 2: Build the Linear Regression model

model = LinearRegression()

# Step 3: Train the model

model.fit(X_train, y_train)

# Step 4: Predictions on the test set

y_pred = model.predict(X_test)

# Step 5: Evaluate the model

mse = mean_squared_error(y_test, y_pred)


rmse = np.sqrt(mse)

r2 = r2_score(y_test, y_pred)

# Print the results

print(f'Mean Squared Error: {mse}')

print(f'Root Mean Squared Error: {rmse}')

print(f'R-squared: {r2}')

# Optional: Print the model coefficients and intercept

print("Model Coefficients: ", model.coef_)

print("Model Intercept: ", model.intercept_)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy