File size: 2,368 Bytes
a48ebed
 
 
a012719
a48ebed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db4111
a48ebed
8db4111
 
 
 
 
a012719
 
 
 
 
 
8db4111
a012719
 
8db4111
 
a48ebed
 
 
 
 
8db4111
a48ebed
8db4111
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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

# 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:.2f} million")

    # Plotting 3D and 2D
    fig = plt.figure(figsize=(15, 7))

    # 3D plot
    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()

    # 2D plot
    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)