gensim与numpy array 互转

目的

  将gensim输出的格式转化为numpy array格式,支持作为scikit-learn,tensorflow的输入

实施

使用nltk库的停用词和网上收集的资料整合成一份新的停用词表,用来过滤文档中的停用词,也去除了数字和特殊的标点符号,最后将所有字母转化为小写形式。

以下是原文:

Subject: Re: Candida(yeast) Bloom, Fact or Fiction

From: [email protected] (Pat Churchill)

Organization: Actrix Networks

Lines: 17

I am currently in the throes of a hay fever attack. SO who certainly

never reads Usenet, let alone Sci.med, said quite spontaneously "

There are a lot of mushrooms and toadstools out on the lawn at the

moment. Sure that‘s not your problem?"

Well, who knows? Or maybe it‘s the sourdough bread I bake?

After reading learned, semi-learned, possibly ignorant and downright

ludicrous stuff in this thread, I am about ready to believe anything :-)

If the hayfever gets any worse, maybe I will cook those toadstools...

--

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The floggings will continue until morale improves

[email protected] Pat Churchill, Wellington New Zealand

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

下面是分词后的结果:

[‘subject‘, ‘bloom‘, ‘fiction‘, ‘pat‘, ‘organization‘, ‘networks‘, ‘lines‘, ‘hay‘, ‘fever‘, ‘attack‘, ‘reads‘, ‘usenet‘, ‘lot‘, ‘lawn‘, ‘moment‘, ‘bread‘, ‘reading‘, ‘learned‘, ‘ignorant‘, ‘stuff‘, ‘thread‘, ‘ready‘, ‘worse‘, ‘cook‘, ‘continue‘, ‘pat‘, ‘wellington‘, ‘zealand‘]

使用gensim工具包转化为词袋子模型如下:

[(17, 1.0), (23, 1.0), (32, 1.0), (381, 1.0), (536, 1.0), (768, 1.0), (776, 1.0), (877, 1.0), (950, 1.0), (1152, 1.0), (1195, 1.0), (1389, 1.0), (1548, 1.0), (1577, 1.0), (1682, 2.0), (1861, 1.0), (2041, 1.0), (3098, 1.0), (3551, 1.0), (3886, 1.0), (5041, 1.0), (5148, 1.0), (5149, 1.0), (8494, 1.0), (8534, 1.0), (9972, 1.0), (11608, 1.0)]

上述gensim转换的格式不能直接作为scikit-learn,tensorflow的输入,需要使用

Gensim包的工具函数进行转换, 转换后变为:

[ 0. 0. 0. ..., 0. 0. 0.]

···

custom_train_matrix = gensim.matutils.corpus2dense(custom_train_corpus, num_terms=len_custom_dict).T # 关键方法为corpus2dense

custom_test_matrix = gensim.matutils.corpus2dense(custom_test_corpus, num_terms=len_custom_dict).T

···

上述输出的格式已经是numpy arrary格式了,可以作为scikit-learn,tensorflow的输入了。我们使用tf-idf技术,提高重要单词的比重,降低常见单词的比重,使用sckit-learn包转换上述输出如下:

(0, 11608) 0.179650698812

(0, 9972) 0.235827148023

(0, 8534) 0.208306524508

...................

(0, 381) 0.119518580927

(0, 32) 0.034904390394

(0, 23) 0.0374215245774

(0, 17) 0.0349485731726

上述输入已经可以作为scikit-learn的输入数据了,如果要作为tensorflow的输入数据,还需要将其转化为numpy array格式

···

custom_train_matrix = custom_train_matrix.toarray() # 将稀疏矩阵转化为numpy array

custom_test_matrix = custom_test_matrix.toarray() # 将稀疏矩阵转化为numpy array

···

原文地址:https://www.cnblogs.com/linyihai/p/8608926.html

时间: 2024-08-04 14:58:44

gensim与numpy array 互转的相关文章

python numpy array 的一些问题

1 将list转换成array 如果list的嵌套数组是不规整的,如 a = [[1,2], [3,4,5]] 则a = numpy.array(a)之后 a的type是ndarray,但是a中得元素a[i]都还是list 如果a = [[1,2], [3,4]] 则a = numpy.array(a)之后 a的type是ndarray,里面的元素a[i]也是ndarray 2 flatten函数 Python自身不带有flatten函数,numpy中array有flatten函数. 同1的一样

numpy.array

关于python中的二维数组,主要有list和numpy.array两种. 好吧,其实还有matrices,但它必须是2维的,而numpy arrays (ndarrays) 可以是多维的. 我们主要讨论list和numpy.array的区别: 我们可以通过以下的代码看出二者的区别 1 >>import numpy as np 2 >>a=[[1,2,3],[4,5,6],[7,8,9]] 3 >>a 4 [[1,2,3],[4,5,6],[7,8,9]] 5 >

python numpy array 与matrix 乘方

python numpy array 与matrix 乘方 编程语言 waitig 1年前 (2017-04-18) 1272℃ 百度已收录 0评论 数组array 的乘方(**为乘方运算符)是每个元素的乘方,而矩阵matrix的乘方遵循矩阵相乘,因此必须是方阵. 2*3的数组与矩阵 >>> from numpy import * >>> import operator >>> a = array([[1,2,3],[4,5,6]]) >>

第四十篇 Numpy.array的基本操作——向量及矩阵的运算

No.1. Numpy.array相较于Python原生List的性能优势 No.2. 将向量或矩阵中的每个元素 + 1 No.2. 将向量或矩阵中的所有元素 - 1 No.3. 将向量或矩阵中的所有元素 * 2 No.4. 将向量或矩阵中的所有元素 / 2 或 // 2 No.5. 幂运算 No.6. 取余 No.7. 取绝对值 No.8. 三角函数 No.9. 取e的x方 No.10. 取任意数的x方 No.11. 取以e为底x的对数 No.12. 取以任意数为底x的对数 No.13. 矩阵

numpy.array 合并和分割

# 导包 import numpy as np numpy.array 的合并 .concatenate() 一维数组 x = np.array([1, 2, 3]) # array([1, 2, 3]) y = np.array([3, 2, 1]) # array([3, 2, 1]) np.concatenate([x, y]) # array([1, 2, 3, 3, 2, 1]) z = np.array([666, 666, 666]) # array([666, 666, 666]

numpy array或matrix的交换两行

A[j,:] = A[maxindex,:] # 注意这样是一个很低级的错误!这样只是赋值 我们很容易想起python中的两个值交换一句搞定不用引入中间变量 a, b = b, a 但在numpy的array或matrix中,这样是错误的 需要使用选中两行来互换: A[[i, j], :] = A[[j, i], :] # 实现了第i行与第j行的互换 下面看一个实例: import numpy as np m = np.mat([[1. ,2 ,-1],[2,1,-2],[-3,1,1]]) p

numpy array转置与两个array合并

我们知道,用 .T 或者 .transpose() 都可以将一个矩阵进行转置. 但是一维数组转置的时候有个坑,光transpose没有用,需要指定shape参数, 在array中,当维数>=2,时这个成立,但=1时,就不成立了,如: In [7]: yOut[7]: array([0, 0, 0, 0, 0]) In [14]: y.TOut[14]: array([0, 0, 0, 0, 0]) In [15]: y.transpose()Out[15]: array([0, 0, 0, 0,

numpy.array的shape属性 —— 2018-09-07

numpy创建的数组都有一个shape属性,它是一个元祖,返回各个维度的维数 二维例子: >>> import numpy as np >>> y = np.array([[1,2,3],[4,5,6]]) >>> print(y) [[1 2 3] [4 5 6]] >>> print(y.shape) (2, 3) >>> print(y.shape[0]) 2 >>> print(y.shap

[Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate

数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.array([1,2,5]) b=np.array([10,12,15]) a_list=list(a) b_list=list(b) a_list.extend(b_list) a_list [1, 2, 5, 10, 12, 15] a=np.array(a_list) a array([ 1,  2