Pandas:ValueError(将 Sparse[float64, 0.0] dtypes 转换为 float64 数据类型的任何方式)
Pandas : ValueError ( any way to convert Sparse[float64, 0.0] dtypes to float64 datatype )
我有一个数据帧 X_train,我正在将两个数据帧连接到该数据帧。第二个和第三个数据帧是从 TF-IDF VEctorizer
生成的稀疏矩阵中获得的
q1_train_df = pd.DataFrame.sparse.from_spmatrix(q1_tdidf_train,index=X_train.index,columns=q1_features)
q2_train_df = pd.DataFrame.sparse.from_spmatrix(q2_tdidf_train,index=X_train.index,columns=q2_features)
X_train_final = pd.concat([X_train,q1_train_df,q2_train_df],axis=1)
X_train_final dtypes 如下所示
X_train_final.dtypes
cwc_min float64
cwc_max float64
csc_min float64
csc_max float64
ctc_min float64
...
q2_zealand Sparse[float64, 0.0]
q2_zero Sparse[float64, 0.0]
q2_zinc Sparse[float64, 0.0]
q2_zone Sparse[float64, 0.0]
q2_zuckerberg Sparse[float64, 0.0]
Length: 10015, dtype: object
我正在使用 XGBoost 在这个最终数据帧上训练,这在尝试拟合数据时抛出错误
model.fit( X_train_final,y_train)
ValueError: DataFrame.dtypes for data must be int, float or bool.
Did not expect the data types in fields q1_04, q1_10, q1_100, q
我认为错误是由于其中存在 Sparse[float64,0.0] dtypes 造成的。你能帮我解决一下吗,不知道如何解决这个错误??
实际上我刚刚遇到了同样的问题。我有一个使用 TF-IDF vectorizor 生成的列列表,我试图在数据集上使用 XGBoost。
这最终对我有用:
import xgboost as xgb
train_df = train_df.apply(pd.to_numeric, errors='coerce')
train_df[tf_idf_column_names] = train_df[tf_idf_column_names].sparse.to_dense()
train_x = train_df.iloc[:,1:]
train_y = train_df.iloc[:,:1]
dtrain= xgb.DMatrix(data=train_x, label=train_y)
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}
num_round = 2
bst = xgb.train(param, dtrain, num_round)
preds = bst.predict(dtest)
X_train_final = hstack( blocks=(x_tr_cwc_min,\
x_tr_cwc_max,\
x_tr_csc_min,\
x_tr_csc_max,\
x_tr_ctc_min,\
x_tr_ctc_max,\
x_tr_last_word_eq,\
x_tr_first_word_eq,\
x_tr_abs_len_diff,\
x_tr_mean_len,\
x_tr_token_set_ratio,\
x_tr_token_sort_ratio,\
x_tr_fuzz_ratio,\
x_tr_fuzz_partial_ratio,\
x_tr_longest_substr_ratio,\
q1_tdidf_train,q2_tdidf_train
)
).tocsr()
这里我没有直接使用 X_train 数据框,而是使用了 X_train 的各个列并将它们中的每一个都转换为 ndarrays。
To dense 是可行的,但对于我使用的数据框,它消耗了将近 3 GB 的 space !!!所以不得不采用这种方法
我有一个数据帧 X_train,我正在将两个数据帧连接到该数据帧。第二个和第三个数据帧是从 TF-IDF VEctorizer
生成的稀疏矩阵中获得的q1_train_df = pd.DataFrame.sparse.from_spmatrix(q1_tdidf_train,index=X_train.index,columns=q1_features)
q2_train_df = pd.DataFrame.sparse.from_spmatrix(q2_tdidf_train,index=X_train.index,columns=q2_features)
X_train_final = pd.concat([X_train,q1_train_df,q2_train_df],axis=1)
X_train_final dtypes 如下所示
X_train_final.dtypes
cwc_min float64
cwc_max float64
csc_min float64
csc_max float64
ctc_min float64
...
q2_zealand Sparse[float64, 0.0]
q2_zero Sparse[float64, 0.0]
q2_zinc Sparse[float64, 0.0]
q2_zone Sparse[float64, 0.0]
q2_zuckerberg Sparse[float64, 0.0]
Length: 10015, dtype: object
我正在使用 XGBoost 在这个最终数据帧上训练,这在尝试拟合数据时抛出错误
model.fit( X_train_final,y_train)
ValueError: DataFrame.dtypes for data must be int, float or bool.
Did not expect the data types in fields q1_04, q1_10, q1_100, q
我认为错误是由于其中存在 Sparse[float64,0.0] dtypes 造成的。你能帮我解决一下吗,不知道如何解决这个错误??
实际上我刚刚遇到了同样的问题。我有一个使用 TF-IDF vectorizor 生成的列列表,我试图在数据集上使用 XGBoost。
这最终对我有用:
import xgboost as xgb
train_df = train_df.apply(pd.to_numeric, errors='coerce')
train_df[tf_idf_column_names] = train_df[tf_idf_column_names].sparse.to_dense()
train_x = train_df.iloc[:,1:]
train_y = train_df.iloc[:,:1]
dtrain= xgb.DMatrix(data=train_x, label=train_y)
param = {'max_depth':2, 'eta':1, 'silent':1, 'objective':'binary:logistic'}
num_round = 2
bst = xgb.train(param, dtrain, num_round)
preds = bst.predict(dtest)
X_train_final = hstack( blocks=(x_tr_cwc_min,\
x_tr_cwc_max,\
x_tr_csc_min,\
x_tr_csc_max,\
x_tr_ctc_min,\
x_tr_ctc_max,\
x_tr_last_word_eq,\
x_tr_first_word_eq,\
x_tr_abs_len_diff,\
x_tr_mean_len,\
x_tr_token_set_ratio,\
x_tr_token_sort_ratio,\
x_tr_fuzz_ratio,\
x_tr_fuzz_partial_ratio,\
x_tr_longest_substr_ratio,\
q1_tdidf_train,q2_tdidf_train
)
).tocsr()
这里我没有直接使用 X_train 数据框,而是使用了 X_train 的各个列并将它们中的每一个都转换为 ndarrays。 To dense 是可行的,但对于我使用的数据框,它消耗了将近 3 GB 的 space !!!所以不得不采用这种方法