如果你有一个稀疏矩阵X:
>> X = csr_matrix([[0,2,0,2],[0,2,0,1]])
>> print type(X)
>> print X.todense()
<class 'scipy.sparse.csr.csr_matrix'>
[[0 2 0 2]
[0 2 0 1]]
Run Code Online (Sandbox Code Playgroud)
矩阵Y:
>> print type(Y)
>> print text_scores
<class 'numpy.matrixlib.defmatrix.matrix'>
[[8]
[5]]
Run Code Online (Sandbox Code Playgroud)
...如何将X的每个元素乘以Y的行.例如:
[[0*8 2*8 0*8 2*8]
[0*5 2*5 0*5 1*5]]
Run Code Online (Sandbox Code Playgroud)
要么:
[[0 16 0 16]
[0 10 0 5]]
Run Code Online (Sandbox Code Playgroud)
我已经厌倦了这个,但显然它不起作用,因为尺寸不匹配:
Z = X.data * Y
如果你有两个numpy矩阵,你如何将它们合并为一个?它们应该水平连接,以便
[[0] [1] [[0][1]
[1] + [0] = [1][0]
[4] [1] [4][1]
[0]] [1]] [0][1]]
Run Code Online (Sandbox Code Playgroud)
例如,使用这些矩阵:
>>type(X)
>>type(Y)
>>X.shape
>>Y.shape
<class 'numpy.matrixlib.defmatrix.matrix'>
<class 'numpy.matrixlib.defmatrix.matrix'>
(53, 1)
(53, 1)
Run Code Online (Sandbox Code Playgroud)
我试过hstack但得到一个错误:
>>Z = hstack([X,Y])
Traceback (most recent call last):
File "labels.py", line 85, in <module>
Z = hstack([X, Y])
File "C:\Python27\lib\site-packages\scipy\sparse\construct.py", line 263, in h
stack
return bmat([blocks], format=format, dtype=dtype)
File "C:\Python27\lib\site-packages\scipy\sparse\construct.py", line 329, in b
mat
raise ValueError('blocks must have rank 2')
ValueError: blocks must have rank 2
Run Code Online (Sandbox Code Playgroud) 我似乎无法让MatPlotLib工作.我下载并安装了正确的版本(matplotlib-1.1.0.win32-py2.7.exe),我已经安装了numpy和scipy(它们没有问题).
这是我得到的错误:
C:\python code>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", line 95, in <module>
new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "C:\Python27\lib\site-packages\matplotlib\backends\__init__.py", line 25,
in pylab_setup
globals(),locals(),[backend_name])
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_tkagg.py", lin
e 8, in <module>
import Tkinter as Tk, FileDialog
File "C:\Python27\lib\lib-tk\Tkinter.py", line 38, in <module> …
Run Code Online (Sandbox Code Playgroud) 我正在 Python ( sklearn ) 中进行多元线性回归,但由于某种原因,系数没有正确返回为列表。相反,返回一个列表 IN A LIST:
from sklearn import linear_model
clf = linear_model.LinearRegression()
# clf.fit ([[0, 0, 0], [1, 1, 1], [2, 2, 2]], [0, 1, 2])
clf.fit([[394, 3878, 13, 4, 0, 0],[384, 10175, 14, 4, 0, 0]],[3,9])
print 'coef array',clf.coef_
print 'length', len(clf.coef_)
print 'getting value 0:', clf.coef_[0]
print 'getting value 1:', clf.coef_[1]
Run Code Online (Sandbox Code Playgroud)
这将返回列表 [[]] 而不是列表 [] 的列表中的值。知道为什么会这样吗?输出:
coef array [[ 1.03428648e-03 9.54477167e-04 1.45135995e-07 0.00000000e+00
0.00000000e+00 0.00000000e+00]]
length 1
getting value 0: [ 1.03428648e-03 9.54477167e-04 1.45135995e-07 …
Run Code Online (Sandbox Code Playgroud) NLTK书中有几个单词计数的例子,但实际上它们不是字数而是令牌数.例如,第1章,计数词汇表说以下给出了一个单词计数:
text = nltk.Text(tokens)
len(text)
Run Code Online (Sandbox Code Playgroud)
但是,它没有 - 它给出了一个单词和标点符号.你怎么能得到真实的字数(忽略标点符号)?
同样,如何获得单词中的平均字符数?显而易见的答案是:
word_average_length =(len(string_of_text)/len(text))
Run Code Online (Sandbox Code Playgroud)
然而,这将是关闭因为:
我在这里错过了什么吗?这必须是一个非常常见的NLP任务......
如何使用WordNet确定python中两个文本之间的语义相似度?
明显的预处理将是删除停止词和词干,但那又是什么?
我能想到的唯一方法是计算两个文本中每个单词之间的WordNet路径距离.这是unigrams的标准.但这些是大型(400字)文本,即自然语言文档,其中的单词不具有任何特定顺序或结构(除英语语法强加的单词外).那么,你会在文本之间比较哪些词?你会如何在python中做到这一点?
如何将1列矩阵添加到稀疏矩阵,或者将稀疏matix添加到列矩阵(两种方式)?它不应该替换数据,只需将其加入一种数据类型即可.
稀疏矩阵:
>>print type(X)
>>print X.shape
<class 'scipy.sparse.csr.csr_matrix'>
(53, 6596)
Run Code Online (Sandbox Code Playgroud)
要添加的列:
>>print type(Y)
>>print Y.shape
<class 'numpy.matrixlib.defmatrix.matrix'>
(53, 1)
Run Code Online (Sandbox Code Playgroud)
你怎么能这样做?
python文档暗示重复项可以存在于列表中,这由assignmnet支持:list = ["word1","word1"].但是,Python的append()似乎没有添加项目,如果它已经在列表中.我在这里遗漏了什么,或者这是故意尝试set()之类的行为?
>> d = {}
>> d["word1"] = 1
>> d["word2"] = 2
>> d["word2"] = 3
>> vocab = []
>> for word,freq in d.iteritems():
>> ... vocab.append(word)
>> for item in vocab:
>> ... print item
Run Code Online (Sandbox Code Playgroud)
收益:
word1
word2
Run Code Online (Sandbox Code Playgroud)
哪个是第二个字2?
在列表字典中删除每个列表的最后一个元素有什么更加pythonic或有效的方法?
例如,拿这个:
listDict = {'tom': [-2,10,2,-8], 'sam': [-9,-10,-10,-7]}
Run Code Online (Sandbox Code Playgroud)
并将其转化为:
listDict = {'tom': [-2,10,2], 'sam': [-9,-10,-10]}
Run Code Online (Sandbox Code Playgroud)
这就是我目前正在做的事情:
new = {}
for t,item in listDict.iteritems():
new[t] = [item[0], item[1], item[2]]
listDict= new
Run Code Online (Sandbox Code Playgroud) 这是一个关于ngrams线性回归的问题,使用Tf-IDF(术语频率 - 逆文档频率).为此,我使用numpy稀疏矩阵和sklearn进行线性回归.
使用unigrams时我有53个病例和超过6000个功能.预测基于使用LeaveOneOut的交叉验证.
当我创建一个只有unigram分数的tf-idf稀疏矩阵时,我得到的预测比我创建unigram + bigram分数的tf-idf稀疏矩阵要好一些.我添加到矩阵的列越多(三元组,四元组,五元组等的列),回归预测的准确性就越低.
这是常见的吗?这怎么可能?我会认为功能越多越好.
python ×9
matrix ×3
nlp ×3
numpy ×3
scipy ×3
nltk ×2
regression ×2
append ×1
dictionary ×1
list ×1
matplotlib ×1
scikit-learn ×1
tf-idf ×1
wordnet ×1