我是包含文本数据、分类数据和数字数据的数据集。我已经对文本数据进行了计数,并将其添加到dataframe中。现在我正在尝试拟合模型,我得到了下面的错误
误差
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_array(array, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
400 force_all_finite)
401 else:
--> 402 array = np.array(array, dtype=dtype, order=order, copy=copy)
403
404 if ensure_2d:
ValueError: could not convert string to float: 'IP'码
cv = CountVectorizer( max_features = 500,analyzer='word')
cv_addr = cv.fit_transform(data.pop('Clean_addr'))
for i, col in enumerate(cv.get_feature_names()):
data[col] = pd.SparseSeries(cv_addr[:, i].toarray().ravel(), fill_value=0)
train = data.drop(['Co_Name','Cust_ID','Phone','Shpr_ID','Resi_Cnt','Buz_Cnt','Nearby_Cnt','parseNumber','removeString','Qty','bins','Adj_Addr','Resi','Co_Name_FLag','Phone_Type'], axis=1)
Y = data['Resi']
X_train, X_test, y_train, y_test = train_test_split(train, Y, test_size=0.3)
gbc = GradientBoostingClassifier(max_depth = 7, n_estimators=1500, min_samples_leaf=10)
print('Training GBC')
gbc.fit(X_train, y_train)我猜这是因为在建立模型的时候,分类数据没有被转换成数值数据。如何动态地将它们转换为数字数据
稀疏矩阵中的范畴域
Phone_Type Co_Name_FLag Product
undefined Present IP
Landline Present IP
undefined Not_present IP
Landline Present IPD
Mobile Not_Present IP
Landline Present IE
Mobile Present IPF
Landline Present IP
undefined Present IP
Landline Present IP发布于 2017-11-01 07:17:59
您将得到这个ValueError,因为您还没有从train中删除Product字段。
您在设计矩阵中标识了三个分类字段:Product、Phone_Type和Co_Name_FLag。定义train时,删除后两个,但保留Product。如果您想保持Product作为预测器,可以在pd.get_dummies()仍然是Pandas格式时应用它,或者交替使用sklearn LabelEncoder。
无论哪种方法,重点都是将分类变量转换为一系列表示每个类别级别的指示变量。
https://stackoverflow.com/questions/47049487
复制相似问题