|
import streamlit as st |
|
import pandas as pd |
|
import numpy as np |
|
from sklearn.neighbors import KNeighborsRegressor |
|
from geopy.distance import geodesic |
|
import googlemaps |
|
from geopy.exc import GeocoderTimedOut |
|
from streamlit_folium import st_folium |
|
import folium |
|
from branca.colormap import LinearColormap |
|
|
|
|
|
def add_heatmap_layer(map_obj, data, column_name, colormap_name, radius=15): |
|
heat_data = data[['latitude', 'longitude', column_name]].dropna() |
|
heat_layer = folium.FeatureGroup(name=f'Heatmap - {column_name}') |
|
|
|
cmap = LinearColormap(colors=['blue', 'white', 'red'], vmin=heat_data[column_name].min(), vmax=heat_data[column_name].max()) |
|
|
|
for index, row in heat_data.iterrows(): |
|
folium.CircleMarker( |
|
location=[row['latitude'], row['longitude']], |
|
radius=radius, |
|
fill=True, |
|
fill_color=cmap(row[column_name]), |
|
fill_opacity=0.7, |
|
color='black', |
|
weight=0.5, |
|
).add_to(heat_layer) |
|
|
|
heat_layer.add_to(map_obj) |
|
|
|
|
|
def calculate_distance(lat1, lon1, lat2, lon2): |
|
coords_1 = (lat1, lon1) |
|
coords_2 = (lat2, lon2) |
|
return geodesic(coords_1, coords_2).meters |
|
|
|
def knn_predict(df, target_column, features_columns, k=5): |
|
|
|
X = df[features_columns] |
|
y = df[target_column] |
|
|
|
|
|
if len(X) < k: |
|
return np.zeros(len(X)) |
|
|
|
|
|
knn = KNeighborsRegressor(n_neighbors=k) |
|
|
|
|
|
knn.fit(X, y) |
|
|
|
|
|
predictions = knn.predict(df[features_columns]) |
|
|
|
return predictions |
|
|
|
|
|
st.set_page_config(layout="wide") |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
@font-face {font-family: 'Quicksand'; |
|
src: url('font/Quicksand-VariableFont_wght.ttf') format('truetype'); |
|
|
|
} |
|
body { |
|
color: white; |
|
background-color: #1e1e1e; |
|
font-family: 'Quicksand', sans-serif; |
|
} |
|
.st-df-header, .st-df-body, .st-df-caption { |
|
color: #f8f9fa; /* Bootstrap table header text color */ |
|
} |
|
.st-eb { |
|
background-color: #343a40; /* Streamlit exception box background color */ |
|
} |
|
</style> |
|
""", |
|
unsafe_allow_html=True |
|
) |
|
|
|
|
|
|
|
data = pd.read_excel('data_nexus.xlsx') |
|
|
|
|
|
radius_visible = True |
|
custom_address_initial = 'Centro, Lajeado - RS, Brazil' |
|
|
|
custom_lat = -29.7168 |
|
|
|
custom_lon = -52.4943 |
|
radius_in_meters = 150000 |
|
filtered_data = data |
|
|
|
|
|
zoom_level = 14 |
|
|
|
|
|
title_html = """ |
|
<style> |
|
@font-face {font-family: 'Quicksand'; |
|
src: url('font/Quicksand-VariableFont_wght.ttf') format('truetype'); |
|
} |
|
body {{ |
|
font-family: 'Quicksand', sans-serif; |
|
}} |
|
</style> |
|
<span style='color: gray; font-size: 50px;'>aval</span> |
|
<span style='color: white; font-size: 50px;'>ia</span> |
|
<span style='color: gray; font-size: 50px;'>.NEXUS</span> |
|
""" |
|
|
|
|
|
factor_html = """ |
|
<style> |
|
@font-face {font-family: 'Quicksand'; |
|
src: url('font/Quicksand-VariableFont_wght.ttf') format('truetype'); |
|
} |
|
body {{ |
|
font-family: 'Quicksand', sans-serif; |
|
}} |
|
</style> |
|
<a href='https://huggingface.co/spaces/DavidSB/avaliaFACTOR' target='_blank' style='text-decoration: none; color: inherit;'> |
|
<span style='color: gray; font-size: 20px;'>aval</span> |
|
<span style='color: white; font-size: 20px;'>ia</span> |
|
<span style='color: gray; font-size: 20px;'>.FACTOR</span> |
|
""" |
|
|
|
|
|
evo_html = """ |
|
<style> |
|
@font-face {font-family: 'Quicksand'; |
|
src: url('font/Quicksand-VariableFont_wght.ttf') format('truetype'); |
|
} |
|
body {{ |
|
font-family: 'Quicksand', sans-serif; |
|
}} |
|
</style> |
|
<a href='https://huggingface.co/spaces/DavidSB/avalia.EVO' target='_blank' style='text-decoration: none; color: inherit;'> |
|
<span style='color: gray; font-size: 20px;'>aval</span> |
|
<span style='color: white; font-size: 20px;'>ia</span> |
|
<span style='color: gray; font-size: 20px;'>.EVO</span> |
|
""" |
|
|
|
|
|
import streamlit as st |
|
|
|
import streamlit as st |
|
|
|
|
|
with st.sidebar: |
|
st.markdown(title_html, unsafe_allow_html=True) |
|
|
|
|
|
selected_fonte = st.selectbox('Finalidade', data['Fonte'].unique()) |
|
data = data[data['Fonte'] == selected_fonte] |
|
|
|
|
|
selected_tipo = st.selectbox('Tipo de imóvel', data['Tipo'].unique()) |
|
data_tipo = data[data['Tipo'] == selected_tipo] |
|
|
|
custom_address = st.text_input('Informe o endereço', custom_address_initial) |
|
radius_visible = True |
|
|
|
gmaps = googlemaps.Client(key='AIzaSyDoJ6C7NE2CHqFcaHTnhreOfgJeTk4uSH0') |
|
|
|
try: |
|
|
|
custom_address = custom_address.strip() |
|
if not custom_address.endswith(" - RS, Brazil"): |
|
custom_address += " - RS, Brazil" |
|
|
|
location = gmaps.geocode(custom_address)[0]['geometry']['location'] |
|
custom_lat, custom_lon = location['lat'], location['lng'] |
|
except (IndexError, GeocoderTimedOut): |
|
st.error("Erro: Não foi possível geocodificar o endereço fornecido. Por favor, verifique e tente novamente.") |
|
|
|
|
|
zoom_level = st.slider('Nível de zoom', min_value=1, max_value=15, value=zoom_level) |
|
|
|
|
|
if radius_visible: |
|
radius_in_meters = st.number_input('Selecione raio (em metros)', min_value=0, max_value=100000, value=2000) |
|
|
|
|
|
dorm_range = (int(data_tipo['Dorm'].min()), int(data_tipo['Dorm'].max())) |
|
banho_range = (int(data_tipo['Banh'].min()), int(data_tipo['Banh'].max())) |
|
vaga_range = (int(data_tipo['Vaga'].min()), int(data_tipo['Vaga'].max())) |
|
test_range = (int(data_tipo['Test'].min()), int(data_tipo['Test'].max())) |
|
|
|
|
|
atotal_range = st.slider('Área Total', float(data_tipo['Atotal'].min()), float(data_tipo['Atotal'].max()), (float(data_tipo['Atotal'].min()), float(data_tipo['Atotal'].max())), step=.1 if data_tipo['Atotal'].min() != data_tipo['Atotal'].max() else 0.1) |
|
apriv_range = st.slider('Área Privativa', float(data_tipo['Apriv'].min()), float(data_tipo['Apriv'].max()), (float(data_tipo['Apriv'].min()), float(data_tipo['Apriv'].max())), step=.1 if data_tipo['Apriv'].min() != data_tipo['Apriv'].max() else 0.1) |
|
|
|
if int(data_tipo['Dorm'].min()) != 0 and int(data_tipo['Dorm'].max()) != 0: |
|
dorm_range = st.slider('Dormitórios', int(data_tipo['Dorm'].min()), int(data_tipo['Dorm'].max()), (int(data_tipo['Dorm'].min()), int(data_tipo['Dorm'].max())), step=1 if data_tipo['Dorm'].min() != data_tipo['Dorm'].max() else 1) |
|
if int(data_tipo['Banh'].min()) != 0 and int(data_tipo['Banh'].max()) != 0: |
|
banho_range = st.slider('Banheiros', int(data_tipo['Banh'].min()), int(data_tipo['Banh'].max()), (int(data_tipo['Banh'].min()), int(data_tipo['Banh'].max())), step=1 if data_tipo['Banh'].min() != data_tipo['Banh'].max() else 1) |
|
if int(data_tipo['Vaga'].min()) != 0 and int(data_tipo['Vaga'].max()) != 0: |
|
vaga_range = st.slider('Vaga de estacionamento', int(data_tipo['Vaga'].min()), int(data_tipo['Vaga'].max()), (int(data_tipo['Vaga'].min()), int(data_tipo['Vaga'].max())), step=1 if data_tipo['Vaga'].min() != data_tipo['Vaga'].max() else 1) |
|
if int(data_tipo['Test'].min()) != 0 and int(data_tipo['Test'].max()) != 0: |
|
test_range = st.slider('Testada', int(data_tipo['Test'].min()), int(data_tipo['Test'].max()), (int(data_tipo['Test'].min()), int(data_tipo['Test'].max())), step=1 if data_tipo['Test'].min() != data_tipo['Test'].max() else 1) |
|
|
|
|
|
elev_checkbox = False |
|
esq_checkbox = False |
|
|
|
|
|
if int(data_tipo['Elevador'].min()) != 0 and int(data_tipo['Elevador'].max()) != 0: |
|
elev_checkbox = st.checkbox('Elevador') |
|
if int(data_tipo['Lot_pos'].min()) != 0 and int(data_tipo['Lot_pos'].max()) != 0: |
|
esq_checkbox = st.checkbox('Duas ou mais frentes') |
|
|
|
|
|
elev_value = 1 if elev_checkbox else 0 |
|
esq_value = 1 if esq_checkbox else 0 |
|
|
|
data_tipo = data_tipo[(data_tipo['Atotal'].between(atotal_range[0], atotal_range[1])) & |
|
(data_tipo['Apriv'].between(apriv_range[0], apriv_range[1])) & |
|
(data_tipo['Dorm'].between(dorm_range[0], dorm_range[1])) & |
|
(data_tipo['Banh'].between(banho_range[0], banho_range[1])) & |
|
(data_tipo['Vaga'].between(vaga_range[0], vaga_range[1])) & |
|
(data_tipo['Test'].between(test_range[0], test_range[1])) & |
|
(data_tipo['Elevador'] == elev_value) & |
|
(data_tipo['Lot_pos'] == esq_value)] |
|
|
|
|
|
st.sidebar.markdown(factor_html, unsafe_allow_html=True) |
|
st.sidebar.markdown(evo_html, unsafe_allow_html=True) |
|
|
|
filtered_data = data_tipo[data_tipo.apply(lambda x: calculate_distance(x['latitude'], x['longitude'], custom_lat, custom_lon), axis=1) <= radius_in_meters] |
|
filtered_data = filtered_data.dropna() |
|
|
|
|
|
st.markdown(f"""<style> |
|
.map {{ |
|
width: 100%; |
|
height: 100vh; |
|
}} |
|
</style>""", unsafe_allow_html=True) |
|
|
|
|
|
filtered_data['area_feature'] = np.where(filtered_data['Apriv'] != 0, filtered_data['Apriv'], filtered_data['Atotal']) |
|
|
|
|
|
filtered_data['target_column'] = np.where(filtered_data['Vunit_priv'] != 0, filtered_data['Vunit_priv'], filtered_data['Vunit_total']) |
|
|
|
|
|
predicted_target = knn_predict(filtered_data, 'target_column', ['latitude', 'longitude', 'area_feature']) |
|
|
|
|
|
filtered_data['Predicted_target'] = predicted_target |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with st.container(): |
|
folium_map = folium.Map(location=[custom_lat, custom_lon], zoom_start=zoom_level, control_scale=True) |
|
|
|
|
|
add_heatmap_layer(folium_map, filtered_data, 'Valor_Urb', 'RdBu_r') |
|
add_heatmap_layer(folium_map, filtered_data, 'Valor_Eqp', 'RdBu_r') |
|
add_heatmap_layer(folium_map, filtered_data, 'RENDA', 'RdBu_r') |
|
|
|
|
|
folium.LayerControl().add_to(folium_map) |
|
|
|
|
|
folium_static(folium_map) |
|
|
|
st.write("Dados:", filtered_data) |
|
|
|
k_threshold = 5 |
|
|
|
|
|
def bootstrap_stats(bound_data, num_samples=1000): |
|
|
|
bound_data = np.array(bound_data).reshape(-1, 1) |
|
|
|
|
|
bootstrapped_means = [] |
|
for _ in range(num_samples): |
|
bootstrap_sample = np.random.choice(bound_data.flatten(), len(bound_data), replace=True) |
|
bootstrapped_means.append(np.mean(bootstrap_sample)) |
|
|
|
|
|
lower_bound = np.percentile(bootstrapped_means, 25.) |
|
higher_bound = np.percentile(bootstrapped_means, 75.) |
|
|
|
return lower_bound, higher_bound |
|
|
|
|
|
predicted_target = knn_predict(filtered_data, 'Predicted_target', ['latitude', 'longitude', 'area_feature']) |
|
|
|
|
|
if 'Predicted_target' in filtered_data.columns and not np.all(predicted_target == 0): |
|
|
|
|
|
lower_bound, higher_bound = bootstrap_stats(filtered_data['target_column']) |
|
|
|
mean_value = np.mean(filtered_data['Predicted_target']) |
|
|
|
|
|
st.markdown("## **Resultado da Análise Estatística**") |
|
st.write(f"Valor médio (Reais/m²) para as características selecionadas: ${mean_value:.2f}$ Reais") |
|
st.write(f"Os valores podem variar entre ${lower_bound:.2f}$ e ${higher_bound:.2f}$ Reais, dependendo das características dos imóveis.") |
|
else: |
|
st.warning(f"**Dados insuficientes para inferência do valor. Mínimo necessário:** {k_threshold}") |