Python打卡DAY13

知识点复习
  1. 不平衡数据集的处理:过采样、修改权重、修改阈值
  2. 交叉验证代码

作业:

从示例代码可以看到 效果没有变好,所以很多步骤都是理想是好的,但是现实并不一定可以变好。这个实验仍然有改进空间,如下。

1. 我还没做smote+过采样+修改权重的组合策略,有可能一起做会变好。

2. 我还没有调参,有可能调参后再取上述策略可能会变好

1. 结合SMOTE、Random OverSampling和修改类别权重

from imblearn.over_sampling import SMOTE, RandomOverSampler

# 假设X_train, y_train为训练集
# 1. 使用SMOTE生成合成样本
smote = SMOTE(sampling_strategy='auto', k_neighbors=5, random_state=42)
X_resampled, y_resampled = smote.fit_resample(X_train, y_train)

# 2. 再用随机过采样做平衡(可选,根据效果调整)
ros = RandomOverSampler(sampling_strategy='auto', random_state=42)
X_final, y_final = ros.fit_resample(X_resampled, y_resampled)

# 3. 使用类别权重(以样本为权重或模型参数)
# 例如:在XGBoost中设置scale_pos_weight=(负样本数/正样本数)
model = xgb.XGBClassifier(
    scale_pos_weight=class_weight_ratio,  # 可以根据样本数比调整
    n_estimators=100,
    max_depth=6,
    learning_rate=0.1,
    random_state=42
)

model.fit(X_final, y_final)

2.调参

from sklearn.model_selection import GridSearchCV

param_grid = {
    'max_depth': [4, 6, 8],
    'learning_rate': [0.01, 0.1],
    'n_estimators': [50, 100, 150],
    'scale_pos_weight': [1, 5, 10]  # 根据样本比例调节
}

grid_search = GridSearchCV(estimator=model, param_grid=param_grid, scoring='f1', cv=5)
grid_search.fit(X_train, y_train)
print(grid_search.best_params_)

@浙大疏锦行

Python中实现打卡兑换礼物的功能,通常会涉及到以下几个步骤: 1. **数据结构设计**:创建一个数据库或数据结构来存储用户的打卡记录,比如字典或列表,其中每个元素包含用户ID、日期等信息。 ```python users_gifts = {} # 使用字典,key为用户ID,value为打卡记录 ``` 2. **添加打卡功能**:编写函数,当用户调用时,检查用户是否存在并更新打卡次数。例如,可以使用`datetime`库来记录每日打卡时间。 ```python import datetime def check_in(user_id): today = datetime.datetime.now().strftime("%Y-%m-%d") if user_id not in users_gifts: users_gifts[user_id] = {today: 1} else: if today not in users_gifts[user_id]: users_gifts[user_id][today] = 1 else: users_gifts[user_id][today] += 1 ``` 3. **条件判断与兑换规则**:设定一个规则,如连续7天打卡即可兑换一份礼物。可以遍历用户的打卡记录,检查是否符合条件。 ```python def can_exchange(user_id): user_history = users_gifts.get(user_id, {}) consecutive_days = {} for date, count in user_history.items(): if date - consecutive_days.get(date, '') <= datetime.timedelta(days=6): # 连续6天 consecutive_days[date] = count if len(consecutive_days) == 7: # 找到7连日 return True return False ``` 4. **兑换操作**:如果满足兑换条件,可以删除已达到兑换的打卡记录,并通知用户兑换成功。 ```python def redeem_gift(user_id): if can_exchange(user_id): for day, _ in list(users_gifts[user_id].items())[:7]: # 删除前7天的打卡记录 del users_gifts[user_id][day] print(f"恭喜用户{user_id},您的7天连续打卡已成功兑换礼物!") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值