Spaces:
Sleeping
Sleeping
Commit
·
598c992
1
Parent(s):
44725d4
Fix labels and change chart type
Browse files- page_shopping.py +30 -12
page_shopping.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
from kmodes.kmodes import KModes
|
3 |
from matplotlib.font_manager import FontProperties
|
4 |
import matplotlib.pyplot as plt
|
|
|
5 |
import pandas as pd
|
6 |
import numpy as np
|
7 |
from fields.prod_feat_flat_fields import prod_feat_flat_fields
|
@@ -13,21 +14,37 @@ def show(df):
|
|
13 |
chinese_font = FontProperties(fname='mingliu.ttf', size=12)
|
14 |
st.title("Shopping")
|
15 |
st.write("Clustering Students based on Product Feature choices")
|
16 |
-
|
|
|
17 |
clusters = perform_kmodes_clustering(df, prod_feat_flat_fields)
|
18 |
-
|
19 |
show_radar_chart(clusters, font_prop=chinese_font)
|
|
|
|
|
20 |
|
21 |
-
def show_boycott_count(df):
|
22 |
# Count the number of people who have invested and who have not
|
23 |
-
investment_count = df["你/妳有沒有抵制過某公司?"].value_counts()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
# Display the DataFrame as a table in Streamlit
|
30 |
-
st.table(investment_table)
|
31 |
|
32 |
|
33 |
def perform_kmodes_clustering(df, feature_columns, n_clusters=3):
|
@@ -58,9 +75,9 @@ def perform_kmodes_clustering(df, feature_columns, n_clusters=3):
|
|
58 |
def show_radar_chart(clusters, font_prop):
|
59 |
|
60 |
df_dict={
|
61 |
-
'
|
62 |
-
'
|
63 |
-
'
|
64 |
}
|
65 |
|
66 |
feature_translations_dict = dict(zip(prod_feat_flat_fields, feature_translations))
|
@@ -100,6 +117,7 @@ def show_radar_chart(clusters, font_prop):
|
|
100 |
ax.fill(angles, data, alpha=0.25)
|
101 |
|
102 |
# Add legend
|
|
|
103 |
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
|
104 |
|
105 |
# Add a title
|
|
|
2 |
from kmodes.kmodes import KModes
|
3 |
from matplotlib.font_manager import FontProperties
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
import pandas as pd
|
7 |
import numpy as np
|
8 |
from fields.prod_feat_flat_fields import prod_feat_flat_fields
|
|
|
14 |
chinese_font = FontProperties(fname='mingliu.ttf', size=12)
|
15 |
st.title("Shopping")
|
16 |
st.write("Clustering Students based on Product Feature choices")
|
17 |
+
st.title("Boycott Count")
|
18 |
+
show_boycott_count(df, font_prop=chinese_font)
|
19 |
clusters = perform_kmodes_clustering(df, prod_feat_flat_fields)
|
20 |
+
st.title("Feature Preferences")
|
21 |
show_radar_chart(clusters, font_prop=chinese_font)
|
22 |
+
st.title("Feature Preferences")
|
23 |
+
plot_feature_preferences(clusters, font_prop=chinese_font)
|
24 |
|
25 |
+
def show_boycott_count(df, font_prop):
|
26 |
# Count the number of people who have invested and who have not
|
27 |
+
investment_count = df["你/妳有沒有抵制過某公司?"].value_counts().reset_index()
|
28 |
+
investment_count.columns = ['Boycott', 'Count']
|
29 |
+
|
30 |
+
# Create a bar chart using seaborn
|
31 |
+
plt.figure(figsize=(10, 6))
|
32 |
+
barplot = sns.barplot(x='Boycott', y='Count', data=investment_count, palette='viridis')
|
33 |
+
ax = plt.gca() # Get the current Axes instance on the current figure matching the given keyword args, or create one.
|
34 |
+
ax.set_xticklabels(ax.get_xticklabels(), fontproperties=font_prop)
|
35 |
|
36 |
+
# Add labels and title
|
37 |
+
plt.xlabel('Have you ever boycotted a company?', fontsize=12, fontproperties=font_prop)
|
38 |
+
plt.ylabel('Count', fontsize=12, fontproperties=font_prop)
|
39 |
+
plt.title("Number of People Who Have/Haven't Boycotted a Company", fontsize=16, fontproperties=font_prop)
|
40 |
+
|
41 |
+
# Display values on the bars
|
42 |
+
for index, value in enumerate(investment_count['Count']):
|
43 |
+
plt.text(index, value, str(value), ha='center', va='bottom', fontproperties=font_prop)
|
44 |
+
|
45 |
+
# Display the chart in Streamlit
|
46 |
+
st.pyplot(plt)
|
47 |
|
|
|
|
|
48 |
|
49 |
|
50 |
def perform_kmodes_clustering(df, feature_columns, n_clusters=3):
|
|
|
75 |
def show_radar_chart(clusters, font_prop):
|
76 |
|
77 |
df_dict={
|
78 |
+
'Eco-conscious Shopper (n=340)': clusters[0],
|
79 |
+
'Casual Eco-Interested)': clusters[1],
|
80 |
+
'Eco Advocate (n=126)': clusters[2]
|
81 |
}
|
82 |
|
83 |
feature_translations_dict = dict(zip(prod_feat_flat_fields, feature_translations))
|
|
|
117 |
ax.fill(angles, data, alpha=0.25)
|
118 |
|
119 |
# Add legend
|
120 |
+
plt.legend(title='Personas')
|
121 |
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
|
122 |
|
123 |
# Add a title
|