训练模型时发生不兼容行维度的数值错误

我在一个数据集上实现了一个决策树。在此之前,我想使用CountVectorizer转换一个特定的列。为此,我使用管道来简化操作。

但出现了不兼容行维度的错误

代码

# 导入了库....from sklearn.feature_extraction.text import CountVectorizer as cvfrom sklearn.preprocessing import OneHotEncoder as ohefrom sklearn.compose import ColumnTransformer as ctfrom sklearn.pipeline import make_pipeline as mpfrom sklearn.tree import DecisionTreeClassifier as dtctransformer=ct(transformers=[('review_counts',cv(),['verified_reviews']),                             ('variation_dummies', ohe(),['variation'])                            ],remainder='passthrough')pipe= mp(transformer,dtc(random_state=42))x= data[['rating','variation','verified_reviews']].copy()y= data.feedbackx_train,x_test,y_train,y_test= tts(x,y,test_size=0.3,random_state=42,stratify=y)print(x_train.shape,y_train.shape)             # ((2205, 3), (2205,))pipe.fit(x_train,y_train)                       # 这一行报错

错误

---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-79-a981c354b190> in <module>()----> 1 pipe.fit(x_train,y_train)7 frames/usr/local/lib/python3.6/dist-packages/scipy/sparse/construct.py in bmat(blocks, format, dtype)    584                                                     exp=brow_lengths[i],    585                                                     got=A.shape[0]))--> 586                     raise ValueError(msg)    587     588                 if bcol_lengths[j] == 0:ValueError: blocks[0,:] has incompatible row dimensions. Got blocks[0,1].shape[0] == 2205, expected 1.

pic

问题

  1. 这个不兼容行维度的错误是如何形成的
  2. 如何解决这个问题?

回答:

尝试将所需列作为列表传递给ohe,而将简单的字符串传递给cv

from sklearn.feature_extraction.text import CountVectorizer as cvfrom sklearn.preprocessing import OneHotEncoder as ohefrom sklearn.compose import ColumnTransformer as ctfrom sklearn.pipeline import make_pipeline as mpfrom sklearn.tree import DecisionTreeClassifier as dtcdata = pd.DataFrame({'rating':np.random.randint(0,10,6),'variation':['a','b','c','a','b','c'],                   'verified_reviews':['adnf asd','sdf dsa','das j s','asd jd s','sad jds a','sajd'],                   'feedback':np.random.randint(0,2,6)})transformer=ct(transformers=[('review_counts',cv(),'verified_reviews'),                             ('variation_dummies', ohe(),['variation'])],               remainder='passthrough')pipe= mp(transformer, dtc(random_state=42))x= data[['rating','variation','verified_reviews']].copy()y= data.feedbackpipe.fit(x,y)

根据文档,每当变换器期望输入为一维数组时,列应指定为字符串(“xxx”)。对于期望二维数据的变换器,我们需要将列指定为字符串列表([“xxx”])。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注