Python 3.x 如何对超出范围的串联值使用numpy Digital?
我试图通过Python 3.x 如何对超出范围的串联值使用numpy Digital?,python-3.x,numpy,indexing,vectorization,binary-search,Python 3.x,Numpy,Indexing,Vectorization,Binary Search,我试图通过numpy模块使用数字化,以帮助维护成绩册。其思想是输入学生在课堂上获得的总分,从而输出相应的字母分数。我的尝试如下: import numpy as np from collections import OrderedDict ## letter grades and points at cusps of letter grades letter_grades = np.array(['F', 'D-', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B',
numpy
模块使用数字化
,以帮助维护成绩册。其思想是输入学生在课堂上获得的总分,从而输出相应的字母分数。我的尝试如下:
import numpy as np
from collections import OrderedDict
## letter grades and points at cusps of letter grades
letter_grades = np.array(['F', 'D-', 'D', 'D+', 'C-', 'C', 'C+', 'B-', 'B', 'B+', 'A-', 'A'])
point_edges = np.concatenate(np.linspace(101, 153, len(letter_grades)), 10**3)
point_edges[0] = 0
## each letter grade corresponds to point values within the two corresponding point edges
edge_pairs = np.array([('{} - {}'.format(point_edges[idx-1], point_edges[idx])) for idx in range(1, len(point_edges))])
criteria = OrderedDict(zip(letter_grades, edge_pairs))
# print(criteria)
## sample data (the top one works, the one below throws an error)
# point_scores = (0, 100, 100.9, 101, 101.1, 136)
point_scores = (0, 100, 100.9, 101, 101.1, 136, 146, 150, 152, 153, 154)
## use numpy to get result
indices = np.digitize(point_scores, point_edges)
final_grades = letter_grades[indices]
for point, grade in zip(point_scores, final_grades):
print("\n .. {} POINTS :: {}\n".format(point, grade))
运行上述代码会输出以下错误:
IndexError: index 12 is out of bounds for axis 1 with size 12
我将
1000
作为点边的最后一个元素
,这样任何大于153的输入值都将输出'A'
(如打印(标准)中所示)
语句已在上面注释掉。但是,该算法仅适用于严格小于153的输入值。为什么会发生这种情况,以及如何修复它?np。digizize
的编号与np不同。直方图
用于指示边界以外的值:
从:
如果x中的值超出了存储单元的边界,则0或len(存储单元)为
视情况返回
您案例中的索引12表示某个值高于给定的限制。如果您想要最后一个箱子,则表示您案例中的索引11。
索引为0的第一个箱子是低于下边界的值,索引1是第一个有效箱子