Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 合并数字和文本特征以进行类别分类_Python_Machine Learning_Scipy_Scikit Learn - Fatal编程技术网

Python 合并数字和文本特征以进行类别分类

Python 合并数字和文本特征以进行类别分类,python,machine-learning,scipy,scikit-learn,Python,Machine Learning,Scipy,Scikit Learn,我试图对产品项目进行分类,以便根据产品名称和基本价格预测其类别 示例(产品名称、价格、类别): 以前,我只在预测任务中使用产品名称,但我想包括价格,看看准确性是否有所提高 我的代码的问题是无法合并文本/数字功能,我在SO中阅读了一些问题,这是我的代码摘录: #extracting features from text count_vect = CountVectorizer() X_train_counts = count_vect.fit_transform([e[0] for e in tr

我试图对产品项目进行分类,以便根据产品名称和基本价格预测其类别

示例(产品名称、价格、类别):

以前,我只在预测任务中使用产品名称,但我想包括价格,看看准确性是否有所提高

我的代码的问题是无法合并文本/数字功能,我在SO中阅读了一些问题,这是我的代码摘录:

#extracting features from text
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform([e[0] for e in training_set])
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

#extracting numerical features
X_train_price = np.array([e[1] for e in training_set])

X = sparse.hstack([X_train_tfidf, X_train_price]) #this is where the problem begins

clf = svm.LinearSVC().fit(X, [e[2] for e in training_set])
我尝试将数据类型与sparse.hstack合并,但出现以下错误:

ValueError: blocks[0,:] has incompatible row dimensions
我想问题在于X_train_price(一个价格列表),但我不知道如何设置它的格式以使稀疏函数成功工作

以下是两个阵列的形状:

>>> X_train_tfidf.shape
(65845, 23136)
>>>X_train_price.shape
(65845,)

在我看来,这应该像堆叠阵列一样简单。如果scikit learn遵循我熟悉的惯例,那么
X_train_tfidf
中的每一行都是一个训练数据点,总共有65845个点。所以你只需要做一个
hstack
——就像你说的那样

但是,您需要确保尺寸是兼容的!在vanilla
numpy
中,否则会出现此错误:

>>> a = numpy.arange(15).reshape(5, 3)
>>> b = numpy.arange(15, 20)
>>> numpy.hstack((a, b))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/
        Extras/lib/python/numpy/core/shape_base.py", line 270, in hstack
    return _nx.concatenate(map(atleast_1d,tup),1)
ValueError: arrays must have same number of dimensions
因此,在您的例子中,您需要一个shape
(65845,1)
数组,而不是
(65845,)
。我可能遗漏了一些内容,因为您使用的是稀疏数组。尽管如此,原则应该是相同的。基于上述代码,我不知道您使用的是什么稀疏格式,所以我只选择了一种进行测试:

>>> a = scipy.sparse.lil_matrix(numpy.arange(15).reshape(5, 3))
>>> scipy.sparse.hstack((a, b.reshape(5, 1))).toarray()
array([[ 0,  1,  2, 15],
       [ 3,  4,  5, 16],
       [ 6,  7,  8, 17],
       [ 9, 10, 11, 18],
       [12, 13, 14, 19]])

看看我在这里的回答,了解一个相关的问题
>>> b
array([15, 16, 17, 18, 19])
>>> b.reshape(5, 1)
array([[15],
       [16],
       [17],
       [18],
       [19]])
>>> numpy.hstack((a, b.reshape(5, 1)))
array([[ 0,  1,  2, 15],
       [ 3,  4,  5, 16],
       [ 6,  7,  8, 17],
       [ 9, 10, 11, 18],
       [12, 13, 14, 19]])
>>> a = scipy.sparse.lil_matrix(numpy.arange(15).reshape(5, 3))
>>> scipy.sparse.hstack((a, b.reshape(5, 1))).toarray()
array([[ 0,  1,  2, 15],
       [ 3,  4,  5, 16],
       [ 6,  7,  8, 17],
       [ 9, 10, 11, 18],
       [12, 13, 14, 19]])