|
import numpy as np |
|
import streamlit as st |
|
import matplotlib.pyplot as plt |
|
from mpl_toolkits.mplot3d import Axes3D |
|
|
|
def prediction_pop_model(year, population, pred_year): |
|
n = len(year) |
|
sum_x = np.sum(year, dtype=np.float64) |
|
sum_y = np.sum(population, dtype=np.float64) |
|
sum_xy = np.sum(year * population, dtype=np.float64) |
|
sum_x_squared = np.sum(year ** 2, dtype=np.float64) |
|
|
|
slope = (n * sum_xy - sum_x * sum_y) / (n * sum_x_squared - sum_x ** 2) |
|
intercept = (sum_y - slope * sum_x) / n |
|
|
|
pred_population = slope * (pred_year) + intercept |
|
return pred_population |
|
|
|
|
|
year = np.array([1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, 1997, 2002, 2007, 2009, 2011, 2013, 2015, 2017, 2019, 2021, 2023, 2024], dtype=np.float64) |
|
population = np.array([22223309, 25009741, 28173309, 31681188, 34807417, 38783863, 45681811, 52799062, 59402198, 66134291, 73312559, 75100000, 78300000, 81500000, 85800000, 89000000, 94800000, 98900000, 102800000, 106100000, 107300000], dtype=np.float64) |
|
|
|
|
|
st.title("Population Prediction Model") |
|
|
|
|
|
input_year = st.number_input("Enter the year you want to predict the population for:", min_value=1950, max_value=2100, step=1, value=2024) |
|
|
|
|
|
if st.button("Predict"): |
|
predicted_population = prediction_pop_model(year, population, input_year) |
|
st.write(f"Predicted Population for {input_year}: {predicted_population:.2f} million") |
|
|
|
|
|
fig = plt.figure(figsize=(15, 7)) |
|
|
|
|
|
ax = fig.add_subplot(121, projection='3d') |
|
ax.scatter(year, population, zs=0, zdir='z', color='blue', label='Actual Population') |
|
ax.plot(year, population, zs=0, zdir='z', color='blue') |
|
ax.scatter([input_year], [predicted_population], zs=0, zdir='z', color='red', label='Predicted Population') |
|
ax.set_xlabel('Year') |
|
ax.set_ylabel('Population') |
|
ax.set_zlabel('Values') |
|
ax.set_title('Population Prediction (3D)') |
|
ax.legend() |
|
|
|
|
|
plt.subplot(122) |
|
plt.scatter(year, population, color='blue', label='Actual Population') |
|
plt.plot(year, population, color='blue') |
|
plt.scatter(input_year, predicted_population, color='red', label='Predicted Population') |
|
plt.xlabel('Year') |
|
plt.ylabel('Population') |
|
plt.title('Population Prediction (2D)') |
|
plt.legend() |
|
|
|
st.pyplot(fig) |
|
|