ValueError:数组长度与索引长度不匹配

时间:2016-05-06 02:39:25

标签: python pandas numpy kaggle

我正在练习像kaggle这样的竞赛,我一直在尝试使用XGBoost,并试图熟悉python第三方库,如pandas和numpy。

我一直在审查这个名为桑坦德客户满意度分类的竞赛中的脚本,我一直在修改不同的分叉脚本以便对它们进行试验。

这是一个经过修改的脚本,我试图通过它来实现XGBoost:

import pandas as pd
from sklearn import cross_validation as cv
import xgboost as xgb

df_train = pd.read_csv("/Users/pavan7vasan/Desktop/Machine_Learning/Project Datasets/Santander_Customer_Satisfaction/train.csv")
df_test  = pd.read_csv("/Users/pavan7vasan/Desktop/Machine_Learning/Project Datasets/Santander_Customer_Satisfaction/test.csv")   

df_train = df_train.replace(-999999,2)

id_test = df_test['ID']
y_train = df_train['TARGET'].values
X_train = df_train.drop(['ID','TARGET'], axis=1).values
X_test = df_test.drop(['ID'], axis=1).values

X_train, X_test, y_train, y_test = cv.train_test_split(X_train, y_train, random_state=1301, test_size=0.4)

clf = xgb.XGBClassifier(objective='binary:logistic',
                missing=9999999999,
                max_depth = 7,
                n_estimators=200,
                learning_rate=0.1, 
                nthread=4,
                subsample=1.0,
                colsample_bytree=0.5,
                min_child_weight = 3,
                reg_alpha=0.01,
                seed=7)

clf.fit(X_train, y_train, early_stopping_rounds=50, eval_metric="auc", eval_set=[(X_train, y_train), (X_test, y_test)])
y_pred = clf.predict_proba(X_test)

print("Cross validating and checking the score...")
scores = cv.cross_val_score(clf, X_train, y_train) 
'''
test = []
result = []
for each in id_test:
    test.append(each)
for each in y_pred[:,1]:
    result.append(each)

print len(test)
print len(result)
'''
submission = pd.DataFrame({"ID":id_test, "TARGET":y_pred[:,1]})
#submission = pd.DataFrame({"ID":test, "TARGET":result})
submission.to_csv("submission_XGB_Pavan.csv", index=False)

这是stacktrace:

Traceback (most recent call last):
  File "/Users/pavan7vasan/Documents/workspace/Machine_Learning_Project/Kaggle/XG_Boost.py", line 45, in <module>
submission = pd.DataFrame({"ID":id_test, "TARGET":y_pred[:,1]})
  File "/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 214, in __init__
mgr = self._init_dict(data, index, columns, dtype=dtype)
  File "/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 341, in _init_dict
dtype=dtype)
  File "/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 4798, in _arrays_to_mgr
index = extract_index(arrays)
  File "/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 4856, in extract_index
raise ValueError(msg)
ValueError: array length 30408 does not match index length 75818

我尝试过基于搜索不同解决方案的解决方案,但我无法弄清楚错误是什么。我出错了什么?请让我知道

1 个答案:

答案 0 :(得分:0)

问题在于您将X_test定义为@maxymoo提到的两次。首先,您将其定义为

X_test = df_test.drop(['ID'], axis=1).values

然后你用以下方式重新定义:

X_train, X_test, y_train, y_test = cv.train_test_split(X_train, y_train, random_state=1301, test_size=0.4)

这意味着现在X_test的大小等于0.4*len(X_train)。然后:

y_pred = clf.predict_proba(X_test)

您已经预测了X_train的那一部分,并且您尝试使用原始id_test的长度X_test初始X_fit创建数据框。 您可以在X_eval中使用train_test_splitX_train,而不是隐藏初始X_testcross_validation,因为对于您的X_train,您也有cv这意味着你没有得到正确答案,或者你//Put the value YourNewFragment ldf = new YourNewFragment (); Bundle args = new Bundle(); args.putString("KEY", "VALUE"); ldf.setArguments(args); //Inflate the fragment getFragmentManager().beginTransaction().add(R.id.container, ldf).commit(); In onCreateView of the new Fragment: //Retrieve the value String value = getArguments().getString("KEY"); 对公共/私人得分不准确。