Regresi dengan Python

 Pada postingan Analisis Tren  dapat simika penjelasan dan contoh dan penjelasan tentang analisis tren dengan algoritma regresi.

Berikut adalah sintaks analisis regresi menggunakan python

Regersi dnegan Python

Regresi dengan Data Random


import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

# Generate 'random' data
np.random.seed(0)
X = 2.5 * np.random.randn(100) + 1.5   # Array of 100 values with mean = 1.5, stddev = 2.5
res = 0.5 * np.random.randn(100)       # Generate 100 residual terms
y = 2 + 0.3 * X + res                  # Actual values of Y

# Create pandas dataframe to store our X and y values
df = pd.DataFrame(
    {'X': X,
     'y': y}
)

# Show the first five rows of our dataframe
df.head()

Regresi Sederhana


# Calculate the mean of X and y
xmean = np.mean(X)
ymean = np.mean(y)

# Calculate the terms needed for the numator and denominator of beta
df['xycov'] = (df['X'] - xmean) * (df['y'] - ymean)
df['xvar'] = (df['X'] - xmean)**2

# Calculate beta and alpha
beta = df['xycov'].sum() / df['xvar'].sum()
alpha = ymean - (beta * xmean)
print(f'alpha = {alpha}')
print(f'beta = {beta}')

ypred = alpha + beta * X      # fungsi prediksi

# Plot regression against actual data
plt.figure(figsize=(12, 6))
plt.plot(X, ypred)     # regression line
plt.plot(X, y, 'ro')   # scatter plot showing actual data
plt.title('Actual vs Predicted')
plt.xlabel('X')
plt.ylabel('y')

plt.show()

Regresi dan Plot 3D

import plotly.graph_objects as go
import numpy as np
import pandas as pd
import statsmodels.api as sm

# Menghasilkan data acak
np.random.seed(42)
x1 = np.random.randint(1, 51, 50)  # Angka acak untuk x1
x2 = np.random.randint(3, 61, 50)  # Angka acak untuk x2
y = 10 + 2 * x1 + 3 * x2 + np.random.normal(0, 10, 50)  # Hubungan linear dengan noise

# Membuat DataFrame
df = pd.DataFrame({
    "x1": x1,
    "x2": x2,
    "y": y
})

# Analisis regresi berganda
X = sm.add_constant(df[["x1", "x2"]])  # Menambahkan konstanta untuk intersep
model = sm.OLS(df["y"], X).fit()  # Fit model regresi

# Mendapatkan koefisien regresi
koefisien = model.params

# Scatter plot 3D
scatter_plot = go.Scatter3d(
    x=df["x1"],
    y=df["x2"],
    z=df["y"],
    mode="markers",
    marker=dict(
        size=5,
        color="blue",
        opacity=0.8
    ),
    name="Data"
)

# Membuat grid untuk permukaan regresi
x1_grid, x2_grid = np.meshgrid(np.linspace(1, 50, 10), np.linspace(3, 60, 10))
y_pred = koefisien[0] + koefisien[1] * x1_grid + koefisien[2] * x2_grid

# Permukaan regresi 3D
regression_surface = go.Surface(
    x=x1_grid,
    y=x2_grid,
    z=y_pred,
    colorscale="Reds",
    opacity=0.5,
    name="Garis Regresi"
)

# Membuat layout plot
layout = go.Layout(
    title="Scatter Plot 3D dengan Garis Regresi Berganda",
    scene=dict(
        xaxis_title="x1",
        yaxis_title="x2",
        zaxis_title="y"
    ),
    showlegend=True
)

# Membuat figure dengan scatter plot dan permukaan regresi
fig = go.Figure(data=[scatter_plot, regression_surface], layout=layout)

# Menampilkan plot
fig.show()