Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import seaborn as sns
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from sklearn.model_selection import train_test_split
|
7 |
+
from sklearn.linear_model import LinearRegression
|
8 |
+
from sklearn.metrics import mean_squared_error, r2_score
|
9 |
+
|
10 |
+
# T铆tulo de la aplicaci贸n
|
11 |
+
st.title("Regresi贸n Lineal Simple: Predicci贸n de Peso basado en Altura")
|
12 |
+
|
13 |
+
# Cargar el dataset
|
14 |
+
url = "https://raw.githubusercontent.com/burnoutminer/Heights-and-Weights-Dataset/master/data.csv"
|
15 |
+
data = pd.read_csv(url)
|
16 |
+
|
17 |
+
# Mostrar el dataset en la aplicaci贸n
|
18 |
+
st.subheader("Dataset de Altura y Peso")
|
19 |
+
st.write(data.head())
|
20 |
+
|
21 |
+
# Exploraci贸n de datos
|
22 |
+
st.subheader("Relaci贸n entre Altura y Peso")
|
23 |
+
fig, ax = plt.subplots()
|
24 |
+
sns.scatterplot(x='Height', y='Weight', data=data, ax=ax)
|
25 |
+
plt.title('Relaci贸n entre Altura y Peso')
|
26 |
+
plt.xlabel('Altura (pulgadas)')
|
27 |
+
plt.ylabel('Peso (libras)')
|
28 |
+
st.pyplot(fig)
|
29 |
+
|
30 |
+
# Dividir el dataset en entrenamiento y prueba
|
31 |
+
X = data[['Height']] # Variable independiente (altura)
|
32 |
+
y = data['Weight'] # Variable dependiente (peso)
|
33 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
34 |
+
|
35 |
+
# Crear y entrenar el modelo de regresi贸n lineal simple
|
36 |
+
model = LinearRegression()
|
37 |
+
model.fit(X_train, y_train)
|
38 |
+
|
39 |
+
# Predecir en el conjunto de prueba
|
40 |
+
y_pred = model.predict(X_test)
|
41 |
+
|
42 |
+
# Evaluar el modelo
|
43 |
+
mse = mean_squared_error(y_test, y_pred)
|
44 |
+
r2 = r2_score(y_test, y_pred)
|
45 |
+
|
46 |
+
# Mostrar m茅tricas del modelo
|
47 |
+
st.subheader("Evaluaci贸n del Modelo")
|
48 |
+
st.write(f'Error Cuadr谩tico Medio (MSE): {mse:.2f}')
|
49 |
+
st.write(f'Coeficiente de Determinaci贸n (R虏): {r2:.2f}')
|
50 |
+
|
51 |
+
# Gr谩fico de regresi贸n con l铆nea de tendencia
|
52 |
+
st.subheader("L铆nea de Regresi贸n")
|
53 |
+
fig, ax = plt.subplots()
|
54 |
+
plt.scatter(X_test, y_test, color='blue', label='Datos reales')
|
55 |
+
plt.plot(X_test, y_pred, color='red', label='L铆nea de regresi贸n')
|
56 |
+
plt.title('Regresi贸n Lineal Simple: Altura vs Peso')
|
57 |
+
plt.xlabel('Altura (pulgadas)')
|
58 |
+
plt.ylabel('Peso (libras)')
|
59 |
+
plt.legend()
|
60 |
+
st.pyplot(fig)
|
61 |
+
|
62 |
+
# Coeficientes del modelo
|
63 |
+
st.subheader("Coeficientes del Modelo")
|
64 |
+
st.write(f'Intercepto (b0): {model.intercept_:.2f}')
|
65 |
+
st.write(f'Coeficiente de Altura (b1): {model.coef_[0]:.2f}')
|
66 |
+
|
67 |
+
# Entrada de usuario para predecir el peso
|
68 |
+
st.subheader("Predicci贸n de Peso")
|
69 |
+
height_input = st.number_input("Ingresa la altura (en pulgadas):", min_value=50.0, max_value=100.0, value=70.0)
|
70 |
+
|
71 |
+
# Predecir el peso basado en la altura ingresada
|
72 |
+
if st.button("Predecir Peso"):
|
73 |
+
predicted_weight = model.predict([[height_input]])
|
74 |
+
st.write(f"El peso predicho para una altura de {height_input} pulgadas es: **{predicted_weight[0]:.2f} libras**.")
|