如何csr_matrix以便携式格式保存/加载scipy稀疏?scipy稀疏矩阵在Python 3(Windows 64位)上创建,以在Python 2(Linux 64位)上运行.最初,我使用了pickle(使用protocol = 2和fix_imports = True),但这从Python 3.2.2(Windows 64位)到Python 2.7.2(Windows 32位)不起作用并得到错误:
TypeError: ('data type not understood', <built-in function _reconstruct>, (<type 'numpy.ndarray'>, (0,), '[98]')).
Run Code Online (Sandbox Code Playgroud)
接下来,尝试过numpy.save,numpy.load以及scipy.io.mmwrite()并且scipy.io.mmread()这些方法都没有奏效.
对于Python3,我跟着@Martijn Pieters的代码:
import gzip
import json
# writing
with gzip.GzipFile(jsonfilename, 'w') as fout:
for i in range(N):
uid = "whatever%i" % i
dv = [1, 2, 3]
data = json.dumps({
'what': uid,
'where': dv})
fout.write(data + '\n')
Run Code Online (Sandbox Code Playgroud)
但这会导致错误:
Traceback (most recent call last):
...
File "C:\Users\Think\my_json.py", line 118, in write_json
fout.write(data + '\n')
File "C:\Users\Think\Anaconda3\lib\gzip.py", line 258, in write
data = memoryview(data)
TypeError: memoryview: a bytes-like object is required, not 'str'
Run Code Online (Sandbox Code Playgroud)
关于发生了什么的任何想法?
在Linux集群上,我通过请求收到此错误:
ConnectionError:HTTPConnectionPool(host ='andes-1-47',port = 8181):使用url:/ jammy/api/v1超出最大重试次数(由:''引起)
这个错误是什么意思?它是一个请求问题还是主机上的问题,解决方案是什么?
顺便说一句,该程序在具有localhost的Windows和Linux独立计算机上成功运行.
我想在使用该argparse库的命令行程序中为特定的使用错误生成自定义错误消息.我知道我可以通过子类化来覆盖错误的一般表示argparse.ArgumentParser:
class HelpParser(argparse.ArgumentParser):
def error(self, message):
sys.stderr.write('error: %s\n' % message)
sys.exit(2)
parser = HelpParser(... ...)
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)
但是当error调用我的方法时,message已经被库格式化了.例如,
> python prog.py old stuff
usage: prog [-h] {hot,cold,rain,snow} ...
prog: error: argument subparser: invalid choice: 'old' (choose from u'hot', u'cold', u'rain', u'snow')
Run Code Online (Sandbox Code Playgroud)
我怎样才能改变后面的error:内容,例如
usage: prog [-h] {hot,cold,rain,snow} ...
error: 'old' is not a valid option. select from 'hot', 'cold', 'rain', 'snow'
Run Code Online (Sandbox Code Playgroud)
?
大多数现代API都是使用JSON构建的,并通过HTTP进行请求/响应消息传递.由于ZeroMQ是通过TCP,可以在ZeroMQ上构建基于JSON的API吗?如果是这样,优势是什么?用途是
开发人员为与一个或多个Web服务器通信的客户端/设备编写应用程序,以及
网络服务器与网络服务器进行通信.
将一个numpy数组索引为另一个 - 两者都定义为dtype ='uint32'.使用numpy.take索引并获得不安全的转换错误.以前没见过这个.知道发生了什么事吗?
Python 2.7.8 |Anaconda 2.1.0 (32-bit)| (default, Jul 2 2014, 15:13:35) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import numpy
>>> numpy.__version__
'1.9.0'
>>> a = numpy.array([9, 7, 5, 4, 3, 1], dtype=numpy.uint32)
>>> b = numpy.array([1, 3], dtype=numpy.uint32)
>>> c = a.take(b)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
c = a.take(b)
TypeError: Cannot cast array data from dtype('uint32') to dtype('int32') according to …Run Code Online (Sandbox Code Playgroud) 我正在寻找Python和/或Numpy矢量化的方法,以消除以下的for循环使用:
for i in list_range_values:
v[list_list_values[i]] += list_comp_values[i]
Run Code Online (Sandbox Code Playgroud)
哪里:
list_range_values是一个整数值的Python列表,例如.[1,3,5],从范围(0,R-1,1)中抽取
list_comp_values是数值的Python列表,例如.[0.7,9.8,1.25,5,10,11.7,6,0.2]这样len(list_comp_values)= R
v是长度为V的numpy向量,使得R可以是<,=,>而不是V.
list_list_values是一个列表的Python列表(每个列表包含不同数量的整数值,例如[[3,6,7],[5,7,11,25,99],[8,45],[4,7] ,8],[0,1],[21,31,41],[9,11,22,33,44],[17,19]])从范围(0,V-1,1)中绘制并使用len(list_list_values)= R.
例如.
for i in list_range_values (= [1, 3, 5]):
i=1: v[[5, 7, 11, 25, 99]] += list_comp_values[1] (= 9.8)
i=3: v[[4, 7, 8]] += list_comp_values[3] (= 5)
i=5: v[[21, 31, 41]] += list_comp_values[5] (= 11.7)
Run Code Online (Sandbox Code Playgroud)
有没有可用的方法可以消除for循环?
Cython,Scipy/Weave/Blitz和C模块是替代解决方案,但是要确保首先是否存在Numpy矢量化答案.
生成器表达式抛弃了大量的元组对,例如.以列表形式:
pairs = [(3, 47), (6, 47), (9, 47), (6, 27), (11, 27), (23, 27), (41, 27), (4, 67), (9, 67), (11, 67), (33, 67)]
Run Code Online (Sandbox Code Playgroud)
对于成对的每一对,使用key = pair [0]和value = pair [1],我想将此对流提供给字典以累积地添加相应键的值.明显的解决方案是:
dict_k_v = {}
for pair in pairs:
try:
dict_k_v[pair[0]] += pair[1]
except:
dict_k_v[pair[0]] = pair[1]
>>> dict_k_v
{33: 67, 3: 47, 4: 67, 6: 74, 9: 114, 11: 94, 41: 27, 23: 27}
Run Code Online (Sandbox Code Playgroud)
但是,这可以通过生成器表达式或一些不使用for循环的类似构造来实现吗?
编辑
为了澄清,生成器表达式抛弃了大量的元组对:
(3,47),(6,47),(9,47),(6,27),(11,27),(23,27),(41,27),(4,67),(9) ,67),(11,67),(33,67)......
并且我希望在生成每对时将每个键值对累积到字典中(参见Paul McGuire的答案).pairs = list []语句是不必要的,对此感到抱歉.对于每对(x,y),x是整数,y可以是整数或小数/浮点数.
我的生成器表达式的形式如下:
((x,y) for y in …Run Code Online (Sandbox Code Playgroud) 有,s = u'Gaga\xe2\x80\x99s'但需要转换为t = u'Gaga\u2019s'
如何才能最好地实现这一目标?
使用多处理,我想传递一个可迭代的和多个参数:
a) 到在 n_core cpu 上运行的函数 b) 一次产生(或返回)n_core 结果 c) 以任何完成顺序
from multiprocessing import Pool
def func(iterable, args):
this, that, other = args[0], args[1], args[2]
for s in iterable:
return ' '.join([s, this, that, other])
def main():
iterable = ['abc', 'bcd', 'cde', 'def', 'efg', 'fgh', 'ghi', 'hij']
args = ['this', 'that', 'other']
n_core = 2
p = Pool(n_core)
for r in p.imap_unordered(func, iterable, args):
print(r)
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
预期的结果是:
Run Code Online (Sandbox Code Playgroud)"abc this that other" "bcd this that other" "cde …