小编Zac*_*ach的帖子

字段之间无关的字典共享?

以下代码应该创建频率分布的新(修改)版本(nltk.FreqDist).两个变量应该是相同的长度.

当创建单个WebText实例时,它可以正常工作.但是当创建多个WebText实例时,新变量似乎被所有对象共享.

例如:

import nltk
from operator import itemgetter

class WebText:

    freq_dist_weighted = {}

    def __init__(self, text):
        tokens = nltk.wordpunct_tokenize(text) #tokenize
        word_count = len(tokens)
        freq_dist = nltk.FreqDist(tokens)


        for word,frequency in freq_dist.iteritems():
            self.freq_dist_weighted[word] = frequency/word_count*frequency
        print len(freq_dist), len(self.freq_dist_weighted)

text1 = WebText("this is a test")
text2 = WebText("this is another test")
text3 = WebText("a final sentence")
Run Code Online (Sandbox Code Playgroud)

结果是

4 4
4 5
3 7
Run Code Online (Sandbox Code Playgroud)

这是不正确的.由于我只是转置和修改值,因此每列中应该有相同的数字.如果我在循环之前重置freq_dist_weighted,它可以正常工作:

import nltk
from operator import itemgetter

class WebText:

    freq_dist_weighted = {} 

    def __init__(self, text):
        tokens = nltk.wordpunct_tokenize(text) #tokenize …
Run Code Online (Sandbox Code Playgroud)

python class nltk data-structures

2
推荐指数
2
解决办法
1112
查看次数

在将python浮点数写入文件时,请使用科学记数法

将浮点数写入CSV会像下面这样写入其中一些:2.0628800997782577e-05

c = csv.writer(open(file, "wb"))
c.writerow([var1, var2])    
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

  • 我已经在StackOverflow上的其他答案之后尝试了var1**8,但这只是将它们提升到了8的幂.
  • 我也尝试过Decimal(var1),但这并不能抑制科学记数法.

这使得以后很难处理excel中的输出,因为它被识别为文本而不是数字.如何以非科学的十进制表示法打印?

python floating-point notation

2
推荐指数
1
解决办法
2496
查看次数

按行切片numpy数组除1行外

如何按列切割numpy数组,并排除特定行?

想象一下,你有一个numpy数组,其中第一列作为"玩家"的索引,接下来的列是不同游戏中的玩家得分.如何排除游戏的分数,同时排除一名玩家.

例如:

[0  0  0  0
 1  2  1  1 
 2 -6  0  2
 3  4  1  3]
Run Code Online (Sandbox Code Playgroud)

如果你想返回第一个分数(第1列),你会这样做:

>>score[:,1]
[0,2,-6,4]
Run Code Online (Sandbox Code Playgroud)

但是如何排除玩家/排?如果那个玩家/第3行,你怎么得到:

[0,2,-6]
Run Code Online (Sandbox Code Playgroud)

或者,如果该玩家/第1行,你如何得到:

[0,-6, 4]
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

2
推荐指数
1
解决办法
6319
查看次数

稀疏矩阵上的逐元素运算

如果你有一个稀疏矩阵X:

>> print type(X)
<class 'scipy.sparse.csr.csr_matrix'>
Run Code Online (Sandbox Code Playgroud)

...如何将每行中每个元素的平方相加,并将它们保存到列表中?例如:

>>print X.todense()
[[0 2 0 2]
 [0 2 0 1]]
Run Code Online (Sandbox Code Playgroud)

如何将其转换为每行的平方和列表:

[[0²+2²+0²+2²]
 [0²+2²+0²+1²]]
Run Code Online (Sandbox Code Playgroud)

要么: [8, 5]

python numpy scipy sparse-matrix

2
推荐指数
1
解决办法
4096
查看次数

查找对象python中变量的平均值

如何迭代一组对象以最有效的方式找到它们的平均值?这只使用一个循环(除了Numpy中的循环),但我想知道是否有更好的方法.目前,我这样做:

scores = []
ratings= []
negative_scores = []
positive_scores = []

for t in text_collection:
 scores.append(t.score)
 ratings.append(t.rating)
 if t.score < 0:
    negative_scores.append(t.score)
 elif t.score > 0:
    positive_scores.append(t.score)

print "average score:", numpy.mean(scores)
print "average rating:", numpy.mean(ratings)
print "average negative score:", numpy.mean(negative_scores)
print "average positive score:", numpy.mean(positive_scores)
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法呢?

python numpy

1
推荐指数
1
解决办法
4273
查看次数

在python函数中调用参数作为函数定义的一部分?

如何在函数定义中的函数输入中传递参数?

例如:

def correlate1(vara1, vara2=vara1*2):
    print "test"
Run Code Online (Sandbox Code Playgroud)

返回:

Traceback (most recent call last):
  File "test.py", line 321, in <module>
    def correlate1(vara1, vara2=vara*2):
NameError: name 'vara1' is not defined
Run Code Online (Sandbox Code Playgroud)

显然,你可以这样做:

    def correlate1(vara1, vara2=0):
        if vara2==0:
            var2=vara1*2        
        print "test"
Run Code Online (Sandbox Code Playgroud)

但有没有办法在函数定义中做到这一点?

python arguments function

1
推荐指数
1
解决办法
83
查看次数

matplotlib的线性回归线给出了ValueError

我试图在散点图上绘制线性回归线.我已经研究过它(例如使用matplotlib/numpy进行线性回归),但我的暗示不起作用

    x = [-6.0, -5.0, -10.0, -5.0, -8.0, -3.0, -6.0, -8.0, -8.0, 7.5, 8.0, 9.0, 10.0, 7.0, 5.0, 5.0, -8.0, 8.0, 7.0] 
    y = [-7.094043198985176, -6.1018562538660044, -15.511155265492038, -2.7131460277126984, -8.6127363078417609, -3.1575686002528163, -10.246242711042497, -6.4333658386991992, -16.167988119268013, 2.4709555610646134, 4.5492058088492948, 5.5896790992867942, 3.3824425476540005, -1.8140272426684692, -1.5975329456235758, 5.1403915611396904, -4.4469105070935955, 0.51211850576547091, 5.7059436876065952]
    m,b = numpy.polyfit(x,y,1)
    plt.plot(x, y, x, m*x+b) 
    plt.show()
Run Code Online (Sandbox Code Playgroud)

返回:

Traceback (most recent call last):
  File "test.py", line 464, in <module>
    correlate(trainingSet,trainingSet.trainingTexts)
  File "test.py", line 434, in correlate
    plt.plot(x, y, x, m*x+b)
  File "C:\Python27\lib\site-packages\matplotlib\pyplot.py", …
Run Code Online (Sandbox Code Playgroud)

python numpy matplotlib

1
推荐指数
1
解决办法
2462
查看次数

索引numpy多维数组

我需要访问这个numpy数组,有时只有最后一列为0的行,有时候最后一列的值为1的行.

y = [0  0  0  0
     1  2  1  1 
     2 -6  0  1
     3  4  1  0]
Run Code Online (Sandbox Code Playgroud)

我必须反复这样做,但更愿意回避创建重复的数组或每次都必须重新计算.有什么方法我可以识别相关指数并只是打电话给他们吗?所以我可以这样做:

>>print y[LAST_COLUMN_IS_0] 
[0  0  0  0
3  4  1  0]

>>print y[LAST_COLUMN_IS_1] 
[1  2  1  1 
2 -6  0  1]
Run Code Online (Sandbox Code Playgroud)

PS数组中的列数永远不会改变,它总是有4列.

python arrays numpy

0
推荐指数
1
解决办法
1448
查看次数