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
30 changes: 14 additions & 16 deletions homework/m1_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ def green_mean():
"""建立 [10, 20, 30, 40, 50],回傳所有元素的平均值 (float)"""
# TODO: 你的程式碼
pass

arr = np.array([10, 20, 30, 40, 50])
return arr.mean()

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

arr = np.array([10, 20, 30, 40, 50])
return arr * 2

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


arr = np.array([10, 20, 30, 40, 50])
return arr[arr > 25]
# ============================================================
# 🟡 核心題(每題 15 分,共 45 分)
# 以下函式會接收從 products.csv 讀出的 prices, stocks 陣列
Expand All @@ -43,6 +45,7 @@ def yellow_expensive_count(prices):
"""回傳單價 > 1000 的商品數量 (int)"""
# TODO: 你的程式碼
pass
return int((prices > 1000).sum())


def yellow_top3_stock_indices(stocks):
Expand All @@ -52,7 +55,7 @@ def yellow_top3_stock_indices(stocks):
"""
# TODO: 你的程式碼
pass

return np.argsort(stocks)[::-1][:3]

def yellow_restock_cost(prices, stocks):
"""
Expand All @@ -61,20 +64,15 @@ def yellow_restock_cost(prices, stocks):
"""
# TODO: 你的程式碼
pass


return (prices[prices < 500] * 50).sum()
# ============================================================
# 🔴 挑戰題(25 分)
# ============================================================

def red_double11_prices(prices, stocks):
"""
雙 11 定價規則(必須向量化,不能用 for-loop):
- 庫存 >= 100:打 7 折
- 庫存 20~99:打 9 折
- 庫存 < 20:原價
回傳每個商品的雙 11 售價 (ndarray)
提示:np.where 可以巢狀使用
"""
# TODO: 你的程式碼
pass
return np.where(
stocks >= 100,
prices * 0.7,
np.where(stocks >= 20, prices * 0.9, prices)
)
60 changes: 53 additions & 7 deletions homework/m2_pandas_cleaning.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def green_read_csv():
提示:pd.read_csv()
"""
# TODO: 你的程式碼
pass
df = pd.read_csv('../datasets/ecommerce/orders_raw.csv')
return df



def green_shape(df):
Expand All @@ -29,7 +31,7 @@ def green_shape(df):
提示:df.shape
"""
# TODO: 你的程式碼
pass
return df.shape


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



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


def yellow_clean_amount(df):
Expand All @@ -63,7 +68,16 @@ def yellow_clean_amount(df):
提示:.str.replace() + .astype(float)
"""
# TODO: 你的程式碼
pass
df_new = df.copy()
df_new['amount'] = (
df_new['amount']
.astype(str)
.str.replace('$', '')
.str.replace(',', '')
.astype(float)
)

return df_new


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


# ============================================================
Expand All @@ -93,4 +109,34 @@ def red_clean_orders(path):
提示:pd.to_datetime(errors='coerce')
"""
# TODO: 你的程式碼
pass
df = pd.read_csv(path)

df.columns = df.columns.str.strip().str.lower()

df['amount'] = (
df['amount']
.astype(str)
.str.replace('$', '')
.str.replace(',', '')
.astype(float)
)

df['order_date'] = pd.to_datetime(df['order_date'], errors='coerce')

df = df.dropna(subset=['amount'])

df = df.dropna(subset=['order_date'])

df = df.drop_duplicates()

return df










37 changes: 30 additions & 7 deletions homework/m3_pandas_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,30 @@ def green_load_and_merge():
提示:pd.merge(how='left')
"""
# TODO: 你的程式碼
pass
DATA = '../datasets/ecommerce'
orders = pd.read_csv(f'{DATA}/orders_clean.csv', parse_dates=['order_date'])
customers = pd.read_csv(f'{DATA}/customers.csv')
products = pd.read_csv(f'{DATA}/products.csv')

df = (
orders
.merge(customers, on = 'customer_id', how = 'left')
.merge(products, on = 'product_id', how = 'left')
)

return df


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


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


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


def yellow_gold_vip_stats(df):
Expand All @@ -60,7 +71,12 @@ def yellow_gold_vip_stats(df):
提示:df[df['vip_level'] == 'Gold']
"""
# TODO: 你的程式碼
pass
df_gold = df.groupby('vip_level').agg(
總訂單數 = ('order_id', 'count'),
總金額 = ('amount', 'sum')
).loc['Gold']

return f'(訂單數 {df_gold.values[0].astype(int)}, 總金額 {df_gold.values[1]})'


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


# ============================================================
Expand All @@ -94,4 +111,10 @@ def red_rfm_top5(df):
提示:groupby('customer_id').agg(...)
"""
# TODO: 你的程式碼
pass
rfm = df.groupby('customer_id').agg(
R = ('order_date', 'max'),
F = ('order_id', 'count'),
M = ('amount', 'sum')
).reset_index().sort_value('M', ascending = False).head()

return rfm
66 changes: 59 additions & 7 deletions homework/m4_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ def green_avg_by_month():
提示:df['order_date'].dt.month
"""
# TODO: 你的程式碼
pass
df = pd.read_csv("../datasets/ecommerce/orders_enriched.csv",
parse_dates=["order_date"])
df['month'] = df['order_date'].dt.month
df_mon = df.groupby('month')['amount'].mean()
return df_mon


def green_top3_dates():
Expand All @@ -37,7 +41,11 @@ def green_top3_dates():
提示:value_counts().head(3)
"""
# TODO: 你的程式碼
pass
df = pd.read_csv("../datasets/ecommerce/orders_enriched.csv",
parse_dates=["order_date"])
ts = df.set_index('order_date').sort_index()
top3 = ts['order_id'].resample('D').count().head(3)
return top3


def green_date_range():
Expand All @@ -46,7 +54,10 @@ def green_date_range():
格式為 pandas Timestamp
"""
# TODO: 你的程式碼
pass
df = pd.read_csv("../datasets/ecommerce/orders_enriched.csv",
parse_dates=["order_date"])

return f'({list(df['order_date'].sort_values())[0]}, {list(df['order_date'].sort_values())[-1]})'


# ============================================================
Expand All @@ -60,7 +71,14 @@ def yellow_monthly_revenue():
提示:set_index('order_date').resample('ME')['amount'].sum()
"""
# TODO: 你的程式碼
pass
df = pd.read_csv("../datasets/ecommerce/orders_enriched.csv",
parse_dates=["order_date"])
ts = df.set_index('order_date').sort_index()

mon_rev = ts.resample('ME')['amount'].sum()

return mon_rev



def yellow_rolling_avg(monthly_revenue):
Expand All @@ -71,7 +89,17 @@ def yellow_rolling_avg(monthly_revenue):
提示:.rolling(window=3).mean()
"""
# TODO: 你的程式碼
pass
df = pd.read_csv("../datasets/ecommerce/orders_enriched.csv",
parse_dates=["order_date"])
ts = df.set_index('order_date').sort_index()

mon_rev = ts.resample('ME')['amount'].sum()

mon_roll = mon_rev.rolling(window = monthly_revenue).mean()

return mon_roll




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


# ============================================================
Expand All @@ -101,4 +130,27 @@ def red_monthly_report():
提示:resample + agg + pct_change
"""
# TODO: 你的程式碼
pass
df = pd.read_csv("../datasets/ecommerce/orders_enriched.csv",
parse_dates=["order_date"])
ts = df.set_index('order_date').sort_index()

mon_count = ts['order_id'].resample('ME').count()

mon_rev = ts['amount'].resample('ME').sum()

mon_act = ts['customer_id'].resample('ME').nunique()

mon_avg = mon_rev / mon_count

mon_rev_grow = mon_rev.pct_change()

mon_info = pd.DataFrame({
'order_count': mon_count,
'revenue': mon_rev,
'active_customers': mon_act,
'avg_order_value': mon_avg,
'revenue_growth': mon_rev_grow,
})

return mon_info

Loading
Loading