考虑这个简单的例子:
#setup figure
import matplotlib.pyplot as plt
import pylab
import math
import numpy as np
#output control
fig_width_pt = 345.0 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (math.sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
params = {'backend': 'ps',
'axes.labelsize': 10,
'text.fontsize': 10,
'legend.fontsize': 8,
'xtick.labelsize': 8,
'ytick.labelsize': 8,
'text.usetex': True,
'figure.figsize': fig_size,
'figure.dpi': 1000,
'savefig.dpi': 1000} …Run Code Online (Sandbox Code Playgroud) 是否可以根据take功能的工作原理分配一个numpy数组?
例如,如果我有一个数组a,一个索引列表inds和一个所需的轴,我可以使用如下:
import numpy as np
a = np.arange(12).reshape((3, -1))
inds = np.array([1, 2])
print(np.take(a, inds, axis=1))
[[ 1 2]
[ 5 6]
[ 9 10]]
Run Code Online (Sandbox Code Playgroud)
当所需的索引/轴在运行时可能发生变化时,这非常有用.
但是,numpy不允许你这样做:
np.take(a, inds, axis=1) = 0
print(a)
Run Code Online (Sandbox Code Playgroud)
看起来这个通道有一些有限的(1-D)支持numpy.put,但我想知道是否有更清洁的方法来做到这一点?