import os
import sys
import json
sys.path.append(os.getcwd())
import numpy as np
import pandas as pd
import lightgbm as lgb
from sklearn.model_selection import train_test_split
from sklearn.datasets import make_classification
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score

label_encoder = LabelEncoder()
my_c = [
        "uid",
        "type",
        "channel",
        "fans",
        "view_count_user_30days",
        "share_count_user_30days",
        "return_count_user_30days",
        "rov_user",
        "str_user",
        "out_user_id",
        "mode",
        "out_play_cnt",
        "out_like_cnt",
        "out_share_cnt",
        "out_collection_cnt"
    ]

str_cols = ["uid", "type", "channel", "mode"]
float_cols = [
        "fans",
        "view_count_user_30days",
        "share_count_user_30days",
        "return_count_user_30days",
        "rov_user",
        "str_user",
        "out_user_id",
        "out_play_cnt",
        "out_like_cnt",
        "out_share_cnt",
        "out_collection_cnt"
    ]
with open("whole_data/x_data.json") as f1:
    x_list = json.loads(f1.read())
    X_train = pd.DataFrame(x_list[:15000], columns=my_c)
    for key in str_cols:
        X_train[key] = label_encoder.fit_transform(X_train[key])
    for key in float_cols:
        X_train[key] = pd.to_numeric(X_train[key], errors='coerce')
    X_test = pd.DataFrame(x_list[15000:], columns=my_c)
    for key in str_cols:
        X_test[key] = label_encoder.fit_transform(X_test[key])
    for key in float_cols:
        X_test[key] = pd.to_numeric(X_test[key], errors='coerce')


with open("whole_data/y_data.json") as f2:
    y_list = json.loads(f2.read())
    y__list = [0 if i <= 25 else 1 for i in y_list]
    y_train = np.array(y__list[:15000])
    y_test = np.array(y__list[15000:])

# 创建LightGBM数据集
train_data = lgb.Dataset(X_train, label=y_train, categorical_feature=['uid', 'type', 'channel', 'mode'])
test_data = lgb.Dataset(X_test, label=y_test, reference=train_data)

# 设置模型的参数
params = {
    'objective': 'binary',  # 指定二分类任务
    'metric': 'binary_logloss',  # 评估指标为二分类的log损失
    'num_leaves': 31,  # 叶子节点数
    'learning_rate': 0.05,  # 学习率
    'bagging_fraction': 0.9,  # 建树的样本采样比例
    'feature_fraction': 0.8,  # 建树的特征选择比例
    'bagging_freq': 5,  # k 意味着每 k 次迭代执行bagging
}

# 训练模型
num_round = 100
bst = lgb.train(params, train_data, num_round, valid_sets=[test_data])

# 预测
y_pred = bst.predict(X_test, num_iteration=bst.best_iteration)
# 转换为二进制输出
y_pred_binary = np.where(y_pred > 0.5, 1, 0)

# 评估模型
accuracy = accuracy_score(y_test, y_pred_binary)
print(f'Accuracy: {accuracy}')
