Seperti kita sudah pahami Regresi Linear adalah metode statistik yang digunakan untuk memodelkan hubungan antara variabel dependen (misalnya, waktu rekor dunia) dan satu atau lebih variabel independen (misalnya, tahun). Tujuannya adalah untuk menemukan "garis yang paling sesuai" yang meminimalkan kesalahan antara nilai yang diprediksi dan nilai aktual.
Anda dapat menggunakan persamaan garis lurus y=mx+b, di mana:
- y: Variabel dependen (waktu rekor dunia)
- x: Variabel independen (tahun)
- m: Kemiringan (laju perubahan waktu rekor dunia per tahun)
- b: Intersep (waktu rekor dunia yang diprediksi pada tahun 0)
YEAR | TIME (SECONDS) |
---|---|
1912 | 10.6 |
1921 | 10.4 |
1930 | 10.3 |
1936 | 10.2 |
1956 | 10.1 |
1960 | 10.0 |
1968 | 9.95 |
1983 | 9.93 |
1988 | 9.92 |
1991 | 9.86 |
1994 | 9.85 |
1996 | 9.84 |
1999 | 9.79 |
2005 | 9.77 |
2008 | 9.72 |
2009 | 9.58 |
- Batasan Biologis dan Fisik: Performa manusia memiliki batasan alami karena biologi, fisiologi, dan fisika. Model linier mengasumsikan peningkatan tak terbatas, yang tidak realistis.
- Tren Nonlinier: Peningkatan rekor dunia cenderung melambat seiring waktu saat atlet mendekati batas fisik mereka. Model linier tidak dapat menangkap tren perlambatan ini.
- Objek Luar Biasa dan Kejutan: Terkadang, ada objek luar biasa (misalnya, rekor Usain Bolt 9,58 detik pada tahun 2009). Ini dapat mendistorsi model linier dan membuatnya kurang akurat.
- Mengapa regresi linier gagal memprediksi rekor dunia di masa mendatang secara akurat?
- Faktor apa yang memengaruhi perkembangan rekor dunia (misalnya, metode pelatihan, teknologi, genetika)?
- Dapatkah kita memprediksi kapan batas akhir kinerja manusia akan tercapai? Mengapa atau mengapa tidak?
- Regresi linier adalah alat yang ampuh untuk memodelkan hubungan tetapi memiliki keterbatasan.
- Data dunia nyata sering kali menunjukkan pola kompleks yang tidak dapat ditangkap oleh model linier.
- Memahami keterbatasan ini sangat penting untuk membuat prediksi yang tepat.
import matplotlib.pyplot as pltimport numpy as np# Historical datayears = [1912, 1921, 1930, 1936, 1956, 1960, 1968, 1983, 1988, 1991, 1994, 1996, 1999, 2005, 2008, 2009]times = [10.6, 10.4, 10.3, 10.2, 10.1, 10.0, 9.95, 9.93, 9.92, 9.86, 9.85, 9.84, 9.79, 9.77, 9.72, 9.58]# Linear regression modelm = -0.01 # Slopeb = 29.5 # Intercept# Generate predicted valuesfuture_years = np.linspace(1900, 3000, 10)predicted_times = m * future_years + b# Plotplt.figure(figsize=(10, 6))plt.scatter(years, times, color='blue', label='Actual World Records')plt.plot(future_years, predicted_times, color='red', label='Linear Regression Prediction')plt.axhline(0, color='black', linestyle='--', linewidth=1) # Horizontal line at y=0plt.title("Failure of Linear Regression: Men's 100m World Record")plt.xlabel("Year")plt.ylabel("World Record Time (seconds)")plt.legend()plt.grid(True)plt.ylim(-1, 12) # Set y-axis limits to show negative predictionsplt.show()
#ini coba parkirimport numpy as npimport matplotlib.pyplot as pltfrom sklearn.linear_model import LinearRegression# Step 1: Sample Data (Monthly Parking Lot Occupancy)months = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).reshape(-1, 1) # Month numbers (1 = January, 12 = December)occupancy = np.array([80, 75, 65, 70, 62, 55, 45, 52, 45, 52, 68, 72]) # Occupancy percentages# Step 2: Fit Linear Regression Modelmodel = LinearRegression()model.fit(months, occupancy)# Extract slope (m) and intercept (b)m = model.coef_[0] # Slopeb = model.intercept_ # Interceptprint(f"Linear Regression Model: y = {m:.2f} * x + {b:.2f}")# Step 3: Predict Occupancy for Next Year (Months 13 to 24)next_year_months = np.array(range(13, 25)).reshape(-1, 1) # Months for the next yearpredicted_occupancy = model.predict(next_year_months)# Print Predictionsfor month, pred in zip(range(13, 25), predicted_occupancy):print(f"Month {month}: Predicted Occupancy = {pred:.2f}%")# Step 4: Visualize Resultsplt.figure(figsize=(10, 6))# Plot Historical Dataplt.scatter(months, occupancy, color='blue', label='Historical Data')# Plot Regression Line for Historical Datax_range = np.array(range(1, 25)).reshape(-1, 1) # Extend line to cover both yearsy_range = model.predict(x_range)plt.plot(x_range, y_range, color='red', label='Linear Regression Fit')# Plot Predictions for Next Yearplt.scatter(next_year_months, predicted_occupancy, color='green', label='Next Year Predictions')# Add Labels and Legendplt.title("Parking Lot Occupancy Prediction")plt.xlabel("Month Number")plt.ylabel("Occupancy (%)")plt.axvline(x=12.5, color='gray', linestyle='--', linewidth=1, label='End of First Year') # Separator between yearsplt.legend()plt.grid(True)plt.show()