说我有这个数据数组
data = np.asarray([[1, 2, 3, 4], ['a', 'b', 'c', 'd'], ['A', 'B', 'C', 'D'], ['x', 'y', 'z', 'zz']])
和这些索引,每个“对”对应于数据矩阵中的一个单元格
indices = np.asarray([[0, 0], [3, 0], [2, 2], [3, 2]])
现在,我想从指定的单元格中检索数据。我可以通过以下方式做到这一点:
searched_data = []
for x, y in coords:
searched_data.append(data[y][x])
是否存在更多的Python风格或更多的numpy-ish变体,可以通过花式数组索引或其他方式在一行中完成此操作?
我尝试过(受this post的启发):
x_indexed1 = data[indices[:, 1]][:,[indices[:, 0]]]
但这给了我
[[['1' '4' '3' '4']]
[['1' '4' '3' '4']]
[['A' 'D' 'C' 'D']]
[['A' 'D' 'C' 'D']]]
还有这个
x_indexed = data[np.ix_(indices[:, 1],indices[:, 0])]
给出
[['1' '4' '3' '4']
['1' '4' '3' '4']
['A' 'D' 'C' 'D']
['A' 'D' 'C' 'D']]
答案 0 :(得分:2)
您已经接近了,但是当您想像这样的具有对齐索引的library(highcharter)
data = data.frame(date = seq(as.Date("2019-01-01"), as.Date("2020-01-01"), by = "day"))
data$y = 1
data$y2 = 2
# base
highchart() %>%
hc_yAxis_multiples(
list(top = "0%", height = "47%", title = list(text = "y0"), opposite=FALSE, labels = list(format = "£{value}"), min=0),
list(top = "53%", height = "47%", title = list(text = "y1"), opposite=FALSE, labels = list(format = "£{value}"), offset=0, min=0)) %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) %>%
hc_add_theme(hc_theme_flat(chart = list(backgroundColor = "#FFF"))) %>%
hc_add_series(data, "area", stacking="normal", yAxis = 0, hcaes(x=date, y=y)) %>%
hc_add_series(data, "area", stacking="normal", yAxis = 1, hcaes(x=date, y=y2))
# type = stock
highchart(type="stock") %>%
hc_yAxis_multiples(
list(top = "0%", height = "47%", title = list(text = "y0"), opposite=FALSE, labels = list(format = "£{value}"), min=0),
list(top = "53%", height = "47%", title = list(text = "y1"), opposite=FALSE, labels = list(format = "£{value}"), offset=0, min=0)) %>%
hc_xAxis(type = "datetime", dateTimeLabelFormats = list(day = '%d of %b')) %>%
hc_add_theme(hc_theme_flat(chart = list(backgroundColor = "#FFF"))) %>%
hc_add_series(data, "area", stacking="normal", yAxis = 0, hcaes(x=date, y=y)) %>%
hc_add_series(data, "area", stacking="normal", yAxis = 1, hcaes(x=date, y=y2))
索引时,请勿使用numpy.ndarray
。使用 tuple 进行多维索引:
[][]
为了更加清楚:
>>> data[indices[:, 1], indices[:,0]]
array(['1', '4', 'C', 'D'], dtype='<U21')
您的第一次尝试是这样的:
>>> ys = indices[:, 1]
>>> xs = indices[:, 0]
>>> data[ys, xs]
array(['1', '4', 'C', 'D'], dtype='<U21')
因此,要进行分解,“部分索引”将假设您在剩下的维度中是一个完整切片>>> data[ys][:,[xs]]
array([[['1', '4', '3', '4']],
[['1', '4', '3', '4']],
[['A', 'D', 'C', 'D']],
[['A', 'D', 'C', 'D']]], dtype='<U21')
,因此对于您:
来说,将选择以下行:
data[ys, :]
然后这就是您用>>> data[ys]
array([['1', '2', '3', '4'],
['1', '2', '3', '4'],
['A', 'B', 'C', 'D'],
['A', 'B', 'C', 'D']], dtype='<U21')
进行索引的过程,该过程基本上选择所有行和xs的列,这些列和您包装在列表中的列基本上不压缩维度:
[:, [xs]]