0% found this document useful (0 votes)
9 views2 pages

LR

Uploaded by

husnainchohan298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

LR

Uploaded by

husnainchohan298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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