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

Regression

The document provides a Python implementation of simple linear regression using the sklearn library. It includes code for creating a dataset, fitting a linear regression model, making predictions, and visualizing the results with a plot. Additionally, it explains the relationship between the independent variable X and the dependent variable Y, along with the model parameters such as intercept and slope.

Uploaded by

puplupattnaik
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)
6 views2 pages

Regression

The document provides a Python implementation of simple linear regression using the sklearn library. It includes code for creating a dataset, fitting a linear regression model, making predictions, and visualizing the results with a plot. Additionally, it explains the relationship between the independent variable X and the dependent variable Y, along with the model parameters such as intercept and slope.

Uploaded by

puplupattnaik
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

Excel by Python

Regression

Simple Regression

import pandas as pd

import numpy as np

from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

# Sample dataset

data = {

"X": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

"Y": [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

# Create a DataFrame from the dataset

df = pd.DataFrame(data)

# Split the dataset into X (independent variable) and Y (dependent variable)

X = df[["X"]]

Y = df["Y"]

# Create a linear regression model

model = LinearRegression()

# Fit the model

model.fit(X, Y)

# Make predictions

Y_pred = model.predict(X)
# Plot the data and regression line

plt.scatter(X, Y, label="Data")

plt.plot(X, Y_pred, color='red', label="Linear Regression")

plt.xlabel("X")

plt.ylabel("Y")

plt.legend()

plt.show()

# Print the model parameters

print("Intercept:", model.intercept_)

print("Slope (Coefficient):", model.coef_[0])

# Predict Y for a new X value (e.g., X = 11)

new_X = np.array([[11]])

predicted_Y = model.predict(new_X)

print("Predicted Y for X = 11:", predicted_Y[0])

TABLE

If it is a simple 1 variable regression with X as the independent variable and Y as dependent variable.

Y=mx+c

c is the Y intercept

In this case, we will have the X and corresponding Y values,

Sr No Step Description

You might also like