Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions homework/m1_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,24 @@
def green_mean():
"""建立 [10, 20, 30, 40, 50],回傳所有元素的平均值 (float)"""
# TODO: 你的程式碼
float = np.array([10, 20, 30, 40, 50])
return np.mean(float)
pass


def green_double():
"""建立 [10, 20, 30, 40, 50],回傳所有元素乘以 2 的 ndarray"""
# TODO: 你的程式碼
ndarray = np.array([10, 20, 30, 40, 50])
return ndarray * 2
pass


def green_filter():
"""建立 [10, 20, 30, 40, 50],回傳大於 25 的元素 (ndarray)"""
# TODO: 你的程式碼
ndarray = np.array([10, 20, 30, 40, 50])
return ndarray[ndarray >25]
pass


Expand All @@ -42,6 +48,7 @@ def green_filter():
def yellow_expensive_count(prices):
"""回傳單價 > 1000 的商品數量 (int)"""
# TODO: 你的程式碼
return (prices > 1000).sum()
pass


Expand All @@ -51,6 +58,7 @@ def yellow_top3_stock_indices(stocks):
提示:np.argsort
"""
# TODO: 你的程式碼
return np.argsort(stocks)[::-1][:3]
pass


Expand All @@ -60,6 +68,7 @@ def yellow_restock_cost(prices, stocks):
提示:布林遮罩 + .sum()
"""
# TODO: 你的程式碼
return (prices[prices < 500] * 50).sum()
pass


Expand All @@ -77,4 +86,10 @@ def red_double11_prices(prices, stocks):
提示:np.where 可以巢狀使用
"""
# TODO: 你的程式碼
return np.where(
stocks >= 100, prices * 0.7,
np.where(
(stocks >=20) & (stocks <100), prices *0.9,
prices
))
pass
18 changes: 18 additions & 0 deletions homework/m2_pandas_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def green_read_csv():
提示:pd.read_csv()
"""
# TODO: 你的程式碼
return pd.read_csv("../datasets/ecommerce/orders_raw.csv")
pass


Expand All @@ -29,6 +30,8 @@ def green_shape(df):
提示:df.shape
"""
# TODO: 你的程式碼

return df.shape
pass


Expand All @@ -38,6 +41,7 @@ def green_dtypes(df):
提示:df.dtypes
"""
# TODO: 你的程式碼
return df.dtypes
pass


Expand All @@ -52,6 +56,9 @@ def yellow_clean_columns(df):
提示:df.columns.str.strip().str.lower()
"""
# TODO: 你的程式碼
df_copy = df.copy()
df_copy.columns = df_copy.columns.str.strip().str.lower()
return df_copy
pass


Expand All @@ -63,6 +70,9 @@ def yellow_clean_amount(df):
提示:.str.replace() + .astype(float)
"""
# TODO: 你的程式碼
df_copy = df.copy()
df_copy["amount"] = df_copy["amount"].str.replace("$","",regex=False).str.replace(",","",regex=False).astype(float)
return df_copy
pass


Expand All @@ -72,6 +82,7 @@ def yellow_drop_duplicates(df):
提示:df.drop_duplicates()
"""
# TODO: 你的程式碼
return df.drop_duplicates()
pass


Expand All @@ -93,4 +104,11 @@ def red_clean_orders(path):
提示:pd.to_datetime(errors='coerce')
"""
# TODO: 你的程式碼
df = pd.read_csv(path)
df.columns = df.columns.str.strip().str.lower()
df["amount"] = df["amount"].str.replace("$","",regex=False).str.replace(",","",regex=False).astype(float)
df["order_date"] = pd.to_datetime(df["order_date"], errors='coerce')
df = df.dropna(subset=["amount","order_date"])
df = df.drop_duplicates()
return df
pass
25 changes: 25 additions & 0 deletions homework/m3_pandas_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@ def green_load_and_merge():
提示:pd.merge(how='left')
"""
# TODO: 你的程式碼
df1 = pd.read_csv("../datasets/ecommerce/orders_clean.csv")
df2 = pd.read_csv("../datasets/ecommerce/customers.csv")
df3 = pd.read_csv("../datasets/ecommerce/products.csv")
df = df1.merge(df2, on = "customer_id", how = "left")
df = df.merge(df3, on = "product_id", how = "left")
return df

pass


def green_row_count(df):
"""回傳 DataFrame 的列數 (int)"""
# TODO: 你的程式碼
return df.shape[0]
pass


def green_column_list(df):
"""回傳 DataFrame 的所有欄位名稱 (list)"""
# TODO: 你的程式碼
return list(df.columns)
pass


Expand All @@ -50,6 +59,7 @@ def yellow_top_category(df):
提示:groupby('category')['amount'].sum()
"""
# TODO: 你的程式碼
return df.groupby("category")["amount"].sum().sort_values().idxmax()
pass


Expand All @@ -60,6 +70,9 @@ def yellow_gold_vip_stats(df):
提示:df[df['vip_level'] == 'Gold']
"""
# TODO: 你的程式碼
order_count = df[df["vip_level"] == "Gold"].shape[0]
total_amount = df[df["vip_level"] == "Gold"]["amount"].sum()
return (order_count, total_amount)
pass


Expand All @@ -70,6 +83,7 @@ def yellow_region_avg_amount(df):
提示:groupby('region')['amount'].mean()
"""
# TODO: 你的程式碼
return df.groupby("region")["amount"].mean()
pass


Expand All @@ -94,4 +108,15 @@ def red_rfm_top5(df):
提示:groupby('customer_id').agg(...)
"""
# TODO: 你的程式碼
result_df = df.groupby("customer_id").agg({
"customer_name" : "first",
"order_date":"max",
"order_id" : "count",
"amount" : "sum"
}).rename(columns={
"order_date" : "R",
"order_id" : "F",
"amount" : "M"
}).sort_values("M", ascending= False).head().reset_index()
return result_df
pass
35 changes: 35 additions & 0 deletions homework/m4_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def green_avg_by_month():
提示:df['order_date'].dt.month
"""
# TODO: 你的程式碼
df = _load_data()
return df.groupby(df['order_date'].dt.month)["amount"].mean()
pass


Expand All @@ -37,6 +39,8 @@ def green_top3_dates():
提示:value_counts().head(3)
"""
# TODO: 你的程式碼
df = _load_data()
return df["order_date"].value_counts().head(3)
pass


Expand All @@ -46,6 +50,10 @@ def green_date_range():
格式為 pandas Timestamp
"""
# TODO: 你的程式碼
df = _load_data()
max_date = df["order_date"].max()
min_date = df["order_date"].min()
return (min_date, max_date)
pass


Expand All @@ -60,6 +68,8 @@ def yellow_monthly_revenue():
提示:set_index('order_date').resample('ME')['amount'].sum()
"""
# TODO: 你的程式碼
df = _load_data()
return df.set_index(["order_date"]).resample("ME")["amount"].sum()
pass


Expand All @@ -71,6 +81,7 @@ def yellow_rolling_avg(monthly_revenue):
提示:.rolling(window=3).mean()
"""
# TODO: 你的程式碼
return monthly_revenue.rolling(window=3).mean()
pass


Expand All @@ -81,6 +92,7 @@ def yellow_category_median(df):
提示:groupby + median + sort_values
"""
# TODO: 你的程式碼
return df.groupby(df["category"])["amount"].median().sort_values(ascending=False)
pass


Expand All @@ -101,4 +113,27 @@ def red_monthly_report():
提示:resample + agg + pct_change
"""
# TODO: 你的程式碼
df = _load_data()
monthly_report = (
df
.set_index('order_date')
.resample("ME")
.agg({
"order_id" : "count",
"amount" : "sum",
"customer_id" : "nunique"
}).rename(columns={
"order_id" : "order_count",
"amount" : "revenue",
"customer_id" : "active_customers"
})
)

monthly_report.index = monthly_report.index.to_period("M")

monthly_report["avg_order_value"] = (monthly_report["revenue"]/monthly_report["order_count"])

monthly_report["revenue_growth"] = (monthly_report["revenue"].pct_change() * 100 )

return monthly_report
pass
55 changes: 55 additions & 0 deletions homework/m5_visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def green_bar_category():
提示:sns.countplot 或 value_counts().plot.bar()
"""
# TODO: 你的程式碼
df = _load_data()
plt.figure(figsize = (8, 4))
sns.countplot(data = df, x = "category")
return plt.gcf()
pass


Expand All @@ -39,6 +43,10 @@ def green_hist_amount():
提示:sns.histplot(bins=20) 或 plt.hist()
"""
# TODO: 你的程式碼
df = _load_data()
plt.figure(figsize=(8,4))
sns.histplot(bins = 20, x = "amount", data = df)
return plt.gcf()
pass


Expand All @@ -51,6 +59,13 @@ def green_set_labels():
回傳 matplotlib Figure 物件
"""
# TODO: 你的程式碼
df = _load_data()
plt.figure(figsize = (8, 4))
sns.countplot(data = df, x = "category")
plt.title("category_order_count")
plt.xlabel("category")
plt.ylabel("order_count")
return plt.gcf()
pass


Expand All @@ -68,6 +83,21 @@ def yellow_line_region_trend():
提示:分別 groupby 再 plot,或用 sns.lineplot(hue='region')
"""
# TODO: 你的程式碼
df = _load_data()
df["month"] = df["order_date"].dt.to_period("M")
region_monthly_revenue = (
df[df["region"].isin(["North", "South"])]
.groupby(["month", "region"])["amount"]
.sum()
.unstack()
)
fig, ax = plt.subplots(figsize=(8, 4))

region_monthly_revenue.plot(
marker="o",
ax=ax
)
return fig
pass


Expand All @@ -78,6 +108,10 @@ def yellow_box_vip():
提示:sns.boxplot(x='vip_level', y='amount', data=df)
"""
# TODO: 你的程式碼
df = _load_data()
fig, ax = plt.subplots(figsize=(8, 4))
sns.boxplot(x = "vip_level", y = "amount", data = df)
return fig
pass


Expand All @@ -88,6 +122,10 @@ def yellow_scatter_price_amount():
提示:plt.scatter() 或 sns.scatterplot()
"""
# TODO: 你的程式碼
df = _load_data()
plt.figure(figsize=(8, 4))
plt.scatter(x = "unit_price", y = "amount", data = df)
return plt.gcf()
pass


Expand All @@ -107,4 +145,21 @@ def red_category_dashboard(category="Electronics"):
提示:fig, axes = plt.subplots(2, 2, figsize=(14, 10))
"""
# TODO: 你的程式碼
df = _load_data()
df["month"] = df["order_date"].dt.to_period("M").astype("str")
df = df[df["category"].isin(["Electronics"])]
fig, axes = plt.subplots(2, 2, figsize = (14,10))


sns.lineplot(data=df, x = "month", y = "amount", marker="o", ax = axes[0, 0], estimator = "sum", errorbar=None )
axes[0, 0].tick_params(axis='x', rotation=45)

sns.barplot(data=df, x = "region", y = "amount", estimator="sum",
errorbar=None, ax = axes[0,1] )

df.groupby("product_name")["amount"].sum().sort_values(ascending=False).head(5).plot.barh(ax=axes[1,0])

sns.histplot(data = df, x = "amount", bins=20, ax=axes[1,1])

return fig
pass
Loading
Loading