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