-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalisis.py
320 lines (243 loc) · 11.1 KB
/
analisis.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 -*-
"""Análisis.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1IgGsebdiJRAdXOeW7I9wXXZQAtyPXlXQ
Install dependencies
"""
#%reset -f
#!pip install psycopg2
"""Import libraries"""
import psycopg2
import numpy as np
import pandas as pd
import time
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
"""[Connect to database](https://pynative.com/python-postgresql-tutorial/)"""
def algoritmo():
try:
connection = psycopg2.connect(user = "postgres",
password = "Welcome01",
host = "34.78.89.69",
port = "5432",
database = "dataproject1")
cursor = connection.cursor()
# Print PostgreSQL Connection properties
print ( connection.get_dsn_parameters(),"\n")
# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
"""Obtain "datos" of the cities and columns names"""
cursor.execute("SELECT * FROM datos;")
record = cursor.fetchall()
cursor.execute("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'datos'")
columns_name = cursor.fetchall()
"""Convert array of arrays to single array"""
array_columns_name = np.array(columns_name)
array_columns_name = np.concatenate( array_columns_name, axis=0 )
#print(array_columns_name)
"""Transform result of query to a pandas dataframe"""
df = pd.DataFrame(record, columns=array_columns_name)
df.head()
"""Obtain "clientes" of the clients responses"""
cursor.execute("SELECT * FROM clientes ORDER BY client_id DESC;")
record = cursor.fetchall()
cursor.execute("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'clientes'")
columns_name = cursor.fetchall()
array_columns_name = np.array(columns_name)
array_columns_name = np.concatenate( array_columns_name, axis=0 )
clientes = pd.DataFrame(record, columns=array_columns_name)
clientes.head()
df['score'] = 0
"""Pollution Variable"""
env_score = clientes.iloc[0].enviromental_score
pollution_cities = pd.DataFrame(columns=df.columns)
#print(env_score)
if env_score == 5:
min = df.pollution.min()
df.loc[df.pollution == min, 'score'] +=1
pollution_cities = df[df.pollution == min]
elif env_score == 4:
order_by_pollution = df.sort_values(by=['pollution'], ascending=True)
mins = order_by_pollution.iloc[:2]
for _, i in mins.iterrows():
df.loc[df.city_id == i.city_id, 'score'] += 1
pollution_cities = pollution_cities.append(i)
elif env_score == 3:
order_by_pollution = df.sort_values(by=['pollution'], ascending=True)
mins = order_by_pollution.iloc[:3]
for _, i in mins.iterrows():
df.loc[df.city_id == i.city_id, 'score'] += 1
pollution_cities = pollution_cities.append(i)
elif env_score == 2:
order_by_pollution = df.sort_values(by=['pollution'], ascending=True)
mins = order_by_pollution.iloc[:4]
for _, i in mins.iterrows():
df.loc[df.city_id == i.city_id, 'score'] += 1
pollution_cities = pollution_cities.append(i)
elif env_score == 1:
order_by_pollution = df.sort_values(by=['pollution'], ascending=True)
mins = order_by_pollution.iloc[:5]
for _, i in mins.iterrows():
df.loc[df.city_id == i.city_id, 'score'] += 1
pollution_cities = pollution_cities.append(i)
#df.head()
"""Work Spaces Variable"""
wk_space = clientes.iloc[0].work_preference
wk_cities = pd.DataFrame(columns=df.columns)
if wk_space == 'Co-Working':
ratio = df['work_spaces'] /df['c_population']
aux = df
aux['ratio'] = ratio*100
aux.loc[aux.ratio > 0.006, 'score'] +=1
wk_cities = aux.loc[aux.ratio>0.006]
#print(aux.head())
#wk_cities
"""Transport Variable"""
a = {'Walking': 'Andando', 'Car': 'Coche', 'Bike': 'Bici', 'Motorbike': 'Moto', 'Bus/Trolleybus': 'Bus', 'Tram/Streetcar': 'Tranvía', 'Train/Metro': 'Metro'}
df.best_mobility_option.replace(a, inplace=True)
clientex = clientes.iloc[0].transport
df.loc[df.best_mobility_option == clientex, 'score']+=1
transport_cities = df.loc[df.best_mobility_option == clientex]
"""
cliente = clientes.iloc[0].transport
for _, i in df.iterrows():
if i.best_mobility_option == cliente:
df.score[df.city_id == i.city_id] += 1
print(df.head())
"""
cliente= clientes.iloc[0]
#df.loc[(df.mountain == True & cliente.place_score == 'Montaña' & df.beach == False, 'score'] =+ 1
"""Landscape Variable"""
landscape_cities = pd.DataFrame(columns=df.columns)
for _, i in df.iterrows():
if i.mountain == True and i.beach == False and cliente.place_score == 'Montaña':
df.loc[df.city_id == i.city_id, 'score'] += 1
landscape_cities = landscape_cities.append(i)
elif i.mountain == False and i.beach == True and cliente.place_score == 'Playa':
df.loc[df.city_id == i.city_id, 'score'] += 1
landscape_cities = landscape_cities.append(i)
elif i.mountain == True and i.beach == True and cliente.place_score == 'Ambos':
df.loc[df.city_id == i.city_id, 'score'] += 1
landscape_cities = landscape_cities.append(i)
elif i.mountain == False and i.beach == False and cliente.place_score == 'Ninguno':
df.loc[df.city_id == i.city_id, 'score'] += 1
landscape_cities = landscape_cities.append(i)
"""Weather Variable"""
weather_cities = pd.DataFrame(columns= df.columns)
for _, i in df.iterrows():
if (i.c_temp < 15 or i.c_rainy_days > 20) and cliente.season == 'Invierno':
df.loc[df.city_id == i.city_id, 'score'] += 1
weather_cities = weather_cities.append(i)
elif (15 <= i.c_temp <= 25 or 10 <= i.c_rainy_days) <= 20 and cliente.season == 'Primavera':
df.loc[df.city_id == i.city_id, 'score'] += 1
weather_cities = weather_cities.append(i)
elif (i.c_temp > 25 or i.c_rainy_days < 10) and cliente.season == 'Verano':
df.loc[df.city_id == i.city_id, 'score'] += 1
weather_cities = weather_cities.append(i)
elif (15 <= i.c_temp <= 25 or 10 <= i.c_rainy_days <= 20) and cliente.season == 'Otoño':
df.loc[df.city_id == i.city_id, 'score'] += 1
weather_cities = weather_cities.append(i)
"""Housing Variable"""
housing_cities = pd.DataFrame(columns=df.columns)
for _, i in df.iterrows():
if i.housing and cliente.percentaje_home > 50:
df.loc[df.city_id == i.city_id, 'score'] += 1
housing_cities = housing_cities.append(i)
elif i.housing < 15 and 30 <= cliente.percentaje_home <= 50:
df.loc[df.city_id == i.city_id, 'score'] += 1
housing_cities = housing_cities.append(i)
elif i.housing < 5 and cliente.percentaje_home < 30:
df.loc[df.city_id == i.city_id, 'score'] += 1
housing_cities = housing_cities.append(i)
"""Size Variable"""
size_cities = pd.DataFrame(columns=df.columns)
for _, i in df.iterrows():
if i.c_population < 2000000 and cliente.size_preference == 'Pequeñas':
df.loc[df.city_id == i.city_id, 'score'] += 1
size_cities = size_cities.append(i)
elif 2000000 <= i.c_population <= 4000000 and cliente.size_preference == 'Medianas':
df.loc[df.city_id == i.city_id, 'score'] += 1
size_cities = size_cities.append(i)
elif i.c_population > 4000000 and cliente.size_preference == 'Grandes':
df.loc[df.city_id == i.city_id, 'score'] += 1
size_cities = size_cities.append(i)
"""Leisure Variable"""
leisure_cities = pd.DataFrame(columns=df.columns)
if cliente.entreteiment == 'Sí':
max_leisure = df.leisure.max()
df.loc[df.leisure == max_leisure, 'score'] += 1
leisure_cities = df[df.leisure == max_leisure]
"""Non-client Variables"""
min_cpi = df.cpi.min()
df.loc[df.cpi == min_cpi, 'score'] += 0.5
max_gdp= df.gdp_pc.max()
df.loc[df.gdp_pc == max_gdp, 'score'] += 0.5
min_tax = df.tax_burden.min()
df.loc[df.tax_burden == min_tax, 'score'] += 0.5
min_crime = df.crime_rate.min()
df.loc[df.crime_rate == min_crime, 'score'] += 0.5
max_hdi = df.hdi.max()
df.loc[df.hdi == max_hdi, 'score'] += 0.5
max_doing_business = df.doing_business.max()
df.loc[df.doing_business == max_doing_business, 'score'] += 0.5
max_freedom = df.freedom.max()
df.loc[df.freedom == max_freedom, 'score'] += 0.5
max_life = df.life_expectancy.max()
df.loc[df.life_expectancy == max_life, 'score'] += 0.5
"""Factor"""
factor = clientes.iloc[0].interest_variable
if factor == 'Medio ambiente':
for _, i in pollution_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Zona de trabajo':
for _, i in wk_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Tamaño de la ciudad':
for _, i in size_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Ocio':
for _, i in leisure_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Gasto en vivienda':
for _, i in housing_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Clima':
for _, i in weather_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Movilidad urbana':
for _, i in transport_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
elif factor == 'Paisaje':
for _, i in landscape_cities.iterrows():
df.loc[i.city_id == df.city_id, 'score']+=1
"""ELEGIR CIUDAD"""
ciudad_ideal = df[df.score == df.score.max()]
#print(ciudad_ideal)
print("Tu ciudad ideal es: "+ ciudad_ideal.city_name + "\n")
print("con una puntuación de: "+str(ciudad_ideal.score))
cursor = connection.cursor()
cursor.execute(
"UPDATE clientes SET best_city_name1='"+ciudad_ideal.iloc[0].city_name+"'"+
" WHERE client_id = "+str(clientes.iloc[0].client_id));
connection.commit()
cursor.close()
if len(ciudad_ideal) >= 2:
cursor = connection.cursor()
cursor.execute(
"UPDATE clientes SET best_city_name2='"+ciudad_ideal.iloc[1].city_name+"'"+
" WHERE client_id = "+str(clientes.iloc[0].client_id));
connection.commit()
cursor.close()
return ciudad_ideal
def main():
while True:
ciudad = algoritmo()
time.sleep(5)
if __name__ == "__main__":
main()