Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
|
|
|
| 5 |
from sklearn.model_selection import train_test_split
|
| 6 |
from sklearn.linear_model import LinearRegression
|
| 7 |
from sklearn.tree import DecisionTreeClassifier
|
|
@@ -60,29 +61,39 @@ if uploaded_file is not None:
|
|
| 60 |
# 排序
|
| 61 |
feature_importance = feature_importance.sort_values('Random Forest', ascending=False)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# 繪製特徵重要性圖表
|
| 64 |
-
def plot_importance(
|
| 65 |
-
plt.figure(figsize=(
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
plt.xlabel('Features')
|
| 69 |
plt.ylabel('Importance')
|
| 70 |
-
plt.xticks(rotation=45, ha='right')
|
|
|
|
| 71 |
st.pyplot(plt)
|
| 72 |
|
| 73 |
# Streamlit UI
|
| 74 |
-
st.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# 下拉選擇模型
|
| 78 |
-
model = st.selectbox("選擇模型", ["Linear Regression", "CART", "Random Forest"])
|
| 79 |
-
|
| 80 |
# 顯示圖表
|
| 81 |
-
plot_importance(
|
| 82 |
|
| 83 |
# 顯示數據框
|
| 84 |
-
st.write(
|
| 85 |
-
st.dataframe(feature_importance
|
| 86 |
|
| 87 |
else:
|
| 88 |
st.error("上傳的檔案中找不到 'target' 欄位,請確認檔案格式。")
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
+
import seaborn as sns
|
| 6 |
from sklearn.model_selection import train_test_split
|
| 7 |
from sklearn.linear_model import LinearRegression
|
| 8 |
from sklearn.tree import DecisionTreeClassifier
|
|
|
|
| 61 |
# 排序
|
| 62 |
feature_importance = feature_importance.sort_values('Random Forest', ascending=False)
|
| 63 |
|
| 64 |
+
# 繪製相關矩陣
|
| 65 |
+
st.write("### 相關矩陣")
|
| 66 |
+
corr_matrix = df.corr()
|
| 67 |
+
plt.figure(figsize=(10, 8))
|
| 68 |
+
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm', fmt='.2f', linewidths=0.5)
|
| 69 |
+
st.pyplot(plt)
|
| 70 |
+
|
| 71 |
# 繪製特徵重要性圖表
|
| 72 |
+
def plot_importance():
|
| 73 |
+
plt.figure(figsize=(12, 8))
|
| 74 |
+
width = 0.25 # 條形圖寬度
|
| 75 |
+
indices = np.arange(len(feature_importance['Feature']))
|
| 76 |
+
|
| 77 |
+
plt.bar(indices - width, feature_importance['Linear Regression'], width=width, label='Linear Regression')
|
| 78 |
+
plt.bar(indices, feature_importance['CART'], width=width, label='CART')
|
| 79 |
+
plt.bar(indices + width, feature_importance['Random Forest'], width=width, label='Random Forest')
|
| 80 |
+
|
| 81 |
+
plt.title('Feature Importance Comparison Across Models')
|
| 82 |
plt.xlabel('Features')
|
| 83 |
plt.ylabel('Importance')
|
| 84 |
+
plt.xticks(indices, feature_importance['Feature'], rotation=45, ha='right')
|
| 85 |
+
plt.legend()
|
| 86 |
st.pyplot(plt)
|
| 87 |
|
| 88 |
# Streamlit UI
|
| 89 |
+
st.write("### 特徵重要性對比圖表 (Linear Regression, CART, Random Forest)")
|
| 90 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
# 顯示圖表
|
| 92 |
+
plot_importance()
|
| 93 |
|
| 94 |
# 顯示數據框
|
| 95 |
+
st.write("### 特徵重要性數據表")
|
| 96 |
+
st.dataframe(feature_importance)
|
| 97 |
|
| 98 |
else:
|
| 99 |
st.error("上傳的檔案中找不到 'target' 欄位,請確認檔案格式。")
|