美文网首页
2022-04-25 机器学习模型训练

2022-04-25 机器学习模型训练

作者: 破阵子沙场秋点兵 | 来源:发表于2022-04-25 20:07 被阅读0次

    引入包

    import pandas as pd
    import numpy as np
    
    import warnings
    warnings.filterwarnings("ignore") 
    
    pd.set_option('display.max_columns', None)
    

    读取数据

    train_data = pd.read_csv('train_all.csv',nrows=10000)
    test_data = pd.read_csv('test_all.csv',nrows=100)
    

    训练和测试数据

    features_columns = [col for col in train_data.columns if col not in ['user_id','label']]
    train = train_data[features_columns].values
    test = test_data[features_columns].values
    target =train_data['label'].values
    

    训练集分割

    from sklearn.model_selection import train_test_split
    from sklearn.ensemble import RandomForestClassifier
    
    clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0, n_jobs=-1)
    X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.4, random_state=0)
    
    print(X_train.shape, y_train.shape)
    print(X_test.shape, y_test.shape)
    
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test) 
    

    交叉验证

    from sklearn.model_selection import cross_val_score
    from sklearn.ensemble import RandomForestClassifier
    
    clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0, n_jobs=-1)
    scores = cross_val_score(clf, train, target, cv=5)
    print(scores)
    print("Accuracy: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2)) 
    

    指定f1 score

    from sklearn import metrics
    from sklearn.model_selection import cross_val_score
    from sklearn.ensemble import RandomForestClassifier
    
    clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0, n_jobs=-1)
    scores = cross_val_score(clf, train, target, cv=5, scoring='f1_macro')
    print(scores)  
    print("F1: %0.2f (+/- %0.2f)" % (scores.mean(), scores.std() * 2))
    

    支持的scoring有

    SCORERS = dict(explained_variance=explained_variance_scorer,
                   r2=r2_scorer,
                   max_error=max_error_scorer,
                   neg_median_absolute_error=neg_median_absolute_error_scorer,
                   neg_mean_absolute_error=neg_mean_absolute_error_scorer,
                   neg_mean_absolute_percentage_error=neg_mean_absolute_percentage_error_scorer,  # noqa
                   neg_mean_squared_error=neg_mean_squared_error_scorer,
                   neg_mean_squared_log_error=neg_mean_squared_log_error_scorer,
                   neg_root_mean_squared_error=neg_root_mean_squared_error_scorer,
                   neg_mean_poisson_deviance=neg_mean_poisson_deviance_scorer,
                   neg_mean_gamma_deviance=neg_mean_gamma_deviance_scorer,
                   accuracy=accuracy_scorer,
                   top_k_accuracy=top_k_accuracy_scorer,
                   roc_auc=roc_auc_scorer,
                   roc_auc_ovr=roc_auc_ovr_scorer,
                   roc_auc_ovo=roc_auc_ovo_scorer,
                   roc_auc_ovr_weighted=roc_auc_ovr_weighted_scorer,
                   roc_auc_ovo_weighted=roc_auc_ovo_weighted_scorer,
                   balanced_accuracy=balanced_accuracy_scorer,
                   average_precision=average_precision_scorer,
                   neg_log_loss=neg_log_loss_scorer,
                   neg_brier_score=neg_brier_score_scorer,
                   # Cluster metrics that use supervised evaluation
                   adjusted_rand_score=adjusted_rand_scorer,
                   rand_score=rand_scorer,
                   homogeneity_score=homogeneity_scorer,
                   completeness_score=completeness_scorer,
                   v_measure_score=v_measure_scorer,
                   mutual_info_score=mutual_info_scorer,
                   adjusted_mutual_info_score=adjusted_mutual_info_scorer,
                   normalized_mutual_info_score=normalized_mutual_info_scorer,
                   fowlkes_mallows_score=fowlkes_mallows_scorer)
    

    手动交叉验证

    import numpy as np
    from sklearn.model_selection import KFold
    from sklearn.ensemble import RandomForestClassifier
    
    clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0, n_jobs=-1)
    kf = KFold(n_splits=5)
    for k, (train_index, test_index) in enumerate(kf.split(train)):
        X_train, X_test, y_train, y_test = train[train_index], train[test_index], target[train_index], target[test_index]
        clf = clf.fit(X_train, y_train)
        print(k, clf.score(X_test, y_test))
    

    StratifiedKFold切分数据(label均分)

    from sklearn.model_selection import StratifiedKFold
    from sklearn.ensemble import RandomForestClassifier
    
    clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0, n_jobs=-1)
    skf = StratifiedKFold(n_splits=5)
    for k, (train_index, test_index) in enumerate(skf.split(train, target)):
        X_train, X_test, y_train, y_test = train[train_index], train[test_index], target[train_index], target[test_index]
        clf = clf.fit(X_train, y_train)
        print(k, clf.score(X_test, y_test))
    

    参数搜索

    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import GridSearchCV
    from sklearn.metrics import classification_report
    from sklearn.ensemble import RandomForestClassifier
    
    
    # Split the dataset in two equal parts
    X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.5, random_state=0)
    
    # model 
    clf = RandomForestClassifier(n_jobs=-1)
    
    # Set the parameters by cross-validation
    
    tuned_parameters = {
                        'n_estimators': [50, 100, 200]
    #                     ,'criterion': ['gini', 'entropy']
    #                     ,'max_depth': [2, 5]
    #                     ,'max_features': ['log2', 'sqrt', 'int']
    #                     ,'bootstrap': [True, False]
    #                     ,'warm_start': [True, False]
                        }
    
    scores = ['precision']
    
    for score in scores:
        print("# Tuning hyper-parameters for %s" % score)
        print()
    
        clf = GridSearchCV(clf, tuned_parameters, cv=5,
                           scoring='%s_macro' % score)
        clf.fit(X_train, y_train)
    
        print("Best parameters set found on development set:")
        print()
        print(clf.best_params_)
        print()
        print("Grid scores on development set:")
        print()
        means = clf.cv_results_['mean_test_score']
        stds = clf.cv_results_['std_test_score']
        for mean, std, params in zip(means, stds, clf.cv_results_['params']):
            print("%0.3f (+/-%0.03f) for %r"
                  % (mean, std * 2, params))
        print()
    
        print("Detailed classification report:")
        print()
        print("The model is trained on the full development set.")
        print("The scores are computed on the full evaluation set.")
        print()
        y_true, y_pred = y_test, clf.predict(X_test)
        print(classification_report(y_true, y_pred))
        print()
    

    模糊矩阵

    import itertools
    import numpy as np
    import matplotlib.pyplot as plt
    
    from sklearn.model_selection import train_test_split
    from sklearn.metrics import confusion_matrix
    from sklearn.ensemble import RandomForestClassifier
    
    # label name
    class_names = ['no-repeat', 'repeat']
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    
    # Run classifier, using a model that is too regularized (C too low) to see
    # the impact on the results
    clf = RandomForestClassifier(n_jobs=-1)
    y_pred = clf.fit(X_train, y_train).predict(X_test)
    
    
    def plot_confusion_matrix(cm, classes,
                              normalize=False,
                              title='Confusion matrix',
                              cmap=plt.cm.Blues):
        """
        This function prints and plots the confusion matrix.
        Normalization can be applied by setting `normalize=True`.
        """
        if normalize:
            cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
            print("Normalized confusion matrix")
        else:
            print('Confusion matrix, without normalization')
    
        print(cm)
    
        plt.imshow(cm, interpolation='nearest', cmap=cmap)
        plt.title(title)
        plt.colorbar()
        tick_marks = np.arange(len(classes))
        plt.xticks(tick_marks, classes, rotation=45)
        plt.yticks(tick_marks, classes)
    
        fmt = '.2f' if normalize else 'd'
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j, i, format(cm[i, j], fmt),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")
    
        plt.ylabel('True label')
        plt.xlabel('Predicted label')
        plt.tight_layout()
    
    
    # Compute confusion matrix
    cnf_matrix = confusion_matrix(y_test, y_pred)
    np.set_printoptions(precision=2)
    
    # Plot non-normalized confusion matrix
    plt.figure()
    plot_confusion_matrix(cnf_matrix, classes=class_names,
                          title='Confusion matrix, without normalization')
    
    # Plot normalized confusion matrix
    plt.figure()
    plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
                          title='Normalized confusion matrix')
    
    plt.show()
    
    from sklearn.metrics import classification_report
    from sklearn.ensemble import RandomForestClassifier
    
    # label name
    class_names = ['no-repeat', 'repeat']
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    
    # Run classifier, using a model that is too regularized (C too low) to see
    # the impact on the results
    clf = RandomForestClassifier(n_jobs=-1)
    y_pred = clf.fit(X_train, y_train).predict(X_test)
    
    print(classification_report(y_test, y_pred, target_names=class_names))
    

    不同模型

    LR

    from sklearn.linear_model import LinearRegression
    from sklearn.linear_model import LogisticRegression
    from sklearn.preprocessing import StandardScaler
    
    stdScaler = StandardScaler()
    X = stdScaler.fit_transform(train)
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(X, target, random_state=0)
    
    clf = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial').fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    KNN

    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.preprocessing import StandardScaler
    
    stdScaler = StandardScaler()
    X = stdScaler.fit_transform(train)
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(X, target, random_state=0)
    
    clf = KNeighborsClassifier(n_neighbors=3).fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    GaussianNB

    from sklearn.naive_bayes import GaussianNB
    from sklearn.preprocessing import StandardScaler
    
    stdScaler = StandardScaler()
    X = stdScaler.fit_transform(train)
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(X, target, random_state=0)
    
    clf = GaussianNB().fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    Tree

    from sklearn import tree
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    bagging

    from sklearn.ensemble import BaggingClassifier
    from sklearn.neighbors import KNeighborsClassifier
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    clf = BaggingClassifier(KNeighborsClassifier(), max_samples=0.5, max_features=0.5)
    
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    随机森林

    from sklearn.ensemble import RandomForestClassifier
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    clf = clf = RandomForestClassifier(n_estimators=10, max_depth=3, min_samples_split=12, random_state=0)
    
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    ExTree

    from sklearn.ensemble import ExtraTreesClassifier
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    clf = ExtraTreesClassifier(n_estimators=10, max_depth=None, min_samples_split=2, random_state=0)
    
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    AdaBoost

    from sklearn.ensemble import AdaBoostClassifier
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    clf = AdaBoostClassifier(n_estimators=10)
    
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    GBDT

    from sklearn.ensemble import GradientBoostingClassifier
    
    # Split the data into a training set and a test set
    X_train, X_test, y_train, y_test = train_test_split(train, target, random_state=0)
    clf = GradientBoostingClassifier(n_estimators=10, learning_rate=1.0, max_depth=1, random_state=0)
    
    clf = clf.fit(X_train, y_train)
    clf.score(X_test, y_test)
    

    VOTE

    from sklearn import datasets
    from sklearn.model_selection import cross_val_score
    from sklearn.linear_model import LogisticRegression
    from sklearn.naive_bayes import GaussianNB
    from sklearn.ensemble import RandomForestClassifier
    from sklearn.ensemble import VotingClassifier
    from sklearn.preprocessing import StandardScaler
    
    stdScaler = StandardScaler()
    X = stdScaler.fit_transform(train)
    y = target
    
    
    clf1 = LogisticRegression(solver='lbfgs', multi_class='multinomial', random_state=1)
    clf2 = RandomForestClassifier(n_estimators=50, random_state=1)
    clf3 = GaussianNB()
    
    eclf = VotingClassifier(estimators=[('lr', clf1), ('rf', clf2), ('gnb', clf3)], voting='hard')
    
    for clf, label in zip([clf1, clf2, clf3, eclf], ['Logistic Regression', 'Random Forest', 'naive Bayes', 'Ensemble']):
        scores = cross_val_score(clf, X, y, cv=5, scoring='accuracy')
        print("Accuracy: %0.2f (+/- %0.2f) [%s]" % (scores.mean(), scores.std(), label))
    

    lgb

    import lightgbm
    
    X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.4, random_state=0)
    X_test, X_valid, y_test, y_valid = train_test_split(X_test, y_test, test_size=0.5, random_state=0)
    
    clf = lightgbm
    
    train_matrix = clf.Dataset(X_train, label=y_train)
    test_matrix = clf.Dataset(X_test, label=y_test)
    params = {
              'boosting_type': 'gbdt',
              #'boosting_type': 'dart',
              'objective': 'multiclass',
              'metric': 'multi_logloss',
              'min_child_weight': 1.5,
              'num_leaves': 2**5,
              'lambda_l2': 10,
              'subsample': 0.7,
              'colsample_bytree': 0.7,
              'colsample_bylevel': 0.7,
              'learning_rate': 0.03,
              'tree_method': 'exact',
              'seed': 2017,
              "num_class": 2,
              'silent': True,
              }
    num_round = 10000
    early_stopping_rounds = 100
    model = clf.train(params, 
                      train_matrix,
                      num_round,
                      valid_sets=test_matrix,
                      early_stopping_rounds=early_stopping_rounds)
    pre= model.predict(X_valid,num_iteration=model.best_iteration)
    

    xgb

    import xgboost
    
    X_train, X_test, y_train, y_test = train_test_split(train, target, test_size=0.4, random_state=0)
    X_test, X_valid, y_test, y_valid = train_test_split(X_test, y_test, test_size=0.5, random_state=0)
    
    clf = xgboost
    
    train_matrix = clf.DMatrix(X_train, label=y_train, missing=-1)
    test_matrix = clf.DMatrix(X_test, label=y_test, missing=-1)
    z = clf.DMatrix(X_valid, label=y_valid, missing=-1)
    params = {'booster': 'gbtree',
              'objective': 'multi:softprob',
              'eval_metric': 'mlogloss',
              'gamma': 1,
              'min_child_weight': 1.5,
              'max_depth': 5,
              'lambda': 100,
              'subsample': 0.7,
              'colsample_bytree': 0.7,
              'colsample_bylevel': 0.7,
              'eta': 0.03,
              'tree_method': 'exact',
              'seed': 2017,
              "num_class": 2
              }
    
    num_round = 10000
    early_stopping_rounds = 100
    watchlist = [(train_matrix, 'train'),
                 (test_matrix, 'eval')
                 ]
    
    model = clf.train(params,
                      train_matrix,
                      num_boost_round=num_round,
                      evals=watchlist,
                      early_stopping_rounds=early_stopping_rounds
                      )
    pre = model.predict(z,ntree_limit=model.best_ntree_limit)
    

    Stacking,Bootstrap,Bagging技术实践(手动封装)

    """
        导入相关包
    """
    import pandas as pd
    import numpy as np
    import lightgbm as lgb
    from sklearn.metrics import f1_score
    from sklearn.model_selection import train_test_split
    from sklearn.model_selection import KFold
    from sklearn.model_selection import StratifiedKFold
    
    class SBBTree():
        """
            SBBTree
            Stacking,Bootstap,Bagging
        """
        def __init__(
                        self, 
                        params,
                        stacking_num,
                        bagging_num,
                        bagging_test_size,
                        num_boost_round,
                        early_stopping_rounds
                    ):
            """
                Initializes the SBBTree.
                Args:
                  params : lgb params.
                  stacking_num : k_flod stacking.
                  bagging_num : bootstrap num.
                  bagging_test_size : bootstrap sample rate.
                  num_boost_round : boost num.
                  early_stopping_rounds : early_stopping_rounds.
            """
            self.params = params
            self.stacking_num = stacking_num
            self.bagging_num = bagging_num
            self.bagging_test_size = bagging_test_size
            self.num_boost_round = num_boost_round
            self.early_stopping_rounds = early_stopping_rounds
    
            self.model = lgb
            self.stacking_model = []
            self.bagging_model = []
    
        def fit(self, X, y):
            """ fit model. """
            if self.stacking_num > 1:
                layer_train = np.zeros((X.shape[0], 2))
                self.SK = StratifiedKFold(n_splits=self.stacking_num, shuffle=True, random_state=1)
                for k,(train_index, test_index) in enumerate(self.SK.split(X, y)):
                    X_train = X[train_index]
                    y_train = y[train_index]
                    X_test = X[test_index]
                    y_test = y[test_index]
    
                    lgb_train = lgb.Dataset(X_train, y_train)
                    lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
    
                    gbm = lgb.train(self.params,
                                lgb_train,
                                num_boost_round=self.num_boost_round,
                                valid_sets=lgb_eval,
                                early_stopping_rounds=self.early_stopping_rounds)
    
                    self.stacking_model.append(gbm)
    
                    pred_y = gbm.predict(X_test, num_iteration=gbm.best_iteration)
                    layer_train[test_index, 1] = pred_y
    
                X = np.hstack((X, layer_train[:,1].reshape((-1,1)))) 
            else:
                pass
            for bn in range(self.bagging_num):
                X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.bagging_test_size, random_state=bn)
    
                lgb_train = lgb.Dataset(X_train, y_train)
                lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)
    
                gbm = lgb.train(self.params,
                            lgb_train,
                            num_boost_round=10000,
                            valid_sets=lgb_eval,
                            early_stopping_rounds=200)
    
                self.bagging_model.append(gbm)
    
        def predict(self, X_pred):
            """ predict test data. """
            if self.stacking_num > 1:
                test_pred = np.zeros((X_pred.shape[0], self.stacking_num))
                for sn,gbm in enumerate(self.stacking_model):
                    pred = gbm.predict(X_pred, num_iteration=gbm.best_iteration)
                    test_pred[:, sn] = pred
                X_pred = np.hstack((X_pred, test_pred.mean(axis=1).reshape((-1,1))))  
            else:
                pass 
            for bn,gbm in enumerate(self.bagging_model):
                pred = gbm.predict(X_pred, num_iteration=gbm.best_iteration)
                if bn == 0:
                    pred_out=pred
                else:
                    pred_out+=pred
            return pred_out/self.bagging_num
    
    """
        TEST CODE
    """
    from sklearn.datasets import make_classification
    from sklearn.datasets import load_breast_cancer
    from sklearn.datasets import make_gaussian_quantiles
    from sklearn import metrics
    from sklearn.metrics import f1_score
    # X, y = make_classification(n_samples=1000, n_features=25, n_clusters_per_class=1, n_informative=15, random_state=1)
    X, y = make_gaussian_quantiles(mean=None, cov=1.0, n_samples=1000, n_features=50, n_classes=2, shuffle=True, random_state=2)
    # data = load_breast_cancer()
    # X, y = data.data, data.target
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1)
    params = {
            'task': 'train',
            'boosting_type': 'gbdt',
            'objective': 'binary',
            'metric': 'auc',
            'num_leaves': 9,
            'learning_rate': 0.03,
            'feature_fraction_seed': 2,
            'feature_fraction': 0.9,
            'bagging_fraction': 0.8,
            'bagging_freq': 5,
            'min_data': 20,
            'min_hessian': 1,
            'verbose': -1,
            'silent': 0
            }
    # test 1
    model = SBBTree(params=params, stacking_num=2, bagging_num=1,  bagging_test_size=0.33, num_boost_round=10000, early_stopping_rounds=200)
    model.fit(X,y)
    X_pred = X[0].reshape((1,-1))
    pred=model.predict(X_pred)
    print('pred')
    print(pred)
    print('TEST 1 ok')
    
    
    # test 1
    model = SBBTree(params, stacking_num=1, bagging_num=1, bagging_test_size=0.33, num_boost_round=10000, early_stopping_rounds=200)
    model.fit(X_train,y_train)
    pred1=model.predict(X_test)
    
    # test 2 
    model = SBBTree(params, stacking_num=1, bagging_num=3, bagging_test_size=0.33, num_boost_round=10000, early_stopping_rounds=200)
    model.fit(X_train,y_train)
    pred2=model.predict(X_test)
    
    # test 3 
    model = SBBTree(params, stacking_num=5, bagging_num=1, bagging_test_size=0.33, num_boost_round=10000, early_stopping_rounds=200)
    model.fit(X_train,y_train)
    pred3=model.predict(X_test)
    
    # test 4 
    model = SBBTree(params, stacking_num=5, bagging_num=3, bagging_test_size=0.33, num_boost_round=10000, early_stopping_rounds=200)
    model.fit(X_train,y_train)
    pred4=model.predict(X_test)
    
    fpr, tpr, thresholds = metrics.roc_curve(y_test+1, pred1, pos_label=2)
    print('auc: ',metrics.auc(fpr, tpr))
    
    fpr, tpr, thresholds = metrics.roc_curve(y_test+1, pred2, pos_label=2)
    print('auc: ',metrics.auc(fpr, tpr))
    
    fpr, tpr, thresholds = metrics.roc_curve(y_test+1, pred3, pos_label=2)
    print('auc: ',metrics.auc(fpr, tpr))
    
    fpr, tpr, thresholds = metrics.roc_curve(y_test+1, pred4, pos_label=2)
    print('auc: ',metrics.auc(fpr, tpr))
    
    
    # auc:  0.7281621243885396
    # auc:  0.7710471146419509
    # auc:  0.7894369046305492
    # auc:  0.8084519474787597
    

    相关文章

      网友评论

          本文标题:2022-04-25 机器学习模型训练

          本文链接:https://www.haomeiwen.com/subject/xrkqyrtx.html