# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import StratifiedKFold
from lightgbm import LGBMClassifier
from sklearn import metrics
import warnings
from scipy import stats
warnings.filterwarnings('ignore')

df_train = pd.read_csv('./train/train.csv')
df_test = pd.read_csv('./test/test.csv')

X_train = np.asarray(df_train.drop(['ID', 'label'], axis = 1))
y_train = np.asarray(df_train['label'].map({'围网':0, '拖网':1,'刺网':2}))

scaler = StandardScaler()

scaler.fit(X_train)

X_train = scaler.transform(X_train)

X_test = scaler.transform(np.asarray(df_test.drop('ID',axis = 1)))

fold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)

params = {
    'n_estimators': 5000,
    'boosting_type': 'gbdt',
    'objective': 'multiclass',
    'num_class': 3,
    'early_stopping_rounds': 100,
}

clf = LGBMClassifier(**params)

models = []
pred = np.zeros((len(X_test), 5))
for index, (train_idx, val_idx) in enumerate(fold.split(X_train, y_train)):
    model = clf.fit(X_train[train_idx], y_train[train_idx], 
                    eval_set = [(X_train[val_idx], y_train[val_idx])], early_stopping_rounds = 500, verbose = False)
    models.append(model)
    val_pred = model.predict(X_train[val_idx])
    val_y = y_train[val_idx]
    print(index, 'val f1', metrics.f1_score(val_y, val_pred, average='macro'))
    pred[:, index] = model.predict(X_test)

pred = [int(stats.mode(num)[0][0]) for num in pred]

df_test['label'] = pred

df_test['label'] = df_test['label'].map({0:'围网',1:'拖网',2:'刺网'})

res = df_test[['ID', 'label']]

res.to_csv('./result/result.csv', index = None, header = None, encoding = 'utf-8')

StarcoderdataJupyterScriptsDedupFiltered