我正在寻找一种矢量化方法来在 NumPy 中乘以 3 个以上的向量。
举个例子,
X = np.array([1,2,3])
Y = np.array([4,5,6])
Z = np.array([7,8,9])
Multiply([X,Y,Z])
Run Code Online (Sandbox Code Playgroud)
将产生作为输出
np.array([28, 80, 162])
Run Code Online (Sandbox Code Playgroud)
我想要相乘的向量不需要像上面那样单独定义。例如,可以是矩阵的行(或列),在这种情况下,我想将此类矩阵的所有行(或列)相乘。
帮助赞赏:)
python numpy multiplication multidimensional-array numpy-ndarray
有没有一种更易读的方法来在 Python 中编写循环来遍历 Numpy 数组的每个元素?我想出了以下代码,但它看起来很麻烦并且可读性不太好:
import numpy as np
arr01 = np.random.randint(1,10,(3,3))
for i in range(0,(np.shape(arr01[0])[0]+1)):
for j in range(0,(np.shape(arr01[1])[0]+1)):
print (arr01[i,j])
Run Code Online (Sandbox Code Playgroud)
我可以让它更明确,例如:
import numpy as np
arr01 = np.random.randint(1,10,(3,3))
rows = np.shape(arr01[0])[0]
cols = np.shape(arr01[1])[0]
for i in range(0, (rows + 1)):
for j in range(0, (cols + 1)):
print (arr01[i,j])
Run Code Online (Sandbox Code Playgroud)
然而,与其他语言相比,这似乎仍然有点麻烦,即 VBA 中的等效代码可以读取(假设数组已经被填充):
dim i, j as integer
for i = lbound(arr01,1) to ubound(arr01,1)
for j = lbound(arr01,2) to ubound(arr01,2)
msgBox arr01(i, j)
next j
next i
Run Code Online (Sandbox Code Playgroud) 我正在尝试将 numpy 数组列表保存到磁盘,这样我就不必每次都生成它,因为这需要一段时间。该列表包含大约 230,000 个 numpy 数组,每个 numpy 数组的维度为 7xlength,其中每个数组的长度可以在 ~200-800 之间变化。
我尝试过 np.save 但收到一条错误消息“无法将输入数组从形状 (7,158) 广播到形状 (7)”列表中第一个数组的长度是 158,因此它在第一个列表项处失败。我也尝试过 np.savez 并首先使用 np.asarray(listname) 将数组列表转换为纯 numpy 数组,但我得到了相同的错误。
将此阵列列表保存到磁盘以便我可以按需加载和使用它的最佳方法是什么?
我有一个相对简单的问题,如果不使用循环就无法解决。我很难找出这个问题的正确标题。假设我们有两个 numpy 数组:
array_1 = np.array([[0, 1, 2],
[3, 3, 3],
[3, 3, 4],
[3, 6, 2]])
array_2 = np.array([[0, 0, 0],
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
[4, 4, 4],
[5, 5, 5],
[6, 6, 6]])
Run Code Online (Sandbox Code Playgroud)
array_1array_2表示我们想要的行的索引sum。例如, array4中的第 行result应包含与array_1 中的所有行array_2具有相同行索引的所有行的总和。3
在代码中更容易理解:
result = np.empty(array_2.shape)
for i in range(array_1.shape[0]):
for j in range(array_1.shape[1]):
index = array_1[i, j]
result[index] = result[index] + array_2[i]
Run Code Online (Sandbox Code Playgroud)
结果应该是:
[[ 0 0 …Run Code Online (Sandbox Code Playgroud) 我只是想了解 np.empty() ,我知道它创建了一个未初始化的数组,但我无法理解这意味着什么以及值来自哪里。欢迎任何帮助,提前致谢。
我是python的新手并且正在关注Python中的机器学习.我有一个例子
min(datMat[:,0])
Run Code Online (Sandbox Code Playgroud)
应该归还
matrix([[-5.379713]])
Run Code Online (Sandbox Code Playgroud)
但我得到的是
matrix([[<map object at 0x000002130BF3E240>]], dtype=object)
Run Code Online (Sandbox Code Playgroud)
当我在代码中包含它
rangeJ = float(max(dataSet[:,j]) - minJ)
Run Code Online (Sandbox Code Playgroud)
我得到错误"TypeError:不支持的操作数类型 - :'map'和'map'".
这是我用来生成dataMat的代码
def loadDataSet(fileName):
dataMat = []
fr = open(fileName)
for line in fr.readlines():
curLine = line.strip().split('\t')
fltLine = map(float,curLine)
dataMat.append(fltLine)
return dataMat
Run Code Online (Sandbox Code Playgroud)
我很难搞清楚这一点.任何帮助都会很棒!!
我的工作numpy array如下:
input_series = ['BUY' 'SELL' 'BUY' 'SELL' 'BUY' 'SELL' 'SELL' 'SELL' 'BUY' 'SELL' nan nan
nan nan nan nan nan nan nan]
Run Code Online (Sandbox Code Playgroud)
我正在搜索数组中的特定值,如果元素存在则删除
我这样做了如下:
delete_indices = list()
val = ['BUY','SELL','No','YES']
found_index = np.where(lowercase_series_nparray == val)
delete_indices.append(found_index)
Run Code Online (Sandbox Code Playgroud)
delete_indices 得到如下:
[(array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([], dtype=int64),), (array([0, 2, 4, 8], dtype=int64),), (array([1, 3, 5, 6, …Run Code Online (Sandbox Code Playgroud) 我读过 numpy 数组是可散列的,这意味着它是不可变的,但我可以更改它的值,那么可散列到底意味着什么?
c=pd.Series('a',index=range(6))
c
Out[276]:
0 a
1 a
2 a
3 a
4 a
5 a
dtype: object
Run Code Online (Sandbox Code Playgroud)
这不会给我错误,那么如果我尝试对 numpy 数组做同样的事情,它为什么会出错。
d=pd.Series(np.array(['a']),index=range(6))
Run Code Online (Sandbox Code Playgroud) 我已经将一些数据记录为npy文件。我尝试data[0]用以下代码显示图片(),以检查它是否有意义
import numpy as np
import cv2
train_data = np.load('c:/data/train_data.npy')
for data in train_data:
output = data[1]
# only take the height, width and channels of the 4 dimensional array
image = data[0][0, :, :, :]
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imshow('test', image)
print('output {}'.format(output))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
Run Code Online (Sandbox Code Playgroud)
但是,如果我显示的图像没有线条,image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)则图像似乎基于BGR。如果我将此行注释为代码,则图像将正确显示。
我的问题:这种观察是否暗示图像阵列已经是BGR格式?还是这意味着
cv2.imshow()默认情况下会将数组解释为BGR数组?
假设我有一个像这样的numpy数组:
arr = np.array([[1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4])
Run Code Online (Sandbox Code Playgroud)
我也有一个确定预期长度的列表:
lens = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
是否有一种优雅而Pythonic的方式来返回一个新数组,并使用该lens变量选择了每个对应的元素?
输出应为:
[[1], [2,2],[3,3,3], [4,4,4,4]]
Run Code Online (Sandbox Code Playgroud) numpy-ndarray ×10
python ×10
numpy ×9
pandas ×2
python-3.x ×2
immutability ×1
list ×1
opencv ×1
save ×1