【问题标题】:Unable to predict using XGBoost无法使用 XGBoost 进行预测
【发布时间】:2018-12-31 12:26:20
【问题描述】:

我有一个程序使用 XGBoost 来预测二元分类。我已经完成了大部分代码,但是在我想使用用户定义的变量来预测该类的最后,我遇到了问题。在分享代码之前,变量'clf'是我在执行GridSearchCV后选择的最优分类器:

def prob1(LIMIT_BAL, SEX, EDUCATION, MARRIAGE, AGE, PAY_0, PAY_2, PAY_3, PAY_4, PAY_5, PAY_6, BILL_AMT1, BILL_AMT2, BILL_AMT3,
 BILL_AMT4, BILL_AMT5, BILL_AMT6, PAY_AMT1, PAY_AMT2, PAY_AMT3, PAY_AMT4, PAY_AMT5, PAY_AMT6):
    #1) Store user entered information into a series, convert to dataframe, then transpose so that it is all in 1 row just like in training set.

    lst = [LIMIT_BAL, SEX, EDUCATION, MARRIAGE, AGE, PAY_0, PAY_2, PAY_3, PAY_4, PAY_5, PAY_6, BILL_AMT1, BILL_AMT2, BILL_AMT3,
 BILL_AMT4, BILL_AMT5, BILL_AMT6, PAY_AMT1, PAY_AMT2, PAY_AMT3, PAY_AMT4, PAY_AMT5, PAY_AMT6]

    ud_df = pd.Series(lst)
    ud_df = ud_df.to_frame()
    ud_df = ud_df.T
    #2) Perform the same normalization and factorization of the values as done when loading the data in above.
    c = [1,2,3] # index of categorical data columns
    r = list(range(0,23)) 
    r = [x for x in r if x not in c] # get list of all other columns
    df_cat = ud_df.iloc[:, [2,3,4]].copy()
    df_con = ud_df.iloc[:, r].copy()

    # factorize categorical data
    for c in df_cat:
         df_cat[c] = pd.factorize(df_cat[c])[0]

    # scale continuous data
    scaler = preprocessing.MinMaxScaler()
    df_scaled = scaler.fit_transform(df_con)
    df_scaled = pd.DataFrame(df_scaled, columns=df_con.columns)

    df_final = pd.concat([df_cat, df_scaled], axis=1)

    #reorder columns back to original order
    cols = df.columns
    df_final = df_final[cols]

    #Predict
    prediction = clf.predict(df_final)

    #Predict Probability
    probability_pred = clf.predict_probab(df_final)

    return(prediction, probability_pred)

所以定义内部发生了什么,用户给出了这些变量,连续的被标准化,分类变量被分解处理。

当我运行这段代码时,我得到了这个错误:

prob1(50000,1, 1, 1, 37,0,0,0,0,0,0,64400,57069,57608,19394,19619,20024,2500,1815,657,1000,1000,800)

错误代码:df_con = ud_df.iloc[:, r].copy()

IndexError: positional indexers are out-of-bounds

任何帮助都会很棒!

下面是一个示例,展示了一行在没有任何争论的情况下的外观: [50000,1,1,2,37,0,0,0,0,0,0,64400,57069,57608,19394, 19619,20024,2500,1815,657,1000,1000,800]

Edit1:原始代码中的固定边界。我收到此错误,突出显示 prob1(.....) 列:

KeyError: "Index(['ID', 'LIMIT_BAL', 'SEX', 'EDUCATION', 'MARRIAGE', 'AGE', 'PAY_0',\n       'PAY_2', 'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2',\n       'BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1',\n       'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6'],\n      dtype='object') not in index"

【问题讨论】:

    标签: python pandas machine-learning xgboost


    【解决方案1】:

    您的列表变量有 23 个元素。

    • r = list(range(0,24)) 有 24 个元素。 r = {0,1..23}

    当你使用iloc根据索引查找udf中的元素时,由于它只有23个元素,你找不到索引为23的元素,它超出了错误代码所说的范围。

    【讨论】:

    • 建议使用正确的语法来发布此问题的答案。
    • 感谢帮助我学习语法的人。第一次回答堆栈溢出问题,对编辑不太熟悉,谢谢!
    • @XiangLi 非常愚蠢的错误,感谢您发现它。我遇到了一个包含在编辑中的新错误。你觉得可以看看吗?
    • @rmahesh 因为答案解决了您的问题,请接受它;请不要用新问题“更新”问题 - 而是打开一个新问题!
    • @desertnaut 好的,我会发布一个新的。
    猜你喜欢
    • 1970-01-01
    • 2020-01-10
    • 2016-06-02
    • 2020-07-17
    • 1970-01-01
    • 2020-03-28
    • 2020-04-21
    • 2022-01-14
    相关资源
    最近更新 更多