我试图反转一个大的(150000,150000)
稀疏矩阵如下:
import scipy as sp
import scipy.sparse.linalg as splu
#Bs is a large sparse matrix with shape=(150000,150000)
#calculating the sparse inverse
iBs=splu.inv(Bs)
Run Code Online (Sandbox Code Playgroud)
导致以下错误消息:
Traceback (most recent call last):
iBs=splu.inv(Bs)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/dsolve/linsolve.py", line 134, in spsolve
autoTranspose=True)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/dsolve/umfpack/umfpack.py", line 603, in linsolve
self.numeric(mtx)
File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/dsolve/umfpack/umfpack.py", line 450, in numeric
umfStatus[status]))
RuntimeError: <function umfpack_di_numeric at 0x7f2c76b1d320> failed with UMFPACK_ERROR_out_of_memory
Run Code Online (Sandbox Code Playgroud)
我重新编写了程序来简单地求解线性微分方程组:
import numpy as np
N=Bs.shape[0]
I=np.ones(N)
M=splu.spsolve(Bs,I)
Run Code Online (Sandbox Code Playgroud)
我又遇到了同样的错误
我在具有16 GB RAM的计算机上使用此代码,然后将其移动到具有32 GB RAM的服务器上,仍然无济于事.
有没有人遇到过这个?
我正在创建一个字典如下:
y=[(1,2),(2,3),(1,2),(5,6)]
dict={}
for tup in y:
tup=tuple(sorted(tup))
if tup in dict.keys():
dict[tup]=dict[tup]+1
else:
dict[tup]=1
Run Code Online (Sandbox Code Playgroud)
但是我的实际y
包含大约4000万个元组,有没有办法使用多处理来加速这个过程?
谢谢
我在Ubuntu 14.04上使用Python3,并在67条原始文本文章的语料库上运行Stanford POSTagger,经过编辑的python脚本如下:
from nltk.tag.stanford import POSTagger
with open('the_file.txt','r') as file:
G=file.readlines()
stan=[]
english_postagger = POSTagger('models/english-bidirectional-distsim.tagger', 'stanford-postagger.jar')
for line in g:
stan.append(english_postagger.tag(tokenize_fast(line)))
Run Code Online (Sandbox Code Playgroud)
经过几次迭代后,我得到以下错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at edu.stanford.nlp.sequences.ExactBestSequenceFinder.bestSequence(ExactBestSequenceFinder.java:109)
at edu.stanford.nlp.sequences.ExactBestSequenceFinder.bestSequence(ExactBestSequenceFinder.java:31)
at edu.stanford.nlp.tagger.maxent.TestSentence.runTagInference(TestSentence.java:322)
at edu.stanford.nlp.tagger.maxent.TestSentence.testTagInference(TestSentence.java:312)
at edu.stanford.nlp.tagger.maxent.TestSentence.tagSentence(TestSentence.java:135)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.tagSentence(MaxentTagger.java:998)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.tagCoreLabelsOrHasWords(MaxentTagger.java:1788)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.tagAndOutputSentence(MaxentTagger.java:1798)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1709)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1770)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1543)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.runTagger(MaxentTagger.java:1499)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.main(MaxentTagger.java:1842)
Run Code Online (Sandbox Code Playgroud)
我也从命令行运行斯坦福postagger:
java -mx300m -classpath stanford-postagger.jar edu.stanford.nlp.tagger.maxent.MaxentTagger -model models/wsj-0-18-bidirectional-distsim.tagger -textFile sample-input.txt > sample-tagged.txt
Run Code Online (Sandbox Code Playgroud)
有类似的错误。我什至通过了Java 2 GB的内存,仍然没有运气。
任何想法/想法或hacky类型的解决方案都将受到欢迎!
发现@nsanglar很好,所以我尝试了:
java -Xmx2g -classpath stanford-postagger.jar edu.stanford.nlp.tagger.maxent.MaxentTagger -model …
Run Code Online (Sandbox Code Playgroud) 所以我试图使用 Flask 将一个值从一个 html 页面传递到另一个 html 页面。我写的代码如下所示:
from flask import Flask, render_template
from flask import request, session, url_for,abort,redirect
app = Flask(__name__)
app.config['SECRET_KEY'] = 'oh_so_secret'
@app.route('/'):
def first():
session['this_one']='hello'
render('template.html')
@app.route('/second')
def second():
it=session['this_one']
render('other_page.html')
if __name__ == '__main__':
app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)
但是当我运行这个时,我得到了一个KeyError:this_one
.
所以今天重启系统就可以了。然后我做了一些改变:
@app.route('/'):
def first():
session['this_one']='goodbye'
render('template.html')
@app.route('/second')
def second():
it=session['this_one']
render('other_page.html')
Run Code Online (Sandbox Code Playgroud)
第二个函数仍在返回hello
。
所以看来session
字典并没有像我希望的那样被覆盖。
这是一个真正的谜,它在没有进行任何更改的情况下,有一天起作用,然后第二天就不起作用了。现在在路线中('/')
我设置一个列表:
@app.route('/'):
def first():
session['this_one']='hello'
session['a_list']=['a','b','c']
render('template.html')
Run Code Online (Sandbox Code Playgroud)
在第二个函数中使用 print 命令调用它:
@app.route('/second')
def second():
it=session['this_one']
print(session['a_list'])
render('other_page.html')
Run Code Online (Sandbox Code Playgroud)
[]
并返回一个空列表。
所以我有一个文件python_dictionary.json
,其中包含我想要附加的字典,而不必每次都打开.假设python_dictionary.json
只包含:
{
key1: value`
}
Run Code Online (Sandbox Code Playgroud)
我想补充一下
new_dictionary=
{
key2:value2
}
Run Code Online (Sandbox Code Playgroud)
现在我在做:
with open('python_dictionary.json','a') as file:
file.write(json.dumps(new_dictionary,indent=4))
Run Code Online (Sandbox Code Playgroud)
这会创建一个字典:
{
key1:value1
}
{
key2:value2
}
Run Code Online (Sandbox Code Playgroud)
这显然不是一本真正的字典.
我知道这一点:将值添加到现有的json文件而不重写它
但这涉及添加一个条目,我想做一个json.dumps
我有一个清单的理解:
thingie=[f(a,x,c) for x in some_list]
Run Code Online (Sandbox Code Playgroud)
我正在并行化如下:
from multiprocessing import Pool
pool=Pool(processes=4)
thingie=pool.map(lambda x: f(a,x,c), some_list)
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
_pickle.PicklingError: Can't pickle <function <lambda> at 0x7f60b3b0e9d8>:
attribute lookup <lambda> on __main__ failed
Run Code Online (Sandbox Code Playgroud)
我尝试安装pathos
显然可以解决此问题的软件包,但是当我尝试导入它时,出现错误:
ImportError: No module named 'pathos'
Run Code Online (Sandbox Code Playgroud) 我在python中有一个列表,看起来像
[['boy','121','is a male child'],['boy','121','is male'],['boy','121','is a child'],['girl','122','is a female child'],['girl','122','is a child']]
Run Code Online (Sandbox Code Playgroud)
我想根据每个列表中的前两个条目来减少列表
[['boy','121',is a male child, is male, is a child'],['girl','122','is a female child','is a child']]
Run Code Online (Sandbox Code Playgroud)
有没有办法有效地做到这一点,而无需创建虚拟列表?
我有一个(key,value)
以下形式的对列表:
x=[(('cat','dog),('a','b')),(('cat','dog'),('a','b')),(('mouse','rat'),('e','f'))]
Run Code Online (Sandbox Code Playgroud)
我想计算每个值元组与键元组一起出现的次数。
期望的输出:
[(('cat','dog'),('a','b',2)),(('mouse','rat'),('e','f',1))]
Run Code Online (Sandbox Code Playgroud)
一个可行的解决方案是:
xs=sc.parallelize(x)
xs=xs.groupByKey()
xs=xs.map(lambda (x,y):(x,Counter(y))
Run Code Online (Sandbox Code Playgroud)
然而,对于大型数据集,此方法会填满磁盘空间(~600GB)。我试图使用以下方法实现类似的解决方案reduceByKey
:
xs=xs.reduceByKey(Counter).collect()
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
TypeError: __init__() takes at most 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud) python ×6
dictionary ×1
flask ×1
java ×1
json ×1
list ×1
pandas ×1
pathos ×1
pyspark ×1
python-3.4 ×1
scipy ×1
set ×1
stanford-nlp ×1
umfpack ×1