代码之家  ›  专栏  ›  技术社区  ›  rmahesh

无法使用XGBoost预测

  •  -2
  • rmahesh  · 技术社区  · 6 年前

    我有一个程序使用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,064400570695760819394, 19619200242500181565710001000800]

    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"
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Community CDub    6 年前

    列表变量有23个元素。

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

    当你使用 iloc 在中查找元素 udf 基于index,因为它只有23个元素,所以u找不到索引为23的元素,正如错误代码所说的,它超出了范围。