使用这个可重复的小例子,我到目前为止还无法从3个数组生成一个新的整数数组,这些数组包含所有三个输入数组的唯一分组.
这些数组与地形属性有关:
import numpy as np
asp = np.array([8,1,1,2,7,8,2,3,7,6,4,3,6,5,5,4]).reshape((4,4)) #aspect
slp = np.array([9,10,10,9,9,12,12,9,10,11,11,9,9,9,9,9]).reshape((4,4)) #slope
elv = np.array([13,14,14,13,14,15,16,14,14,15,16,14,13,14,14,13]).reshape((4,4)) #elevation
Run Code Online (Sandbox Code Playgroud)
我们的想法是使用GIS例程将地理轮廓分解为3个不同的属性:
下面的小图试图描绘我所追求的结果类型(数组显示在左下方).请注意,图中给出的"答案"只是一个可能的答案.只要最终数组在每个行/列索引处包含一个标识唯一分组的整数,我就不关心结果数组中整数的最终排列.
例如,[0,1]和[0,2]处的数组索引具有相同的方面,斜率和高程,因此在结果数组中接收相同的整数标识符.
是否numpy的有一个内置的例程这种事情?
例如,如果我有两个numpy 1D阵列
x=np.array([1,2,3])
y=np.array([11,22,33])
Run Code Online (Sandbox Code Playgroud)
如何将这些拉入Numpy 2D坐标数组?如果我做:
x1,x2,x3=zip(*(x,y))
Run Code Online (Sandbox Code Playgroud)
结果是类型列表,而不是Numpy数组.所以我做到了
x1=np.asarray(x1)
Run Code Online (Sandbox Code Playgroud)
等等..是否有一个更简单的方法,我不需要调用np.asarray每个坐标?是否有Numpy zip函数返回Numpy数组?
假设我有一个形状为(1,256)的行向量。我想将其转换为形状为(256,1)的列向量。您在Numpy中会如何做?
python numpy linear-algebra multidimensional-array numpy-ndarray
我有一个时间序列的 numpy 数组 X。类似的东西:
[[0.05, -0.021, 0.003, 0.025, -0.001, -0.023, 0.095, 0.001, -0.018]
[0.015, 0.011, -0.032, -0.044, -0.002, 0.032, -0.051, -0.03, -0.020]
[0.04, 0.081, -0.02, 0.014, 0.063, -0.077, 0.059, 0.031, 0.025]]
Run Code Online (Sandbox Code Playgroud)
我可以用
fig, axes = plt.subplots(3, 1)
for i in range(3):
axes[i].plot(X[i])
plt.show()
Run Code Online (Sandbox Code Playgroud)
然后会出现类似下面的内容(这些图没有显示我上面写的演示值,而是具有类似结构的其他值)。所以 X 中的每一行都是一个时间序列。

但是我想要一个 numpy 数组,它将每个时间序列描述为灰度图像(因为我想稍后将它用于 cnn)。所以我认为我需要的应该是这样的:
[[[0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 0, 1]
[0, 0, 1, 0, 0, 0]]
[[0, 0, 1, 0, …Run Code Online (Sandbox Code Playgroud) 我想操作numpy数组来使用它们的索引,我想要包含0维的情况.现在我遇到了一个奇怪的情况,如果我不使用就地乘法,则会出现类型转换:
In [1]: import numpy as np
In [2]: x = 1.*np.array(1.)
In [3]: y = np.array(1.)
In [4]: y *= 1.
In [5]: x
Out[5]: 1.0
In [6]: y
Out[6]: array(1.)
In [7]: type(x)
Out[7]: numpy.float64
In [8]: type(y)
Out[8]: numpy.ndarray
Run Code Online (Sandbox Code Playgroud)
为什么x的类型与y不同?我知道,inplace操作是不同的实现,他们不创建数组的副本,但我不明白,为什么类型被更改,如果我将一个0d数组与浮点数相乘?它适用于1d阵列:
In [1]: import numpy as np
In [2]: x = np.array(1.)
In [3]: y = np.array([1.])
In [4]: 1.*x
Out[4]: 1.0
In [5]: 1.*y
Out[5]: array([1.])
In [7]: type(1.*x)
Out[7]: numpy.float64
In [8]: type(1.*y)
Out[8]: numpy.ndarray
Run Code Online (Sandbox Code Playgroud)
我想,这很奇怪......现在我遇到了以下问题,我必须分别处理0d数组: …
关于如何在Python中重复数组中某个值的任何建议?例如,我想只重复2次array_a:
array_a = [1, 2, 1, 2, 1, 1, 2]
Run Code Online (Sandbox Code Playgroud)
想要的结果是:我重复每一个2并离开1:
array_a = [1, 2, 2, 1, 2, 2, 1, 1, 2, 2] # only the `2` should be repeated
Run Code Online (Sandbox Code Playgroud)
我试过numpy,我可以复制整个数组,但不是一定的值.
在NumPy工作,我了解如何使用本文从3D数组中切割2D数组.
根据我想切入的轴:
array = [[[0 1 2]
[3 4 5]
[6 7 8]]
[[9 10 11]
[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]
[24 25 26]]]
Run Code Online (Sandbox Code Playgroud)
切片会给我:
i_slice = array[0]
[[0 1 2]
[3 4 5]
[6 7 8]]
j_slice = array[:, 0]
[[0 1 2]
[9 10 11]
[18 19 20]]
k_slice = array[:, :, 0]
[[0 3 6]
[9 12 15]
[18 21 24]]
Run Code Online (Sandbox Code Playgroud)
但是有可能以45度角切割?如:
j_slice_down = array[slice going down …Run Code Online (Sandbox Code Playgroud) 我想知道是否有更有效/Pythonic 的方法来添加多个 numpy 数组(2D)而不是:
def sum_multiple_arrays(list_of_arrays):
a = np.zeros(shape=list_of_arrays[0].shape) #initialize array of 0s
for array in list_of_arrays:
a += array
return a
Run Code Online (Sandbox Code Playgroud)
Ps:我知道, np.add()但它只适用于 2 个数组。
我有一个 2D numpy.ndarray。给定位置列表,我想找到同一行中给定元素右侧的第一个非零元素的位置。是否可以将其矢量化?我有一个巨大的数组,循环花费了太多时间。
例如:
matrix = numpy.array([
[1, 0, 0, 1, 1],
[1, 1, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1, 1, 1, 1],
[1, 0, 0, 0, 1]
])
query = numpy.array([[0,2], [2,1], [1,3], [0,1]])
Run Code Online (Sandbox Code Playgroud)
预期结果:
>> [[0,3], [2,4], [1,4], [0,3]]
Run Code Online (Sandbox Code Playgroud)
目前我正在使用 for 循环执行此操作,如下所示
for query_point in query:
y, x = query_point
result_point = numpy.min(numpy.argwhere(self.matrix[y, x + 1:] == 1)) + x + 1
print(f'{y}, {result_point}')
Run Code Online (Sandbox Code Playgroud)
PS:我也想找到左边的第一个非零元素。我想,找到正确点的解决方案可以很容易地找到左点。
我有以下代码来估计新的 numpy 数组是否适合内存:
import numpy as np
from subprocess import Popen, PIPE
def getTotalMemory():
output, _= Popen(r'wmic ComputerSystem get TotalPhysicalMemory', stdout=PIPE).communicate()
val = ''.join([c for c in str(output) if c.isdigit()])
return int(val)
def getFreeMemory():
output, _= Popen(r'wmic OS get FreePhysicalMemory', stdout=PIPE).communicate()
val = ''.join([c for c in str(output) if c.isdigit()])
# FreePhysicalMemory returns kilobytes, hence the multiplication with 1024
return 1024 * int(val)
def estimateNumpySize(dim, square = False, nptype = np.float64):
# Make sure that the actual nptype is an …Run Code Online (Sandbox Code Playgroud) numpy ×10
numpy-ndarray ×10
python ×9
arrays ×3
in-place ×1
list ×1
matplotlib ×1
matrix ×1
numpy-ufunc ×1
python-2.7 ×1
python-3.x ×1