Spaces:
Sleeping
Sleeping
create Dados
#1
by
ricardoadriano
- opened
- Dados/AmesHousing.csv +0 -0
- Dockerfile +10 -19
- dados +0 -0
- requirements.txt +3 -10
- src/streamlit_app.py +38 -213
Dados/AmesHousing.csv
DELETED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Dockerfile
CHANGED
|
@@ -1,29 +1,20 @@
|
|
| 1 |
-
|
| 2 |
-
FROM python:3.9-slim
|
| 3 |
|
| 4 |
-
|
| 5 |
-
ENV DEBIAN_FRONTEND=noninteractive
|
| 6 |
|
| 7 |
-
# Instalar dependências do sistema necessárias para algumas libs (ex: pandas, matplotlib)
|
| 8 |
RUN apt-get update && apt-get install -y \
|
| 9 |
build-essential \
|
| 10 |
-
|
|
|
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
| 17 |
-
COPY requirements.txt /app/
|
| 18 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
|
| 20 |
-
|
| 21 |
-
COPY ./src /app/src
|
| 22 |
-
COPY ./Dados /app/Dados
|
| 23 |
-
COPY requirements.txt /app/
|
| 24 |
|
| 25 |
-
|
| 26 |
-
WORKDIR /app/src/
|
| 27 |
|
| 28 |
-
|
| 29 |
-
CMD ["streamlit", "run", "streamlit_app.py", "--server.port", "8501", "--server.address", "0.0.0.0"]
|
|
|
|
| 1 |
+
FROM python:3.13.5-slim
|
|
|
|
| 2 |
|
| 3 |
+
WORKDIR /app
|
|
|
|
| 4 |
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
build-essential \
|
| 7 |
+
curl \
|
| 8 |
+
git \
|
| 9 |
&& rm -rf /var/lib/apt/lists/*
|
| 10 |
|
| 11 |
+
COPY requirements.txt ./
|
| 12 |
+
COPY src/ ./src/
|
| 13 |
|
| 14 |
+
RUN pip3 install -r requirements.txt
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
EXPOSE 8501
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
|
|
|
|
| 19 |
|
| 20 |
+
ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
|
|
|
dados
ADDED
|
File without changes
|
requirements.txt
CHANGED
|
@@ -1,10 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
pandas
|
| 3 |
-
|
| 4 |
-
seaborn==0.11.2
|
| 5 |
-
plotly==5.3.0
|
| 6 |
-
Pillow==9.2.0
|
| 7 |
-
scipy==1.9.3
|
| 8 |
-
statsmodels==0.13.5
|
| 9 |
-
numpy==1.24.0
|
| 10 |
-
scikit-learn==1.3.2
|
|
|
|
| 1 |
+
altair
|
| 2 |
+
pandas
|
| 3 |
+
streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/streamlit_app.py
CHANGED
|
@@ -1,215 +1,40 @@
|
|
| 1 |
-
|
| 2 |
-
# coding: utf-8
|
| 3 |
-
|
| 4 |
-
# =====================================================
|
| 5 |
-
# Dashboard - Testes de Hipóteses com AmesHousing
|
| 6 |
-
# =====================================================
|
| 7 |
-
|
| 8 |
-
import streamlit as st
|
| 9 |
-
import pandas as pd
|
| 10 |
-
import matplotlib.pyplot as plt
|
| 11 |
-
import seaborn as sns
|
| 12 |
-
import plotly.express as px
|
| 13 |
import numpy as np
|
|
|
|
|
|
|
| 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 |
-
se = np.sqrt(p_pop*(1-p_pop)/n)
|
| 51 |
-
z = (p_sample - p_pop)/se
|
| 52 |
-
p_value = 2*(1 - stats.norm.cdf(abs(z)))
|
| 53 |
-
|
| 54 |
-
st.write(f"**Z** = {z:.4f}")
|
| 55 |
-
st.write(f"**p-valor** = {p_value:.4f}")
|
| 56 |
-
if p_value < alpha_prop:
|
| 57 |
-
st.write("**Rejeitamos H0**: diferença significativa.")
|
| 58 |
-
else:
|
| 59 |
-
st.write("**Não rejeitamos H0**: sem diferença significativa.")
|
| 60 |
-
|
| 61 |
-
# -----------------------------------------------------
|
| 62 |
-
# Aba 2: Análise AmesHousing
|
| 63 |
-
# -----------------------------------------------------
|
| 64 |
-
with tabs[1]:
|
| 65 |
-
st.subheader("Análise de Variância - AmesHousing Dataset")
|
| 66 |
-
st.markdown("---")
|
| 67 |
-
|
| 68 |
-
# Leitura do CSV
|
| 69 |
-
casa_data = pd.read_csv("../Dados/AmesHousing.csv")
|
| 70 |
-
casa_data.columns = casa_data.columns.str.strip().str.replace(" ", "_")
|
| 71 |
-
|
| 72 |
-
# -----------------------------
|
| 73 |
-
# Amostragem
|
| 74 |
-
# -----------------------------
|
| 75 |
-
n_amostra = st.session_state.get("n_sample", len(casa_data))
|
| 76 |
-
if n_amostra < len(casa_data):
|
| 77 |
-
dados = casa_data.sample(n=n_amostra, random_state=42)
|
| 78 |
-
else:
|
| 79 |
-
dados = casa_data.copy()
|
| 80 |
-
|
| 81 |
-
# -----------------------------
|
| 82 |
-
# Filtro interativo no sidebar
|
| 83 |
-
# -----------------------------
|
| 84 |
-
st.sidebar.markdown("### Filtros AmesHousing")
|
| 85 |
-
|
| 86 |
-
bairros = st.sidebar.multiselect(
|
| 87 |
-
"Selecione bairros",
|
| 88 |
-
options=sorted(dados["Neighborhood"].dropna().unique()),
|
| 89 |
-
default=None
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
# Aplicar filtro
|
| 93 |
-
dados_filtrados = dados.copy()
|
| 94 |
-
if bairros:
|
| 95 |
-
dados_filtrados = dados_filtrados[dados_filtrados["Neighborhood"].isin(bairros)]
|
| 96 |
-
|
| 97 |
-
# -------------------------------------------------
|
| 98 |
-
# Análise Exploratória
|
| 99 |
-
# -------------------------------------------------
|
| 100 |
-
st.markdown("### Distribuição do Preço de Venda")
|
| 101 |
-
if not dados_filtrados.empty:
|
| 102 |
-
fig, ax = plt.subplots(figsize=(8,5))
|
| 103 |
-
sns.histplot(dados_filtrados['SalePrice'], kde=True, ax=ax)
|
| 104 |
-
ax.set_title("Distribuição do Preço de Venda")
|
| 105 |
-
st.pyplot(fig)
|
| 106 |
-
else:
|
| 107 |
-
st.warning("Nenhum dado disponível com os filtros aplicados.")
|
| 108 |
-
|
| 109 |
-
# Boxplots
|
| 110 |
-
st.markdown("### Boxplots das Variáveis Selecionadas")
|
| 111 |
-
variavel = st.selectbox(
|
| 112 |
-
"Escolha a variável categórica para comparar preços:",
|
| 113 |
-
["Neighborhood","Garage_Type","Fireplaces"]
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
if not dados_filtrados.empty:
|
| 117 |
-
if len(dados_filtrados[variavel].dropna().unique()) > 1:
|
| 118 |
-
fig2, ax2 = plt.subplots(figsize=(12,6))
|
| 119 |
-
sns.boxplot(x=variavel, y="SalePrice", data=dados_filtrados, ax=ax2)
|
| 120 |
-
plt.xticks(rotation=90)
|
| 121 |
-
ax2.set_title(f"Preço de Venda por {variavel}")
|
| 122 |
-
st.pyplot(fig2)
|
| 123 |
-
else:
|
| 124 |
-
st.warning(f"Não é possível gerar boxplot: apenas uma categoria em {variavel} após os filtros.")
|
| 125 |
-
|
| 126 |
-
# Scatter interativo (média de preço por bairro)
|
| 127 |
-
st.markdown("### Preço Médio de Venda por Bairro")
|
| 128 |
-
if not dados_filtrados.empty:
|
| 129 |
-
bairro_grouped = dados_filtrados.groupby('Neighborhood').agg(
|
| 130 |
-
count=('SalePrice','size'),
|
| 131 |
-
mean_price=('SalePrice','mean')
|
| 132 |
-
).reset_index()
|
| 133 |
-
|
| 134 |
-
bairro_filtered = bairro_grouped[bairro_grouped['count'] >= 5]
|
| 135 |
-
if not bairro_filtered.empty:
|
| 136 |
-
fig3 = px.scatter(
|
| 137 |
-
bairro_filtered,
|
| 138 |
-
x='mean_price',
|
| 139 |
-
y='Neighborhood',
|
| 140 |
-
size='count',
|
| 141 |
-
color='Neighborhood',
|
| 142 |
-
title='Preço Médio de Venda vs Bairro (Ames, Iowa)',
|
| 143 |
-
labels={'mean_price': 'Preço Médio de Venda', 'Neighborhood':'Bairro'},
|
| 144 |
-
opacity=0.8
|
| 145 |
-
)
|
| 146 |
-
st.plotly_chart(fig3, use_container_width=True)
|
| 147 |
-
else:
|
| 148 |
-
st.warning("Não há bairros suficientes após filtros para gerar o gráfico.")
|
| 149 |
-
|
| 150 |
-
# -------------------------------------------------
|
| 151 |
-
# ANOVA
|
| 152 |
-
# -------------------------------------------------
|
| 153 |
-
st.markdown("### ANOVA para Neighborhood, Garage_Type e Fireplaces")
|
| 154 |
-
alpha = st.sidebar.slider(
|
| 155 |
-
"Nível de significância (α) - ANOVA AmesHousing",
|
| 156 |
-
0.01,0.10,0.05,0.01,
|
| 157 |
-
key="alpha_ames"
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
if not dados_filtrados.empty:
|
| 161 |
-
for nome in ["Neighborhood", "Garage_Type", "Fireplaces"]:
|
| 162 |
-
categorias = dados_filtrados[nome].dropna().unique()
|
| 163 |
-
if len(categorias) < 2:
|
| 164 |
-
st.warning(f"ANOVA não pôde ser realizada para **{nome}** (menos de 2 grupos após os filtros).")
|
| 165 |
-
continue
|
| 166 |
-
|
| 167 |
-
modelo = ols(f'SalePrice ~ C({nome})', data=dados_filtrados).fit()
|
| 168 |
-
st.markdown(f"#### ANOVA - {nome}")
|
| 169 |
-
anova = sm.stats.anova_lm(modelo, typ=2)
|
| 170 |
-
st.dataframe(anova)
|
| 171 |
-
|
| 172 |
-
# -------------------------------------------------
|
| 173 |
-
# Validação dos Pressupostos
|
| 174 |
-
# -------------------------------------------------
|
| 175 |
-
st.markdown("### Validação dos Pressupostos da ANOVA")
|
| 176 |
-
|
| 177 |
-
st.markdown("#### Teste de Normalidade (Shapiro-Wilk)")
|
| 178 |
-
for nome in ["Neighborhood","Garage_Type","Fireplaces"]:
|
| 179 |
-
categorias = dados_filtrados[nome].dropna().unique()
|
| 180 |
-
if len(categorias) < 2:
|
| 181 |
-
st.warning(f"Shapiro-Wilk não pôde ser realizado para **{nome}** (menos de 2 grupos após os filtros).")
|
| 182 |
-
continue
|
| 183 |
-
|
| 184 |
-
modelo = ols(f'SalePrice ~ C({nome})', data=dados_filtrados).fit()
|
| 185 |
-
residuos = modelo.resid
|
| 186 |
-
stat, p = shapiro(residuos.dropna())
|
| 187 |
-
st.write(f"{nome}: estatística={stat:.3f}, p={p:.3f} "
|
| 188 |
-
+ ("resíduos normais" if p >= alpha else "violação de normalidade"))
|
| 189 |
-
|
| 190 |
-
st.markdown("#### Teste de Homocedasticidade (Levene)")
|
| 191 |
-
for nome in ["Neighborhood","Garage_Type","Fireplaces"]:
|
| 192 |
-
grupos = [grupo["SalePrice"].dropna() for _, grupo in dados_filtrados.groupby(nome)]
|
| 193 |
-
if len(grupos) < 2:
|
| 194 |
-
st.warning(f"Levene não pôde ser realizado para **{nome}** (menos de 2 grupos após os filtros).")
|
| 195 |
-
continue
|
| 196 |
-
|
| 197 |
-
stat, p = levene(*grupos)
|
| 198 |
-
st.write(f"{nome}: estatística={stat:.3f}, p={p:.3f} "
|
| 199 |
-
+ ("variâncias iguais" if p >= alpha else "variâncias diferentes"))
|
| 200 |
-
|
| 201 |
-
# -------------------------------------------------
|
| 202 |
-
# Kruskal-Wallis
|
| 203 |
-
# -------------------------------------------------
|
| 204 |
-
st.markdown("### Teste não-paramétrico (Kruskal-Wallis)")
|
| 205 |
-
for nome in ["Neighborhood","Garage_Type","Fireplaces"]:
|
| 206 |
-
grupos = [grupo["SalePrice"].dropna() for _, grupo in dados_filtrados.groupby(nome)]
|
| 207 |
-
if len(grupos) < 2:
|
| 208 |
-
st.warning(f"Kruskal-Wallis não pôde ser realizado para **{nome}** (menos de 2 grupos após os filtros).")
|
| 209 |
-
continue
|
| 210 |
-
|
| 211 |
-
stat, p = kruskal(*grupos)
|
| 212 |
-
st.write(f"{nome}: estatística={stat:.3f}, p={p:.3f} "
|
| 213 |
-
+ ("diferenças significativas" if p < alpha else "sem diferença significativa"))
|
| 214 |
-
|
| 215 |
-
|
|
|
|
| 1 |
+
import altair as alt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import streamlit as st
|
| 5 |
|
| 6 |
+
"""
|
| 7 |
+
# Welcome to Streamlit!
|
| 8 |
+
|
| 9 |
+
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
+
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
+
forums](https://discuss.streamlit.io).
|
| 12 |
+
|
| 13 |
+
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
+
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
+
|
| 19 |
+
indices = np.linspace(0, 1, num_points)
|
| 20 |
+
theta = 2 * np.pi * num_turns * indices
|
| 21 |
+
radius = indices
|
| 22 |
+
|
| 23 |
+
x = radius * np.cos(theta)
|
| 24 |
+
y = radius * np.sin(theta)
|
| 25 |
+
|
| 26 |
+
df = pd.DataFrame({
|
| 27 |
+
"x": x,
|
| 28 |
+
"y": y,
|
| 29 |
+
"idx": indices,
|
| 30 |
+
"rand": np.random.randn(num_points),
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
+
.mark_point(filled=True)
|
| 35 |
+
.encode(
|
| 36 |
+
x=alt.X("x", axis=None),
|
| 37 |
+
y=alt.Y("y", axis=None),
|
| 38 |
+
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
+
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
+
))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|