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

Ds Prac 6

The document provides a Python implementation of simple and multiple linear regression using synthetic data. It demonstrates data preparation, model training, prediction, and evaluation metrics such as mean squared error and R-squared. Additionally, it includes visualizations for the simple regression results but notes the difficulty of plotting for multiple regression.

Uploaded by

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

Ds Prac 6

The document provides a Python implementation of simple and multiple linear regression using synthetic data. It demonstrates data preparation, model training, prediction, and evaluation metrics such as mean squared error and R-squared. Additionally, it includes visualizations for the simple regression results but notes the difficulty of plotting for multiple regression.

Uploaded by

pandeydimple422
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

--------Simple linear regression

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Example dataset
data = {'X': np.random.rand(100), 'Y': np.random.rand(100)}
df = pd.DataFrame(data)

# Plot initial data


plt.scatter(df['X'], df['Y'])
plt.title('Dataset')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

# Prepare data for regression


X = df[['X']] # Features
Y = df['Y'] # Target variable

# Split the data into training/testing sets


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2,
random_state=42)

# Create linear regression object


regr = LinearRegression()

# Train the model using the training sets


regr.fit(X_train, Y_train)

# Make predictions using the testing set


Y_pred = regr.predict(X_test)

# The coefficients
print('Coefficients:', regr.coef_)
print('Intercept:', regr.intercept_)

# The mean squared error and R-squared


print('Mean squared error:', mean_squared_error(Y_test, Y_pred))
print('Coefficient of determination (R^2):', r2_score(Y_test, Y_pred))

# Plot outputs
plt.scatter(X_test, Y_test, color='black')
plt.plot(X_test, Y_pred, color='blue', linewidth=3)
plt.title('Simple Linear Regression Results')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
--------Multiple linear regression

# Enhancing the dataset with an additional feature


df['Z'] = np.random.rand(100)

# Prepare data for regression


X = df[['X', 'Z']] # Now using two features
Y = df['Y']

# Split the data into training/testing sets


X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2,
random_state=42)

# Create linear regression object


regr_multi = LinearRegression()

# Train the model using the training sets


regr_multi.fit(X_train, Y_train)

# Make predictions using the testing set


Y_pred = regr_multi.predict(X_test)

# The coefficients
print('Coefficients:', regr_multi.coef_)
print('Intercept:', regr_multi.intercept_)

# The mean squared error and R-squared


print('Mean squared error:', mean_squared_error(Y_test, Y_pred))
print('Coefficient of determination (R^2):', r2_score(Y_test, Y_pred))

# Note: We cannot plot a 3D plane easily here, so we'll skip the plot for the
multiple regression.

You might also like