Spaces:
Sleeping
Sleeping
fschwartzer
commited on
Commit
•
0bc1a23
1
Parent(s):
f96846f
Update app.py
Browse files
app.py
CHANGED
@@ -12,19 +12,22 @@ def calculate_distance(lat1, lon1, lat2, lon2):
|
|
12 |
coords_2 = (lat2, lon2)
|
13 |
return geodesic(coords_1, coords_2).meters
|
14 |
|
15 |
-
# Function to apply KNN and return V_oferta values
|
16 |
def knn_predict(df, target_column, features_columns, k=5):
|
17 |
# Separate features and target variable
|
18 |
X = df[features_columns]
|
19 |
y = df[target_column]
|
20 |
|
|
|
|
|
|
|
|
|
21 |
# Create KNN regressor
|
22 |
knn = KNeighborsRegressor(n_neighbors=k)
|
23 |
|
24 |
# Fit the model
|
25 |
knn.fit(X, y)
|
26 |
|
27 |
-
# Use the model to predict
|
28 |
predictions = knn.predict(df[features_columns])
|
29 |
|
30 |
return predictions
|
@@ -223,23 +226,18 @@ filtered_data['Predicted_target'] = predicted_target
|
|
223 |
with st.container():
|
224 |
st.map(filtered_data, zoom=zoom_level, use_container_width=True)
|
225 |
st.write("Dados:", filtered_data) # Debug: Print filtered_data
|
226 |
-
|
227 |
-
# Set the threshold (k) for the number of data points
|
228 |
-
k_threshold = 5 # Adjust the threshold as needed
|
229 |
|
230 |
-
#
|
231 |
-
|
232 |
-
# Determine which area feature to use for prediction
|
233 |
-
filtered_data['area_feature'] = np.where(filtered_data['Apriv'] != 0, filtered_data['Apriv'], filtered_data['Atotal'])
|
234 |
-
|
235 |
-
# Apply KNN and get predicted Predicted_target values
|
236 |
-
predicted_target = knn_predict(filtered_data, 'Predicted_target', ['latitude', 'longitude', 'area_feature']) # Update with your features
|
237 |
|
|
|
|
|
238 |
# Add predicted Predicted_target values to filtered_data
|
239 |
filtered_data['Predicted_target'] = predicted_target
|
240 |
|
241 |
# Display the predicted Predicted_target values
|
242 |
st.write("Valores (R$/m²) previstos com algoritmo KNN:")
|
243 |
st.write(filtered_data[['Localização', 'Atotal', 'Apriv', 'Vunit_total', 'Vunit_priv', 'Predicted_target']])
|
|
|
244 |
else:
|
245 |
st.warning(f"Dados insuficientes para inferência do valor. Mínimo necessário: {k_threshold}")
|
|
|
12 |
coords_2 = (lat2, lon2)
|
13 |
return geodesic(coords_1, coords_2).meters
|
14 |
|
|
|
15 |
def knn_predict(df, target_column, features_columns, k=5):
|
16 |
# Separate features and target variable
|
17 |
X = df[features_columns]
|
18 |
y = df[target_column]
|
19 |
|
20 |
+
# Check if there is enough data for prediction
|
21 |
+
if len(X) < k:
|
22 |
+
return np.zeros(len(X)) # Return an array of zeros if there isn't enough data
|
23 |
+
|
24 |
# Create KNN regressor
|
25 |
knn = KNeighborsRegressor(n_neighbors=k)
|
26 |
|
27 |
# Fit the model
|
28 |
knn.fit(X, y)
|
29 |
|
30 |
+
# Use the model to predict target_column for the filtered_data
|
31 |
predictions = knn.predict(df[features_columns])
|
32 |
|
33 |
return predictions
|
|
|
226 |
with st.container():
|
227 |
st.map(filtered_data, zoom=zoom_level, use_container_width=True)
|
228 |
st.write("Dados:", filtered_data) # Debug: Print filtered_data
|
|
|
|
|
|
|
229 |
|
230 |
+
# Apply KNN and get predicted Predicted_target values
|
231 |
+
predicted_target = knn_predict(filtered_data, 'Predicted_target', ['latitude', 'longitude', 'area_feature']) # Update with your features
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
+
# Check if there are predictions to display
|
234 |
+
if 'Predicted_target' in filtered_data.columns and not np.all(predicted_target == 0):
|
235 |
# Add predicted Predicted_target values to filtered_data
|
236 |
filtered_data['Predicted_target'] = predicted_target
|
237 |
|
238 |
# Display the predicted Predicted_target values
|
239 |
st.write("Valores (R$/m²) previstos com algoritmo KNN:")
|
240 |
st.write(filtered_data[['Localização', 'Atotal', 'Apriv', 'Vunit_total', 'Vunit_priv', 'Predicted_target']])
|
241 |
+
else:
|
242 |
else:
|
243 |
st.warning(f"Dados insuficientes para inferência do valor. Mínimo necessário: {k_threshold}")
|