看到这个答案我想知道X的平面视图的创建是否基本相同,只要我知道X中的轴数是3:
A = X.ravel()
s0, s1, s2 = X.shape
B = X.reshape(s0*s1*s2)
C = X.reshape(-1) # thanks to @hpaulj below
Run Code Online (Sandbox Code Playgroud)
我不是在问A和B和C是否相同.
我想知道在这种情况下特定使用ravel和reshape在这种情况下是否基本相同,或者如果您提前知道X的轴数,是否存在重大差异,优点或缺点.
第二种方法需要几微秒,但这似乎与尺寸无关.
按照http://sfepy.org/doc-devel/installation.html#installing-sfepy 中的指示,我使用以下命令将 SfePy 安装到我的 Python 2.7 anaconda
conda install -c conda-forge sfepy
在那之后,我不能再导入 numpy
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/david/anaconda2/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import core
File "/Users/david/anaconda2/lib/python2.7/site-packages/numpy/core/__init__.py", line 91, in <module>
raise ImportError(msg.format(path))
ImportError: Something is wrong with the numpy installation. While importing we
detected an older version of numpy in ['/Users/david/anaconda2/lib/python2.7/site-packages/numpy']. One method of fixing this is to repeatedly uninstall numpy until …Run Code Online (Sandbox Code Playgroud) 我正在使用其他地方的方法,它需要内置的整数类型int而不是 NumPy 创建的类型。这是一个简化:
a = np.arange(6, dtype='int').reshape(2,3)
b = a.tolist()
f(a[0,0]) # fails
f(b[0][0]) # works
Run Code Online (Sandbox Code Playgroud)
失败给出错误信息:
类型错误:bpy_struct:item.attr = val:int 类型的预期序列项,而不是 numpy.int64
虽然tolist()有效,但我在此过程中失去了 NumPy。我的问题是,我可以使用内置类型的 int 来保持 NumPy 的形式和灵活性吗?
我想存储一个类和许多实例供以后使用,或提供给其他人。
到目前为止,我可以腌制和恢复实例,但是在加载它们之前,我必须手动重新创建该类。
我查看了这份文档,这使我相信我应该能够以某种方式执行此操作,但是我似乎无法确切地找到执行该操作的方法。
编辑:我读过这个答案讨论使用的dill(见这问题的答案也),但我没有dill安装。我想要一个泡菜解决方案(如果存在)。
import numpy as np
import pickle
class wow(object):
def __init__(self, x):
self.x = x
w5 = wow(np.arange(5))
w3 = wow(range(3))
with open("w5w3.pickle", "w") as outfile:
pickle.dump([w5, w3], outfile)
# save the class also
with open("wow.pickle", "w") as outfile:
pickle.dump(wow, outfile)
# OK, now delete class wow, then try to recover the pickles
del wow, w3, w5
try:
with open("wow.pickle", "r") as infile:
wow = pickle.load(infile)
except …Run Code Online (Sandbox Code Playgroud) 以下是bookhomography-example-1.jpg再bookhomography-example-2.jpg从流行的OpenCV博文约单应。
我可以做单应性和扭曲图像,但是当我尝试使用或时,h或h[0]不起作用。我也尝试过将2D数组转换为元组的元组,但是没有任何变化。这可能很简单,但我无法弄清楚。cv2.perspectiveTransform(pts, h)cv2.perspectiveTransform(pts, h[0])h[0]
错误信息:
追溯(最近一次通话):
T_dst = cv2.perspectiveTransform(pts_dst,h)中的文件“ bookhomography stackexchange v00.py”,第36行,TypeError:m不是数字元组
注:设置False于True诱发失败。两条变换线之一是方向错误,但均会失败。
import numpy as np
import matplotlib.pyplot as plt
import cv2
im_src = cv2.imread("bookhomography-example-2.jpg")
im_dst = cv2.imread("bookhomography-example-1.jpg")
im_srcrgb = cv2.cvtColor(im_src, cv2.COLOR_BGR2RGB)
im_dstrgb = cv2.cvtColor(im_dst, cv2.COLOR_BGR2RGB)
pts_src = np.float32([52, 376, 240, 528, 413, 291, 217, 266]).reshape(4, -1)
pts_dst = np.float32([56, 478, 387, 497, 376, 124, 148, 218]).reshape(4, -1)
h = cv2.findHomography(pts_src, …Run Code Online (Sandbox Code Playgroud) 当我显示一个数组时,对象的默认__repr__()方法对于ndarray我想要做的来说太大了:
a = np.eye(32)
b = {'hello':42, 'array':a}
b
Run Code Online (Sandbox Code Playgroud)
产生:
{'array': array([[ 1., 0., 0., ..., 0., 0., 0.],
[ 0., 1., 0., ..., 0., 0., 0.],
[ 0., 0., 1., ..., 0., 0., 0.],
...,
[ 0., 0., 0., ..., 1., 0., 0.],
[ 0., 0., 0., ..., 0., 1., 0.],
[ 0., 0., 0., ..., 0., 0., 1.]]), 'hello': 42}
Run Code Online (Sandbox Code Playgroud)
我尝试了一个丑陋的解决方案,重新分配__repr__:
def wow():
return "wow!"
a.__repr__ = wow
Run Code Online (Sandbox Code Playgroud)
这会产生归因错误,我并不感到惊讶:
Traceback (most recent …Run Code Online (Sandbox Code Playgroud) 在2.7中学习pythonic.有没有办法避免显式循环?回答=[5, 4, 4, 3, 3, 2]
import numpy as np
import scipy.special as spe
nmax = 5 # n = 0, 1 ...5
mmax = 7 # m = 1, 2 ...7
big = 15.
z = np.zeros((nmax+1, mmax))
for i in range(nmax+1):
z[i] = spe.jn_zeros(i, mmax)
answer = [np.max(np.where(z[i]<big))+1 for i in range(nmax+1)]
print answer # list of the largest m for each n where the mth zero of Jn < big
Run Code Online (Sandbox Code Playgroud) 要生成出现在set a中的值的排序列表,这可以:
a = [1,1,7,3,2,9,2]
b = list(set(a))
b.sort()
print b
Run Code Online (Sandbox Code Playgroud)
但是将两条线放在一起,它返回None:
a = [1,1,7,3,2,9,2]
b = list(set(a)).sort()
print b
Run Code Online (Sandbox Code Playgroud)
尽管这些都是真的:
type(list(set(a))) is list
len(list(set(a))) > 0
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么第二种方法只返回一个排序列表 - 为什么我必须将它们放在不同的行上(如第一种方法)?