0% found this document useful (0 votes)
31 views

Tugas2 Regresi Linear Berganda - Ipynb - Colab

Uploaded by

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

Tugas2 Regresi Linear Berganda - Ipynb - Colab

Uploaded by

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

import pandas as pd

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# 1. Pengumpulan Data
data = {
"Iklan": [10, 15, 20, 25, 30, 35, 40, 45, 50, 55],
"Penjualan": [200, 250, 300, 350, 400, 450, 500, 550, 600, 650],
"Jumlah_Barang": [2000, 2500, 2200, 2100, 2300, 2400, 2600, 2500, 2700, 2800]
}
df = pd.DataFrame(data)

# 2. Persiapan Data dan EDA#


# A. Statistik Dasar
print(df.describe())

## B. Visualisasi: Pairplot
plt.figure(figsize=(12, 8))
sns.pairplot(df)
plt.suptitle("Pairplot of Features", y=1.02)
plt.show()

## C. Visualisasi: Heatmap Korelasi


plt.figure(figsize=(10, 6))
sns.heatmap(df.corr(), annot=True, cmap='coolwarm')
plt.title("Heatmap of Feature Correlation")
plt.show()

# 3. Pemilihan dan Pelatihan Model menggunakan semua data


X = df[['Iklan', 'Penjualan']]
y = df['Jumlah_Barang']
model = LinearRegression()
model.fit(X, y)

# Menampilkan koefisien dan intercept


intercept = model.intercept_
coef_Iklan = model.coef_[0]
coef_Penjualan = model.coef_[1]
print("Intercept:", intercept)
print("Koefisien Iklan:", coef_Iklan)
print("Koefisien Penjualan:", coef_Penjualan)

# Membuat Prediksi dengan Data Baru


new_data = pd.DataFrame([[5500, 12000]], columns=['Iklan', 'Penjualan']) # Contoh data b
predicted_Jumlah_Barang = model.predict(new_data)
print(f"Prediksi Jumlah Barang untuk Iklan-10 dan Penjualan-200: {predicted_Jumlah_Baran

# Evaluasi Model menggunakan semua data


predictions = model.predict(X)
mse = mean_squared_error(y, predictions)
rmse = np.sqrt(mse)

print("Mean Squared Error (MSE):", mse)


print("Root Mean Squared Error (RMSE):", rmse)

# Visualisasi Prediksi vs Aktual


plt.figure(figsize=(10, 6))
plt.scatter(y, predictions, color='red', label='Predicted')
plt.plot([y.min(), y.max()], [y.min(), y.max()], 'k--', lw=2, label='Actual')
plt.xlabel("Ad Serving")
plt.ylabel("Predicted Penjualan")
plt.title("Serving vs Predicted Penjualan")
plt.legend()
plt.show()
Iklan Penjualan Jumlah_Barang
count 10.000000 10.000000 10.000000
mean 32.500000 425.000000 2410.000000
std 15.138252 151.382518 260.128174
min 10.000000 200.000000 2000.000000
25% 21.250000 312.500000 2225.000000
50% 32.500000 425.000000 2450.000000
75% 43.750000 537.500000 2575.000000
max 55.000000 650.000000 2800.000000
<Figure size 1200x800 with 0 Axes>

You might also like