antitheft159 commited on
Commit
0cfa379
·
verified ·
1 Parent(s): 6026b83

Upload 2247_159_252 (1).py

Browse files
Files changed (1) hide show
  1. 2247_159_252 (1).py +100 -0
2247_159_252 (1).py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """2247.159.252
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1zzXHR2L7JfiEtViUgooXP5iYBIeu2gkf
8
+ """
9
+
10
+ import warnings
11
+ import numpy as np
12
+ import pandas as pd
13
+ import plotly.express as px
14
+ from sklearn.linear_model import LinearRegression
15
+ from sklearn.model_selection import train_test_split
16
+ from sklearn.metrics import r2_score, mean_absolute_error, mean_squared_error
17
+ warnings.filterwarnings('ignore')
18
+
19
+ df = pd.read_csv('/content/Wprld population growth rate by cities 2024.csv')
20
+
21
+ df.info()
22
+
23
+ df.describe().T
24
+
25
+ df.head()
26
+
27
+ df.isnull().sum().sort_values(ascending=False)
28
+
29
+ df['Continent'].unique()
30
+
31
+ df[df['Continent'].isnull()]
32
+
33
+ df.duplicated().any()
34
+
35
+ df.loc[df['Continent'] == 'Oceana', 'Continent'] = 'Oceania'
36
+
37
+ con = ['North America', 'Africa', 'Europe', 'Africa', 'Europe', 'Africa', 'Europe', 'Africa', 'Europe', 'Europe', 'Europe']
38
+ ind = df[df['Continent'].isnull()].index
39
+
40
+ df['Count'] = 1
41
+
42
+ fig = px.pie(df, names='Continent', values='Count')
43
+ fig.update_layout(legend_title='Continent', title={'text': 'Distribution of Continents', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
44
+ fig.show()
45
+
46
+ for c in df['Continent'].unique():
47
+ 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')
48
+ 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')
49
+ fig.show()
50
+
51
+ for n in ['Population (2023)', 'Population (2024)', 'Growth Rate']:
52
+ fig = px.histogram(df, x=n, y="Count",
53
+ marginal="box",
54
+ hover_data=df.columns)
55
+ fig.update_layout(title={'text': f'Distribution of {n}', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}, yaxis_title='Sum of Count')
56
+ fig.show()
57
+
58
+ country = df.groupby('Country', as_index=False)[['Growth Rate']].mean()
59
+ city = df.groupby('City', as_index=False).agg({'Population (2024)': 'sum', 'Growth Rate': 'mean'})
60
+
61
+ fig = px.box(df, x='Continent', y='Growth Rate', color='Continent', color_discrete_sequence=px.colors.qualitative.Dark24)
62
+ fig.update_layout(title={'text': 'Growth Rate by Continent','y':0.95,'x':0.5,'xanchor': 'center','yanchor': 'top'})
63
+ fig.show()
64
+
65
+ 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)
66
+ fig.update_layout(title={'text': 'Top 10 Countries with Highest Average Growth Rate', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
67
+ fig.show()
68
+
69
+ 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)
70
+ fig.update_layout(title={'text': 'Top 10 Cities with Highest Averate Growth Rate', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
71
+ fig.show()
72
+
73
+ fig = px.bar(country.sort_values('Growth Rate')[:10], x='Country', y='Growth Rate', color='Country', color_discrete_sequence=px.colors.qualitative.Dark24)
74
+ fig.update_layout(title={'text': 'Top 10 Countries with Lowest Growth Rate', 'y':0.95, 'x':0.5, 'xanchor': 'center','yanchor': 'top'})
75
+ fig.show()
76
+
77
+ 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)
78
+ fig.update_layout(title={'text': 'Top 5 Cities with Highest Population (2024)', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
79
+ fig.show()
80
+
81
+ fig = px.bar(city.sort_values('Population (2024)') [:5], x='City', y='Population (2024)', color='City', color_discrete_sequence=px.colors.qualitative.Dark24)
82
+ fig.update_layout(title={'text': 'Top 5 Cities with Lowest Population (2024)', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
83
+ fig.show()
84
+
85
+ df_num = df.drop(columns=['Count']).select_dtypes(include=np.number)
86
+ fig = px.imshow(df_num.corr())
87
+ fig.update_layout(title={'text': 'Correlation Between Numerical Attributes', 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
88
+ fig.show()
89
+
90
+ X = df[['Population (2023)', 'Growth Rate']]
91
+ y = df['Population (2024)']
92
+
93
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
94
+
95
+ model = LinearRegression()
96
+ model.fit(X_train, y_train)
97
+ y_pred = model.predict(X_test)
98
+ print(f'R2 Score: {round(r2_score(y_test, y_pred)*100,2)}%')
99
+ print(f'Mean Absolute Error: {round(mean_absolute_error(y_test, y_pred), 2)}')
100
+ print(f'Root Mean Squared Error: {round(np.sqrt(mean_squared_error(y_test, y_pred)), 2)}\n')