eyad222's picture
Create app.py
9cda215 verified
import numpy as np
import streamlit as st
import matplotlib.pyplot as plt
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
# Data
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)
# Streamlit Interface
st.title("Population Prediction Model")
# Input year
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)
# Predict button
if st.button("Predict"):
predicted_population = prediction_pop_model(year, population, input_year)
st.write(f"Predicted Population for {input_year}: {predicted_population}")
# Plotting
plt.figure(figsize=(10, 5))
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')
plt.legend()
st.pyplot(plt.gcf())