Spaces:
Sleeping
Sleeping
Commit
·
6074a2e
1
Parent(s):
ffc3165
Added app
Browse files- Country-data.csv +168 -0
- app.py +259 -0
- data-dictionary.csv +11 -0
- model.ipynb +0 -0
- requirements.txt +6 -0
Country-data.csv
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
country,child_mort,exports,health,imports,income,inflation,life_expec,total_fer,gdpp
|
2 |
+
Afghanistan,90.2,10,7.58,44.9,1610,9.44,56.2,5.82,553
|
3 |
+
Albania,16.6,28,6.55,48.6,9930,4.49,76.3,1.65,4090
|
4 |
+
Algeria,27.3,38.4,4.17,31.4,12900,16.1,76.5,2.89,4460
|
5 |
+
Angola,119,62.3,2.85,42.9,5900,22.4,60.1,6.16,3530
|
6 |
+
Antigua and Barbuda,10.3,45.5,6.03,58.9,19100,1.44,76.8,2.13,12200
|
7 |
+
Argentina,14.5,18.9,8.1,16,18700,20.9,75.8,2.37,10300
|
8 |
+
Armenia,18.1,20.8,4.4,45.3,6700,7.77,73.3,1.69,3220
|
9 |
+
Australia,4.8,19.8,8.73,20.9,41400,1.16,82,1.93,51900
|
10 |
+
Austria,4.3,51.3,11,47.8,43200,0.873,80.5,1.44,46900
|
11 |
+
Azerbaijan,39.2,54.3,5.88,20.7,16000,13.8,69.1,1.92,5840
|
12 |
+
Bahamas,13.8,35,7.89,43.7,22900,-0.393,73.8,1.86,28000
|
13 |
+
Bahrain,8.6,69.5,4.97,50.9,41100,7.44,76,2.16,20700
|
14 |
+
Bangladesh,49.4,16,3.52,21.8,2440,7.14,70.4,2.33,758
|
15 |
+
Barbados,14.2,39.5,7.97,48.7,15300,0.321,76.7,1.78,16000
|
16 |
+
Belarus,5.5,51.4,5.61,64.5,16200,15.1,70.4,1.49,6030
|
17 |
+
Belgium,4.5,76.4,10.7,74.7,41100,1.88,80,1.86,44400
|
18 |
+
Belize,18.8,58.2,5.2,57.5,7880,1.14,71.4,2.71,4340
|
19 |
+
Benin,111,23.8,4.1,37.2,1820,0.885,61.8,5.36,758
|
20 |
+
Bhutan,42.7,42.5,5.2,70.7,6420,5.99,72.1,2.38,2180
|
21 |
+
Bolivia,46.6,41.2,4.84,34.3,5410,8.78,71.6,3.2,1980
|
22 |
+
Bosnia and Herzegovina,6.9,29.7,11.1,51.3,9720,1.4,76.8,1.31,4610
|
23 |
+
Botswana,52.5,43.6,8.3,51.3,13300,8.92,57.1,2.88,6350
|
24 |
+
Brazil,19.8,10.7,9.01,11.8,14500,8.41,74.2,1.8,11200
|
25 |
+
Brunei,10.5,67.4,2.84,28,80600,16.7,77.1,1.84,35300
|
26 |
+
Bulgaria,10.8,50.2,6.87,53,15300,1.11,73.9,1.57,6840
|
27 |
+
Burkina Faso,116,19.2,6.74,29.6,1430,6.81,57.9,5.87,575
|
28 |
+
Burundi,93.6,8.92,11.6,39.2,764,12.3,57.7,6.26,231
|
29 |
+
Cambodia,44.4,54.1,5.68,59.5,2520,3.12,66.1,2.88,786
|
30 |
+
Cameroon,108,22.2,5.13,27,2660,1.91,57.3,5.11,1310
|
31 |
+
Canada,5.6,29.1,11.3,31,40700,2.87,81.3,1.63,47400
|
32 |
+
Cape Verde,26.5,32.7,4.09,61.8,5830,0.505,72.5,2.67,3310
|
33 |
+
Central African Republic,149,11.8,3.98,26.5,888,2.01,47.5,5.21,446
|
34 |
+
Chad,150,36.8,4.53,43.5,1930,6.39,56.5,6.59,897
|
35 |
+
Chile,8.7,37.7,7.96,31.3,19400,8.96,79.1,1.88,12900
|
36 |
+
China,15.7,26.3,5.07,22.6,9530,6.94,74.6,1.59,4560
|
37 |
+
Colombia,18.6,15.9,7.59,17.8,10900,3.86,76.4,2.01,6250
|
38 |
+
Comoros,88.2,16.5,4.51,51.7,1410,3.87,65.9,4.75,769
|
39 |
+
"Congo, Dem. Rep.",116,41.1,7.91,49.6,609,20.8,57.5,6.54,334
|
40 |
+
"Congo, Rep.",63.9,85.1,2.46,54.7,5190,20.7,60.4,4.95,2740
|
41 |
+
Costa Rica,10.2,33.2,10.9,35,13000,6.57,80.4,1.92,8200
|
42 |
+
Cote d'Ivoire,111,50.6,5.3,43.3,2690,5.39,56.3,5.27,1220
|
43 |
+
Croatia,5.5,37.6,7.76,38.1,20100,0.821,76.3,1.55,13500
|
44 |
+
Cyprus,3.6,50.2,5.97,57.5,33900,2.01,79.9,1.42,30800
|
45 |
+
Czech Republic,3.4,66,7.88,62.9,28300,-1.43,77.5,1.51,19800
|
46 |
+
Denmark,4.1,50.5,11.4,43.6,44000,3.22,79.5,1.87,58000
|
47 |
+
Dominican Republic,34.4,22.7,6.22,33.3,11100,5.44,74.6,2.6,5450
|
48 |
+
Ecuador,25.1,27.9,8.06,32.4,9350,7.47,76.7,2.66,4660
|
49 |
+
Egypt,29.1,21.3,4.66,26.6,9860,10.1,70.5,3.19,2600
|
50 |
+
El Salvador,19.2,26.9,6.91,46.6,7300,2.65,74.1,2.27,2990
|
51 |
+
Equatorial Guinea,111,85.8,4.48,58.9,33700,24.9,60.9,5.21,17100
|
52 |
+
Eritrea,55.2,4.79,2.66,23.3,1420,11.6,61.7,4.61,482
|
53 |
+
Estonia,4.5,75.1,6.03,68.7,22700,1.74,76,1.72,14600
|
54 |
+
Fiji,24.1,57.8,4.86,63.9,7350,4.23,65.3,2.67,3650
|
55 |
+
Finland,3,38.7,8.95,37.4,39800,0.351,80,1.87,46200
|
56 |
+
France,4.2,26.8,11.9,28.1,36900,1.05,81.4,2.03,40600
|
57 |
+
Gabon,63.7,57.7,3.5,18.9,15400,16.6,62.9,4.08,8750
|
58 |
+
Gambia,80.3,23.8,5.69,42.7,1660,4.3,65.5,5.71,562
|
59 |
+
Georgia,16.5,35,10.1,52.8,6730,8.55,72.8,1.92,2960
|
60 |
+
Germany,4.2,42.3,11.6,37.1,40400,0.758,80.1,1.39,41800
|
61 |
+
Ghana,74.7,29.5,5.22,45.9,3060,16.6,62.2,4.27,1310
|
62 |
+
Greece,3.9,22.1,10.3,30.7,28700,0.673,80.4,1.48,26900
|
63 |
+
Grenada,14.6,23.8,5.86,49.2,11200,0.48,71.3,2.24,7370
|
64 |
+
Guatemala,35.4,25.8,6.85,36.3,6710,5.14,71.3,3.38,2830
|
65 |
+
Guinea,109,30.3,4.93,43.2,1190,16.1,58,5.34,648
|
66 |
+
Guinea-Bissau,114,14.9,8.5,35.2,1390,2.97,55.6,5.05,547
|
67 |
+
Guyana,37.6,51.4,5.38,79.1,5840,5.73,65.5,2.65,3040
|
68 |
+
Haiti,208,15.3,6.91,64.7,1500,5.45,32.1,3.33,662
|
69 |
+
Hungary,6,81.8,7.33,76.5,22300,2.33,74.5,1.25,13100
|
70 |
+
Iceland,2.6,53.4,9.4,43.3,38800,5.47,82,2.2,41900
|
71 |
+
India,58.8,22.6,4.05,27.1,4410,8.98,66.2,2.6,1350
|
72 |
+
Indonesia,33.3,24.3,2.61,22.4,8430,15.3,69.9,2.48,3110
|
73 |
+
Iran,19.3,24.4,5.6,19.4,17400,15.9,74.5,1.76,6530
|
74 |
+
Iraq,36.9,39.4,8.41,34.1,12700,16.6,67.2,4.56,4500
|
75 |
+
Ireland,4.2,103,9.19,86.5,45700,-3.22,80.4,2.05,48700
|
76 |
+
Israel,4.6,35,7.63,32.9,29600,1.77,81.4,3.03,30600
|
77 |
+
Italy,4,25.2,9.53,27.2,36200,0.319,81.7,1.46,35800
|
78 |
+
Jamaica,18.1,31.3,4.81,49.6,8000,9.81,74.7,2.17,4680
|
79 |
+
Japan,3.2,15,9.49,13.6,35800,-1.9,82.8,1.39,44500
|
80 |
+
Jordan,21.1,48.3,8.04,69,9470,8.43,75.8,3.66,3680
|
81 |
+
Kazakhstan,21.5,44.2,4.29,29.9,20100,19.5,68.4,2.6,9070
|
82 |
+
Kenya,62.2,20.7,4.75,33.6,2480,2.09,62.8,4.37,967
|
83 |
+
Kiribati,62.7,13.3,11.3,79.9,1730,1.52,60.7,3.84,1490
|
84 |
+
Kuwait,10.8,66.7,2.63,30.4,75200,11.2,78.2,2.21,38500
|
85 |
+
Kyrgyz Republic,29.6,51.6,6.18,81.7,2790,10,68.5,3.1,880
|
86 |
+
Lao,78.9,35.4,4.47,49.3,3980,9.2,63.8,3.15,1140
|
87 |
+
Latvia,7.8,53.7,6.68,55.1,18300,-0.812,73.1,1.36,11300
|
88 |
+
Lebanon,10.3,35.8,7.03,60.2,16300,0.238,79.8,1.61,8860
|
89 |
+
Lesotho,99.7,39.4,11.1,101,2380,4.15,46.5,3.3,1170
|
90 |
+
Liberia,89.3,19.1,11.8,92.6,700,5.47,60.8,5.02,327
|
91 |
+
Libya,16.6,65.6,3.88,42.1,29600,14.2,76.1,2.41,12100
|
92 |
+
Lithuania,6.1,65.3,7.04,67.2,21100,2.38,73.2,1.5,12000
|
93 |
+
Luxembourg,2.8,175,7.77,142,91700,3.62,81.3,1.63,105000
|
94 |
+
"Macedonia, FYR",10.4,39.8,7.09,58.1,11400,2.04,74,1.47,4540
|
95 |
+
Madagascar,62.2,25,3.77,43,1390,8.79,60.8,4.6,413
|
96 |
+
Malawi,90.5,22.8,6.59,34.9,1030,12.1,53.1,5.31,459
|
97 |
+
Malaysia,7.9,86.9,4.39,71,21100,7.27,74.5,2.15,9070
|
98 |
+
Maldives,13.2,77.6,6.33,65.4,10500,2.88,77.9,2.23,7100
|
99 |
+
Mali,137,22.8,4.98,35.1,1870,4.37,59.5,6.55,708
|
100 |
+
Malta,6.8,153,8.65,154,28300,3.83,80.3,1.36,21100
|
101 |
+
Mauritania,97.4,50.7,4.41,61.2,3320,18.9,68.2,4.98,1200
|
102 |
+
Mauritius,15,51.2,6,62.2,15900,1.13,73.4,1.57,8000
|
103 |
+
"Micronesia, Fed. Sts.",40,23.5,14.2,81,3340,3.8,65.4,3.46,2860
|
104 |
+
Moldova,17.2,39.2,11.7,78.5,3910,11.1,69.7,1.27,1630
|
105 |
+
Mongolia,26.1,46.7,5.44,56.7,7710,39.2,66.2,2.64,2650
|
106 |
+
Montenegro,6.8,37,9.11,62.7,14000,1.6,76.4,1.77,6680
|
107 |
+
Morocco,33.5,32.2,5.2,43,6440,0.976,73.5,2.58,2830
|
108 |
+
Mozambique,101,31.5,5.21,46.2,918,7.64,54.5,5.56,419
|
109 |
+
Myanmar,64.4,0.109,1.97,0.0659,3720,7.04,66.8,2.41,988
|
110 |
+
Namibia,56,47.8,6.78,60.7,8460,3.56,58.6,3.6,5190
|
111 |
+
Nepal,47,9.58,5.25,36.4,1990,15.1,68.3,2.61,592
|
112 |
+
Netherlands,4.5,72,11.9,63.6,45500,0.848,80.7,1.79,50300
|
113 |
+
New Zealand,6.2,30.3,10.1,28,32300,3.73,80.9,2.17,33700
|
114 |
+
Niger,123,22.2,5.16,49.1,814,2.55,58.8,7.49,348
|
115 |
+
Nigeria,130,25.3,5.07,17.4,5150,104,60.5,5.84,2330
|
116 |
+
Norway,3.2,39.7,9.48,28.5,62300,5.95,81,1.95,87800
|
117 |
+
Oman,11.7,65.7,2.77,41.2,45300,15.6,76.1,2.9,19300
|
118 |
+
Pakistan,92.1,13.5,2.2,19.4,4280,10.9,65.3,3.85,1040
|
119 |
+
Panama,19.7,70,8.1,78.2,15400,2.59,77.8,2.62,8080
|
120 |
+
Paraguay,24.1,55.1,5.87,51.5,7290,6.1,74.1,2.73,3230
|
121 |
+
Peru,20.3,27.8,5.08,23.8,9960,5.71,77.9,2.54,5020
|
122 |
+
Philippines,31.9,34.8,3.61,36.6,5600,4.22,69,3.16,2130
|
123 |
+
Poland,6,40.1,7.46,42.1,21800,1.66,76.3,1.41,12600
|
124 |
+
Portugal,3.9,29.9,11,37.4,27200,0.643,79.8,1.39,22500
|
125 |
+
Qatar,9,62.3,1.81,23.8,125000,6.98,79.5,2.07,70300
|
126 |
+
Romania,11.5,32.6,5.58,38.8,17800,3.53,73.7,1.59,8230
|
127 |
+
Russia,10,29.2,5.08,21.1,23100,14.2,69.2,1.57,10700
|
128 |
+
Rwanda,63.6,12,10.5,30,1350,2.61,64.6,4.51,563
|
129 |
+
Samoa,18.9,29.2,6.47,53.1,5400,1.72,71.5,4.34,3450
|
130 |
+
Saudi Arabia,15.7,49.6,4.29,33,45400,17.2,75.1,2.96,19300
|
131 |
+
Senegal,66.8,24.9,5.66,40.3,2180,1.85,64,5.06,1000
|
132 |
+
Serbia,7.6,32.9,10.4,47.9,12700,5.88,74.7,1.4,5410
|
133 |
+
Seychelles,14.4,93.8,3.4,108,20400,-4.21,73.4,2.17,10800
|
134 |
+
Sierra Leone,160,16.8,13.1,34.5,1220,17.2,55,5.2,399
|
135 |
+
Singapore,2.8,200,3.96,174,72100,-0.046,82.7,1.15,46600
|
136 |
+
Slovak Republic,7,76.3,8.79,77.8,25200,0.485,75.5,1.43,16600
|
137 |
+
Slovenia,3.2,64.3,9.41,62.9,28700,-0.987,79.5,1.57,23400
|
138 |
+
Solomon Islands,28.1,49.3,8.55,81.2,1780,6.81,61.7,4.24,1290
|
139 |
+
South Africa,53.7,28.6,8.94,27.4,12000,6.35,54.3,2.59,7280
|
140 |
+
South Korea,4.1,49.4,6.93,46.2,30400,3.16,80.1,1.23,22100
|
141 |
+
Spain,3.8,25.5,9.54,26.8,32500,0.16,81.9,1.37,30700
|
142 |
+
Sri Lanka,11.2,19.6,2.94,26.8,8560,22.8,74.4,2.2,2810
|
143 |
+
St. Vincent and the Grenadines,20.7,26.9,4.47,57.1,9920,4.44,71.6,2.07,6230
|
144 |
+
Sudan,76.7,19.7,6.32,17.2,3370,19.6,66.3,4.88,1480
|
145 |
+
Suriname,24.1,52.5,7.01,38.4,14200,7.2,70.3,2.52,8300
|
146 |
+
Sweden,3,46.2,9.63,40.7,42900,0.991,81.5,1.98,52100
|
147 |
+
Switzerland,4.5,64,11.5,53.3,55500,0.317,82.2,1.52,74600
|
148 |
+
Tajikistan,52.4,14.9,5.98,58.6,2110,12.5,69.6,3.51,738
|
149 |
+
Tanzania,71.9,18.7,6.01,29.1,2090,9.25,59.3,5.43,702
|
150 |
+
Thailand,14.9,66.5,3.88,60.8,13500,4.08,76.6,1.55,5080
|
151 |
+
Timor-Leste,62.6,2.2,9.12,27.8,1850,26.5,71.1,6.23,3600
|
152 |
+
Togo,90.3,40.2,7.65,57.3,1210,1.18,58.7,4.87,488
|
153 |
+
Tonga,17.4,12.4,5.07,60.3,4980,3.68,69.9,3.91,3550
|
154 |
+
Tunisia,17.4,50.5,6.21,55.3,10400,3.82,76.9,2.14,4140
|
155 |
+
Turkey,19.1,20.4,6.74,25.5,18000,7.01,78.2,2.15,10700
|
156 |
+
Turkmenistan,62,76.3,2.5,44.5,9940,2.31,67.9,2.83,4440
|
157 |
+
Uganda,81,17.1,9.01,28.6,1540,10.6,56.8,6.15,595
|
158 |
+
Ukraine,11.7,47.1,7.72,51.1,7820,13.4,70.4,1.44,2970
|
159 |
+
United Arab Emirates,8.6,77.7,3.66,63.6,57600,12.5,76.5,1.87,35000
|
160 |
+
United Kingdom,5.2,28.2,9.64,30.8,36200,1.57,80.3,1.92,38900
|
161 |
+
United States,7.3,12.4,17.9,15.8,49400,1.22,78.7,1.93,48400
|
162 |
+
Uruguay,10.6,26.3,8.35,25.4,17100,4.91,76.4,2.08,11900
|
163 |
+
Uzbekistan,36.3,31.7,5.81,28.5,4240,16.5,68.8,2.34,1380
|
164 |
+
Vanuatu,29.2,46.6,5.25,52.7,2950,2.62,63,3.5,2970
|
165 |
+
Venezuela,17.1,28.5,4.91,17.6,16500,45.9,75.4,2.47,13500
|
166 |
+
Vietnam,23.3,72,6.84,80.2,4490,12.1,73.1,1.95,1310
|
167 |
+
Yemen,56.3,30,5.18,34.4,4480,23.6,67.5,4.67,1310
|
168 |
+
Zambia,83.1,37,5.89,30.9,3280,14,52,5.4,1460
|
app.py
ADDED
@@ -0,0 +1,259 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from sklearn.cluster import DBSCAN
|
6 |
+
from sklearn.decomposition import PCA
|
7 |
+
from sklearn.preprocessing import StandardScaler
|
8 |
+
from sklearn.preprocessing import LabelEncoder
|
9 |
+
from sklearn.cluster import SpectralClustering
|
10 |
+
from sklearn.cluster import KMeans
|
11 |
+
from sklearn.metrics import silhouette_score
|
12 |
+
|
13 |
+
@st.cache_resource
|
14 |
+
def load_dataset():
|
15 |
+
"""
|
16 |
+
Loads the `.csv` dataset
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
pd.DataFrame
|
20 |
+
"""
|
21 |
+
return pd.read_csv('./Country-data.csv'), pd.read_csv('./data-dictionary.csv')
|
22 |
+
|
23 |
+
df, df_dict = load_dataset()
|
24 |
+
|
25 |
+
@st.cache_resource
|
26 |
+
def preprocess_data(df: pd.DataFrame,
|
27 |
+
pca_n_components: int = 2,
|
28 |
+
pca: bool = True,
|
29 |
+
scale: bool = True) -> np.array:
|
30 |
+
"""
|
31 |
+
Preprocess the `Country-data.csv`.
|
32 |
+
|
33 |
+
Args:
|
34 |
+
df (pd.DataFrame): The dataset to be preprocessed
|
35 |
+
pca_n_components (int): Number of components to be passed on PCA. Ignore if `pca` == False
|
36 |
+
pca (bool): Implement PCA on the dataset
|
37 |
+
scale (bool): Scale the dataset with Standard Scaler
|
38 |
+
"""
|
39 |
+
df = df.copy()
|
40 |
+
|
41 |
+
encoder = LabelEncoder()
|
42 |
+
df['country'] = encoder.fit_transform(df['country'].values)
|
43 |
+
|
44 |
+
if scale:
|
45 |
+
scaler = StandardScaler()
|
46 |
+
df = scaler.fit_transform(df)
|
47 |
+
|
48 |
+
if pca:
|
49 |
+
pca_model = PCA(n_components=pca_n_components)
|
50 |
+
df = pca_model.fit_transform(df)
|
51 |
+
|
52 |
+
return np.array(df)
|
53 |
+
|
54 |
+
@st.cache_resource
|
55 |
+
def preprocess_input_data(input_data: np.array,
|
56 |
+
pca_n_components: int,
|
57 |
+
pca: bool = True,
|
58 |
+
scale: bool = True) -> np.array:
|
59 |
+
|
60 |
+
"""
|
61 |
+
Preprocess the a single instance of input data.
|
62 |
+
|
63 |
+
Args:
|
64 |
+
df (pd.DataFrame): The dataset to be preprocessed
|
65 |
+
pca_n_components (int): Number of components to be passed on PCA. Ignore if `pca` == False
|
66 |
+
pca (bool): Implement PCA on the dataset
|
67 |
+
scale (bool): Scale the dataset with Standard Scaler
|
68 |
+
"""
|
69 |
+
input_data = input_data.copy()
|
70 |
+
df_copy = df.copy()
|
71 |
+
|
72 |
+
encoder = LabelEncoder()
|
73 |
+
df_copy['country'] = encoder.fit_transform(df_copy['country'].values)
|
74 |
+
|
75 |
+
if scale:
|
76 |
+
scaler = StandardScaler()
|
77 |
+
df_copy = scaler.fit_transform(df_copy)
|
78 |
+
input_data = scaler.transform(input_data)
|
79 |
+
|
80 |
+
if pca:
|
81 |
+
pca_model = PCA(n_components=pca_n_components)
|
82 |
+
df_copy = pca_model.fit_transform(df_copy)
|
83 |
+
input_data = pca_model.transform(input_data)
|
84 |
+
|
85 |
+
return input_data[0]
|
86 |
+
|
87 |
+
def main():
|
88 |
+
st.header('Country Development Clustering')
|
89 |
+
|
90 |
+
col1, col2 = st.columns(2)
|
91 |
+
|
92 |
+
with col1:
|
93 |
+
pca = st.toggle('Train with PCA', value=True)
|
94 |
+
|
95 |
+
with col2:
|
96 |
+
scaler = st.toggle('Train with Scaled Data', value=True)
|
97 |
+
|
98 |
+
preprocessed_df = preprocess_data(df=df, pca_n_components=2, pca=pca, scale=scaler)
|
99 |
+
|
100 |
+
data, kmeans, dbscan, specclus = st.tabs(['About the Data', 'KMeans Algorithm', 'DBSCAN', 'Spectral Clustering'])
|
101 |
+
|
102 |
+
with data:
|
103 |
+
st.header('Country Development Data')
|
104 |
+
st.write('**Clustering the Countries by using Unsupervised Learning for HELP International**')
|
105 |
+
|
106 |
+
col1, col2 = st.columns(2)
|
107 |
+
|
108 |
+
with col1:
|
109 |
+
st.write('**OBJECTIVE**:')
|
110 |
+
st.caption('To categorise the countries using socio-economic and health factors that determine the overall development of the country.')
|
111 |
+
|
112 |
+
with col2:
|
113 |
+
st.write('**About Organization**')
|
114 |
+
st.caption('HELP International is an international humanitarian NGO that is committed to fighting poverty and providing the people of backward countries with basic amenities and relief during the time of disasters and natural calamities.')
|
115 |
+
|
116 |
+
st.write('**Problem Statement:**')
|
117 |
+
st.caption('HELP International have been able to raise around $ 10 million. Now the CEO of the NGO needs to decide how to use this money strategically and effectively. So, CEO has to make decision to choose the countries that are in the direst need of aid. Hence, your Job as a Data scientist is to categorise the countries using some socio-economic and health factors that determine the overall development of the country. Then you need to suggest the countries which the CEO needs to focus on the most.')
|
118 |
+
|
119 |
+
st.dataframe(df)
|
120 |
+
|
121 |
+
st.dataframe(df_dict.set_index('Column Name'), width=1000)
|
122 |
+
|
123 |
+
url = 'https://www.kaggle.com/datasets/rohan0301/unsupervised-learning-on-country-data?select=Country-data.csv'
|
124 |
+
st.caption('For more details about the dataset, visit [Kaggle](%s).' % url)
|
125 |
+
|
126 |
+
with kmeans:
|
127 |
+
st.write("In this tab, you get to train a KMeans model through hyperparameter tuning. With prediction and visualization!")
|
128 |
+
st.write('Experiment with the hyperparameters to tune out the best KMeans model.')
|
129 |
+
|
130 |
+
col1, col2 = st.columns(2)
|
131 |
+
|
132 |
+
with col1:
|
133 |
+
n_clusters = st.slider('`n_clusters`', min_value=1, max_value=20, value=2)
|
134 |
+
|
135 |
+
with col2:
|
136 |
+
init = st.selectbox('`init`', ['k-means++', 'random'], index=0)
|
137 |
+
|
138 |
+
with st.popover('Input data for prediction'):
|
139 |
+
st.caption('**NOTE**: The input data will only be used in KMeans algorithm.')
|
140 |
+
country = st.selectbox('Which country are you from?', df['country'].unique())
|
141 |
+
country_index = list(df['country'].unique()).index(country)
|
142 |
+
child_mort = st.slider('Child Mortality Rate', min_value=1.00, max_value=df['child_mort'].max())
|
143 |
+
exports = st.slider('Exports', min_value=1.00, max_value=df['exports'].max())
|
144 |
+
imports = st.slider('Imports', min_value=1.00, max_value=df['imports'].max())
|
145 |
+
health = st.slider('Health', min_value=1.00, max_value=df['health'].max())
|
146 |
+
income = st.slider('Income', min_value=1, max_value=df['income'].max())
|
147 |
+
inflation = st.slider('Inflation', min_value=1.00, max_value=df['inflation'].max())
|
148 |
+
life_expec = st.slider('Health', min_value=1.00, max_value=df['life_expec'].max())
|
149 |
+
total_fer = st.slider('Fertility Rate', min_value=1.00, max_value=df['total_fer'].max())
|
150 |
+
gdpp = st.slider('GDPP', min_value=1, max_value=df['gdpp'].max())
|
151 |
+
|
152 |
+
input_data = np.array([[country_index, child_mort, exports,
|
153 |
+
health, imports, income, inflation,
|
154 |
+
life_expec, total_fer, gdpp
|
155 |
+
]])
|
156 |
+
input_data = preprocess_input_data(input_data, 2, pca, scaler)
|
157 |
+
|
158 |
+
km = KMeans(n_clusters=n_clusters, init=init)
|
159 |
+
km.fit(preprocessed_df)
|
160 |
+
|
161 |
+
km_centroids = km.cluster_centers_
|
162 |
+
km_preds = km.predict(preprocessed_df)
|
163 |
+
|
164 |
+
st.write(f'Silhouette Score of K-Means: {silhouette_score(preprocessed_df, km_preds):2f}%')
|
165 |
+
|
166 |
+
fig, ax = plt.subplots(figsize=(10,7))
|
167 |
+
ax.set_title('KMeans Algorithm')
|
168 |
+
ax.scatter(km_centroids[:,0], km_centroids[:,1], s=200, c='black', alpha=0.5, label='Centroids')
|
169 |
+
scatter = ax.scatter(preprocessed_df[:,0], preprocessed_df[:,1], c=km_preds, s=20)
|
170 |
+
plt.scatter(input_data[0], input_data[1], s=150, c='red', marker='X', label='Input Data')
|
171 |
+
plt.legend()
|
172 |
+
plt.colorbar(scatter, ax=ax, label='Cluster Labels')
|
173 |
+
ax.grid(True)
|
174 |
+
st.pyplot(fig)
|
175 |
+
|
176 |
+
url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html'
|
177 |
+
st.caption('For more details of the hyperparameters, check out the the documentation of KMeans [source](%s) of the dataset.' % url)
|
178 |
+
|
179 |
+
with dbscan:
|
180 |
+
st.write('In this tab, you get to train a DBSCAN model through hyperparameter tuning, and visualize its results!')
|
181 |
+
st.caption('**NOTE**: DBSCAN is not a predictive algorithm, hence no input data for prediction.')
|
182 |
+
st.write('Experiment with the hyperparameters to tune out the best DBSCAN model.')
|
183 |
+
|
184 |
+
col1, col2 = st.columns(2)
|
185 |
+
|
186 |
+
with col1:
|
187 |
+
eps = st.slider('`eps`', min_value=0.1, max_value=10.0, value=0.5)
|
188 |
+
min_samples = st.slider('`min_samples`', min_value=2, max_value=20, value=5)
|
189 |
+
|
190 |
+
with col2:
|
191 |
+
metric = st.selectbox('`metric`', ['euclidean', 'manhattan', 'chebyshev'], index=0)
|
192 |
+
algorithm = st.selectbox('`algorithm`', ['auto', 'ball_tree', 'kd_tree', 'brute'], index=0)
|
193 |
+
|
194 |
+
dbs = DBSCAN(eps=eps, min_samples=min_samples, metric=metric, algorithm=algorithm)
|
195 |
+
dbs.fit(preprocessed_df)
|
196 |
+
dbs_labels = dbs.labels_
|
197 |
+
|
198 |
+
if scaler:
|
199 |
+
silhouette = silhouette_score(preprocessed_df, dbs_labels)
|
200 |
+
st.write(f'Silhouette Score of DBSCAN: {silhouette:.2f}')
|
201 |
+
else:
|
202 |
+
st.error('Cannot calculate Silhouette Score with unscaled data.')
|
203 |
+
|
204 |
+
fig, ax = plt.subplots(figsize=(10,7))
|
205 |
+
ax.set_title('DBSCAN Model')
|
206 |
+
scatter = ax.scatter(preprocessed_df[:, 0], preprocessed_df[:, 1], s=20, c=dbs_labels, cmap='viridis')
|
207 |
+
ax.grid(True)
|
208 |
+
plt.colorbar(scatter, ax=ax, label='Cluster Labels')
|
209 |
+
st.pyplot(fig)
|
210 |
+
|
211 |
+
url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html'
|
212 |
+
st.caption('For more details of the hyperparameters, check out the the documentation of DBSCAN [source](%s) of the dataset.' % url)
|
213 |
+
|
214 |
+
with specclus:
|
215 |
+
st.write('In this tab, you get to train a Spectral Clustering model through hyperparameter tuning, and visualize its results!')
|
216 |
+
st.write('Experiment with the hyperparameters to tune out the best Spectral Clustering model.')
|
217 |
+
st.caption('**NOTE**: Spectral Clustering is not a predictive algorithm, hence no input data for prediction.')
|
218 |
+
|
219 |
+
col1, col2 = st.columns(2)
|
220 |
+
|
221 |
+
gamma = 1.0
|
222 |
+
n_neighbors = 2
|
223 |
+
|
224 |
+
with col1:
|
225 |
+
affinity = st.selectbox('`affinity`', ['nearest_neighbors', 'rbf', 'precomputed', 'cosine'], index=1)
|
226 |
+
eigen_solver = st.selectbox('`eigen_solver`', ['arpack', 'lobpcg', 'amg', None], index=3)
|
227 |
+
assign_labels = st.selectbox('`degree`', ['kmeans', 'discretize'], index=0)
|
228 |
+
|
229 |
+
with col2:
|
230 |
+
n_clusters = st.slider('`n_clusters`', min_value=2, max_value=20, value=8)
|
231 |
+
if affinity == 'rbf':
|
232 |
+
gamma = st.slider('`gamma`', min_value=0.01, max_value=0.1, value=1.0)
|
233 |
+
elif affinity == 'nearest_neighbors':
|
234 |
+
n_neighbors = st.slider('`n_neighbors`', min_value=2, max_value=20, value=10)
|
235 |
+
|
236 |
+
|
237 |
+
sc = SpectralClustering(n_clusters=n_clusters, affinity=affinity,
|
238 |
+
gamma=gamma, n_neighbors=n_neighbors,
|
239 |
+
eigen_solver=eigen_solver, assign_labels=assign_labels
|
240 |
+
)
|
241 |
+
sc_labels = sc.fit_predict(preprocessed_df)
|
242 |
+
|
243 |
+
if scaler:
|
244 |
+
st.write(f'Silhouette Score of Spectral Clustering: {silhouette_score(preprocessed_df, km_preds):2f}%')
|
245 |
+
else:
|
246 |
+
st.error('Cannot calculate Silhouette Score with unscaled data.')
|
247 |
+
|
248 |
+
fig, ax = plt.subplots(figsize=(10,7))
|
249 |
+
scatter = ax.scatter(preprocessed_df[:, 0], preprocessed_df[:, 1], c=sc_labels, s=20)
|
250 |
+
ax.set_title('Spectral Clustering')
|
251 |
+
plt.colorbar(scatter, ax=ax, label='Cluster Labels')
|
252 |
+
plt.grid(True)
|
253 |
+
st.pyplot(fig)
|
254 |
+
|
255 |
+
url = 'https://scikit-learn.org/stable/modules/generated/sklearn.cluster.SpectralClustering.html'
|
256 |
+
st.caption('For more details of the hyperparameters, check out the the documentation of Spectral Clustering [source](%s) of the dataset.' % url)
|
257 |
+
|
258 |
+
if __name__ == "__main__":
|
259 |
+
main()
|
data-dictionary.csv
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Column Name,Description
|
2 |
+
country,Name of the country
|
3 |
+
child_mort,Death of children under 5 years of age per 1000 live births
|
4 |
+
exports,Exports of goods and services per capita. Given as %age of the GDP per capita
|
5 |
+
health,Total health spending per capita. Given as %age of GDP per capita
|
6 |
+
imports,Imports of goods and services per capita. Given as %age of the GDP per capita
|
7 |
+
Income,Net income per person
|
8 |
+
Inflation,The measurement of the annual growth rate of the Total GDP
|
9 |
+
life_expec,The average number of years a new born child would live if the current mortality patterns are to remain the same
|
10 |
+
total_fer,The number of children that would be born to each woman if the current age-fertility rates remain the same.
|
11 |
+
gdpp,The GDP per capita. Calculated as the Total GDP divided by the total population.
|
model.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas==2.2.3
|
2 |
+
streamlit==1.42.0
|
3 |
+
scikit-learn==1.6.1
|
4 |
+
matplotlib==3.10.0
|
5 |
+
joblib==1.4.2
|
6 |
+
pyamg==5.2.1
|