|
import warnings |
|
import numpy as np |
|
import pandas as pd |
|
import plotly.express as px |
|
from sklearn.linear_model import LinearRegression |
|
from sklearn.model_selection import train_test_split |
|
from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error |
|
warnings.filterwarnings('ignore') |
|
|
|
df = pd.read_csv('/content/Wprld population growth rate by cities 2024.csv') |
|
|
|
df.info() |
|
|
|
df.describe().T |
|
|
|
df.head() |
|
|
|
df.isnull().sum().sort_values(ascending=False) |
|
|
|
df['Continent'].unique() |
|
|
|
df[df['Continent'].isnull()] |
|
|
|
df.duplicated().any() |
|
|
|
df.loc[df['Continent'] == 'Oceana', 'Continent'] = 'Oceania' |
|
|
|
con = ['North America', 'Africa', 'Europe', 'Africa', 'Europe', 'Africa', 'Europe', 'Africa', 'Europe', 'Europe', 'Europe'] |
|
ind = df[df['Continent'].isnull()].index |
|
|
|
df['Count'] = 1 |
|
|
|
fig = px.pie(df, names='Continent', values='Count') |
|
fig.update_layout(legend_title='Continent', title={'text': 'Distribution of Continents', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}) |
|
fig.show() |
|
|
|
for c in df['Continent'].unique(): |
|
fig = px.histogram(df[df['Continent'] == c], x='Count', y='Country', color='Country', color_discrete_sequence=px.colors.qualitative.Dark24).update_yaxes(categoryorder='total ascending') |
|
fig.update_layout(title={'text': f'Distribution of Country in {c}', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}, xaxis_title='Sum of Count') |
|
fig.show() |
|
|
|
for n in ['Population (2023)', 'Population (2024)', 'Growth Rate']: |
|
fig = px.histogram(df, x=n, y="Count", |
|
marginal="box", |
|
hover_data=df.columns) |
|
fig.update_layout(title={'text': f'Distribution of {n}', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}, yaxis_title='Sum of Count') |
|
fig.show() |
|
|
|
country = df.groupby('Country', as_index=False)[['Growth Rate']].mean() |
|
city = df.groupby('City', as_index=False).agg({'Population (2024)': 'sum', 'Growth Rate': 'mean'}) |
|
|
|
fig = px.box(df, x='Continent', y='Growth Rate', color='Continent', color_discrete_sequence=px.colors.qualitative.Dark24) |
|
fig.update_layout(title={'text': 'Growth Rate by Continent','y':0.95,'x':0.5,'xanchor': 'center','yanchor': 'top'}) |
|
fig.show() |
|
|
|
fig = px.bar(country.sort_values('Growth Rate', ascending=False)[:10], x='Country', y='Growth Rate', color='Country', color_discrete_sequence=px.colors.qualitative.Dark24) |
|
fig.update_layout(title={'text': 'Top 10 Countries with Highest Average Growth Rate', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}) |
|
fig.show() |
|
|
|
fig = px.bar(city.sort_values('Growth Rate', ascending=False)[:10], x='City', y='Growth Rate', color='City', color_discrete_sequence=px.colors.qualitative.Dark24) |
|
fig.update_layout(title={'text': 'Top 10 Cities with Highest Averate Growth Rate', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}) |
|
fig.show() |
|
|
|
fig = px.bar(country.sort_values('Growth Rate')[:10], x='Country', y='Growth Rate', color='Country', color_discrete_sequence=px.colors.qualitative.Dark24) |
|
fig.update_layout(title={'text': 'Top 10 Countries with Lowest Growth Rate', 'y':0.95, 'x':0.5, 'xanchor': 'center','yanchor': 'top'}) |
|
fig.show() |
|
|
|
fig = px.bar(city.sort_values('Population (2024)', ascending=False)[:5], x='City', y='Population (2024)', color='City', color_discrete_sequence=px.colors.qualitative.Dark24) |
|
fig.update_layout(title={'text': 'Top 5 Cities with Highest Population (2024)', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}) |
|
fig.show() |
|
|
|
fig = px.bar(city.sort_values('Population (2024)') [:5], x='City', y='Population (2024)', color='City', color_discrete_sequence=px.colors.qualitative.Dark24) |
|
fig.update_layout(title={'text': 'Top 5 Cities with Lowest Population (2024)', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}) |
|
fig.show() |
|
|
|
df_num = df.drop(columns=['Count']).select_dtypes(include=np.number) |
|
fig = px.imshow(df_num.corr()) |
|
fig.update_layout(title={'text': 'Correlation Between Numerical Attributes', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}) |
|
fig.show() |
|
|
|
X = df[['Population (2023)', 'Growth Rate']] |
|
y = df['Population (2024)'] |
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
|
|
|
model = LinearRegression() |
|
model.fit(X_train, y_train) |
|
y_pred = model.predict(X_test) |
|
print(f'R2 Score: {round(r2_score(y_test, y_pred)*100,2)}%') |
|
print(f'Mean Absolute Error: {round(mean_absolute_error(y_test, y_pred), 2)}') |
|
print(f'Root Mean Squared Error: {round(np.sqrt(mean_squared_error(y_test, y_pred)), 2)}\n') |