Spaces:
Sleeping
Sleeping
fschwartzer
commited on
Commit
•
a580030
1
Parent(s):
fff8af2
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,32 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
4 |
-
import openpyxl
|
5 |
from sklearn.neighbors import KNeighborsRegressor
|
6 |
from geopy.distance import geodesic
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Set wide mode
|
9 |
st.set_page_config(layout="wide")
|
10 |
|
@@ -30,11 +52,13 @@ st.markdown(
|
|
30 |
# Create a DataFrame with sample data
|
31 |
data = pd.read_excel('ven_ter_fim_PEDÓ.xlsx')
|
32 |
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
38 |
|
39 |
# Find the maximum distance between coordinates
|
40 |
max_distance = 0
|
@@ -68,17 +92,10 @@ with st.sidebar:
|
|
68 |
if radius_visible:
|
69 |
radius_in_meters = st.slider('Selecione raio (em metros)', min_value=100, max_value=5000, value=1000)
|
70 |
|
71 |
-
|
72 |
-
|
73 |
filtered_data = data[data.apply(lambda x: calculate_distance(x['latitude'], x['longitude'], custom_lat, custom_lon), axis=1) <= radius_in_meters]
|
74 |
-
filtered_data = filtered_data.dropna()
|
75 |
-
|
76 |
-
# Apply KNN and get predicted Vunit values
|
77 |
-
predicted_vunit = knn_predict(filtered_data, 'Vunit', knn_features)
|
78 |
-
|
79 |
-
# Add predicted Vunit values to filtered_data
|
80 |
-
filtered_data['Predicted_Vunit'] = predicted_vunit
|
81 |
-
|
82 |
|
83 |
# Add a custom CSS class to the map container
|
84 |
st.markdown(f"""<style>
|
@@ -88,35 +105,10 @@ st.markdown(f"""<style>
|
|
88 |
}}
|
89 |
</style>""", unsafe_allow_html=True)
|
90 |
|
91 |
-
# Wrap the map in a container with the custom CSS class
|
92 |
-
with st.container():
|
93 |
-
st.map(filtered_data, zoom=zoom_level, use_container_width=True)
|
94 |
-
|
95 |
-
# Function to apply KNN and return Vunit values
|
96 |
-
def knn_predict(df, target_column, features_columns, k=5):
|
97 |
-
# Separate features and target variable
|
98 |
-
X = df[features_columns]
|
99 |
-
y = df[target_column]
|
100 |
-
|
101 |
-
# Create KNN regressor
|
102 |
-
knn = KNeighborsRegressor(n_neighbors=k)
|
103 |
-
|
104 |
-
# Fit the model
|
105 |
-
knn.fit(X, y)
|
106 |
-
|
107 |
-
# Use the model to predict Vunit for the filtered_data
|
108 |
-
predictions = knn.predict(filtered_data[features_columns])
|
109 |
-
|
110 |
-
return predictions
|
111 |
-
|
112 |
-
# Features columns for KNN
|
113 |
-
knn_features = ['latitude', 'longitude', 'Area'] # Add other relevant features
|
114 |
-
|
115 |
# Check if KNN should be applied
|
116 |
if selected_coords == 'Custom' and radius_visible:
|
117 |
# Apply KNN and get predicted Vunit values
|
118 |
-
predicted_vunit = knn_predict(
|
119 |
-
|
120 |
# Add predicted Vunit values to filtered_data
|
121 |
filtered_data['Predicted_Vunit'] = predicted_vunit
|
122 |
|
@@ -128,4 +120,4 @@ with st.container():
|
|
128 |
# Display the predicted Vunit values if applicable
|
129 |
if 'Predicted_Vunit' in filtered_data.columns:
|
130 |
st.write("Predicted Vunit Values:")
|
131 |
-
st.write(filtered_data[['latitude', 'longitude', 'Vunit', 'Predicted_Vunit']])
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
import numpy as np
|
|
|
4 |
from sklearn.neighbors import KNeighborsRegressor
|
5 |
from geopy.distance import geodesic
|
6 |
|
7 |
+
# Function to calculate distance in meters between two coordinates
|
8 |
+
def calculate_distance(lat1, lon1, lat2, lon2):
|
9 |
+
coords_1 = (lat1, lon1)
|
10 |
+
coords_2 = (lat2, lon2)
|
11 |
+
return geodesic(coords_1, coords_2).meters
|
12 |
+
|
13 |
+
# Function to apply KNN and return Vunit values
|
14 |
+
def knn_predict(df, target_column, features_columns, k=5):
|
15 |
+
# Separate features and target variable
|
16 |
+
X = df[features_columns]
|
17 |
+
y = df[target_column]
|
18 |
+
|
19 |
+
# Create KNN regressor
|
20 |
+
knn = KNeighborsRegressor(n_neighbors=k)
|
21 |
+
|
22 |
+
# Fit the model
|
23 |
+
knn.fit(X, y)
|
24 |
+
|
25 |
+
# Use the model to predict Vunit for the filtered_data
|
26 |
+
predictions = knn.predict(df[features_columns])
|
27 |
+
|
28 |
+
return predictions
|
29 |
+
|
30 |
# Set wide mode
|
31 |
st.set_page_config(layout="wide")
|
32 |
|
|
|
52 |
# Create a DataFrame with sample data
|
53 |
data = pd.read_excel('ven_ter_fim_PEDÓ.xlsx')
|
54 |
|
55 |
+
# Initialize variables to avoid NameError
|
56 |
+
selected_coords = 'Custom'
|
57 |
+
radius_visible = True
|
58 |
+
custom_lat = -29.45086
|
59 |
+
custom_lon = -51.9847
|
60 |
+
radius_in_meters = 1000
|
61 |
+
filtered_data = data # Initialize with the entire dataset
|
62 |
|
63 |
# Find the maximum distance between coordinates
|
64 |
max_distance = 0
|
|
|
92 |
if radius_visible:
|
93 |
radius_in_meters = st.slider('Selecione raio (em metros)', min_value=100, max_value=5000, value=1000)
|
94 |
|
95 |
+
# Filter data based on the radius
|
96 |
+
if selected_coords == 'Custom':
|
97 |
filtered_data = data[data.apply(lambda x: calculate_distance(x['latitude'], x['longitude'], custom_lat, custom_lon), axis=1) <= radius_in_meters]
|
98 |
+
filtered_data = filtered_data.dropna() # Drop rows with NaN values
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
# Add a custom CSS class to the map container
|
101 |
st.markdown(f"""<style>
|
|
|
105 |
}}
|
106 |
</style>""", unsafe_allow_html=True)
|
107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
# Check if KNN should be applied
|
109 |
if selected_coords == 'Custom' and radius_visible:
|
110 |
# Apply KNN and get predicted Vunit values
|
111 |
+
predicted_vunit = knn_predict(filtered_data, 'Vunit', ['latitude', 'longitude', 'Area']) # Update with your features
|
|
|
112 |
# Add predicted Vunit values to filtered_data
|
113 |
filtered_data['Predicted_Vunit'] = predicted_vunit
|
114 |
|
|
|
120 |
# Display the predicted Vunit values if applicable
|
121 |
if 'Predicted_Vunit' in filtered_data.columns:
|
122 |
st.write("Predicted Vunit Values:")
|
123 |
+
st.write(filtered_data[['latitude', 'longitude', 'Vunit', 'Predicted_Vunit']])
|