【问题标题】:Predict the output based on multiple input variable using XGBoost in Python在 Python 中使用 XGBoost 根据多个输入变量预测输出
【发布时间】:2017-07-25 19:35:27
【问题描述】:

我是 xgboost 的新手,正在尝试做以下事情。

  1. 使用输入变量预测输出变量
  2. 试图找出哪些输入变量与输出变量的相关性更高(关系良好)。

我无法获得正确的结果 w.r.t 1 和 2。我是这个 xgboost 的初学者,请帮助我。提前致谢。

参考:(Jason Brownlee 博客,kaggle)

代码:

import pandas as pd
from sklearn import preprocessing
import numpy as np
import xgboost as xgb
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split

def main():
    #Removing the blank fields and filling with mean values
    def xls_to_csv():
        df = pd.read_excel(r"C:\ML material - docs\L90\Abhijit's task\extraction-M1947-B3,B6andB5\S2\ML_9820G_PCMvsS2_PCM_FE_BE_ML_S2-2017-02-14-103304-836.xlsx")
        df.drop(['aggregation','lot','____________________wafer','wafer','lot wafer'],axis=1, inplace=True)
        df_1 = df.apply(lambda x: x.fillna(x.mean()),axis=0)
        df_1.to_csv(r"C:\ML material - docs\L90\Abhijit's task\extraction-M1947-B3,B6andB5\S2\ML_9820G_PCMvsS2_PCM_FE_BE_ML_after_impute.csv", index=False)

    #xls_to_csv()   
    #Applying normalization
    df1 = pd.read_csv(r"C:\ML material - docs\L90\Abhijit's task\extraction-M1947-B3,B6andB5\S2\ML_9820G_PCMvsS2_PCM_FE_BE_ML_after_impute.csv")
    for feature in df1.columns: # Loop through all columns in the dataframe
            if df1[feature].dtype == 'object': # Only apply for columns with categorical strings
                df1[feature] = pd.Categorical(df1[feature]).codes
    df2 = (df1 - df1.mean())/df1.std()
    df2 = df2.dropna(axis=1,how='all',thresh=None)

    df2.to_csv(r"C:\ML material - docs\L90\Abhijit's task\extraction-M1947-B3,B6andB5\S2\ML_9820G_PCMvsS2_PCM_FE_BE_ML_after_impute_after_nml.csv", index=False)

    def get_data():
        train = pd.read_csv(r"C:\ML material - docs\L90\Abhijit's task\extraction-M1947-B3,B6andB5\S2\ML_9820G_PCMvsS2_PCM_FE_BE_ML_after_impute_after_nml.csv")
        y_train = train.pop('7;IDDQ_IPD;tested_pct;sbin')
        features = train.columns
        x_train = train[features]
        return features, x_train, y_train

    features, x_train, y_train = get_data()

    final_train,final_test = train_test_split(x_train, test_size = 0.2)
    final_y_train,final_y_test = train_test_split(y_train, test_size = 0.2)

    #XGboost modelling starts here
    xgdmat = xgb.DMatrix(final_train, final_y_train) # Create our DMatrix to make XGBoost more efficient
    our_params = {'eta': 0.1, 'seed':0, 'subsample': 0.8, 'colsample_bytree': 0.8,'objective': 'reg:linear', 'max_depth':3, 'min_child_weight':1} # Grid Search CV optimized settings

    final_gb = xgb.train(our_params, xgdmat, num_boost_round= 1000)
    importances = final_gb.get_fscore()
    importance_frame = pd.DataFrame({'Importance': list(importances.values()), 'Feature': list(importances.keys())})
    importance_frame.sort('Importance', inplace = True)
    importance_frame.to_csv(r"C:\ML material - docs\L90\Abhijit's task\extraction-M1947-B3,B6andB5\S2\ML_9820G_PCMvsS2_PCM_FE_BE_ML_scores.csv", index=False)

    # Analysing the test results
    testdmat = xgb.DMatrix(final_test)
    y_pred = final_gb.predict(testdmat)
    print y_pred,"\n",final_y_test

if __name__ == '__main__':
    main()

【问题讨论】:

    标签: python-2.7 machine-learning xgboost kaggle


    【解决方案1】:

    如果不访问任何错误消息或数据输入/结果,很难评估您可能做错了什么。

    但在不详细介绍您的具体案例示例的情况下,作为 xgboost 的初学者,我对您的建议是尝试复制一些演示/示例以更好地了解其工作原理。

    对于初学者来说,一个很棒的博客/网站是 Jason Brownlee 的。 (编辑:刚刚意识到你提到了他)

    他的一个案例可以在这里找到:http://machinelearningmastery.com/develop-first-xgboost-model-python-scikit-learn/

    他还有一些关于 ML 和 xgboost 的电子书,它们是非常好的入门材料(比大多数视频课程更好)

    我的个人建议是,您在构建模型时首先逐行编写代码,而不是像您所做的那样执行单实例应用程序/功能。这样就更容易评估何时/是否犯了错误。

    编辑 2:另一个好的建议是使用 python 的 xgboost 包装器 (xgb.XGBClassifier),它在第一个实例中更容易理解/使用。

    希望对你有帮助,

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 2018-05-04
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多