本文主要是介绍完美解决ImportError: cannot import name '_validate_lengths'报错问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
欢迎关注WX公众号:【程序员管小亮】
最近在运行代码的时候出现了错误——ImportError: cannot import name '_validate_lengths'
经过查询和尝试,发现下面的这个方法可以完美运行。
找到:C:\Users\Administrat\Anaconda3\Lib\site-packages\numpy\lib\arraypad .py,打开.py文件,找到第954行,添加下面两个函数保存,重新加载即可消除错误,亲测有效。
def _normalize_shape(ndarray, shape, cast_to_int=True):"""Private function which does some checks and normalizes the possiblymuch simpler representations of ‘pad_width‘, ‘stat_length‘,‘constant_values‘, ‘end_values‘.Parameters----------narray : ndarrayInput ndarrayshape : {sequence, array_like, float, int}, optionalThe width of padding (pad_width), the number of elements on theedge of the narray used for statistics (stat_length), the constantvalue(s) to use when filling padded regions (constant_values), or theendpoint target(s) for linear ramps (end_values).((before_1, after_1), ... (before_N, after_N)) unique number ofelements for each axis where `N` is rank of `narray`.((before, after),) yields same before and after constants for eachaxis.(constant,) or val is a shortcut for before = after = constant forall axes.cast_to_int : bool, optionalControls if values in ``shape`` will be rounded and cast to intbefore being returned.Returns-------normalized_shape : tuple of tuplesval => ((val, val), (val, val), ...)[[val1, val2], [val3, val4], ...] => ((val1, val2), (val3, val4), ...)((val1, val2), (val3, val4), ...) => no change[[val1, val2], ] => ((val1, val2), (val1, val2), ...)((val1, val2), ) => ((val1, val2), (val1, val2), ...)[[val , ], ] => ((val, val), (val, val), ...)((val , ), ) => ((val, val), (val, val), ...)"""ndims = ndarray.ndim# Shortcut shape=Noneif shape is None:return ((None, None), ) * ndims# Convert any input `info` to a NumPy arrayshape_arr = np.asarray(shape)try:shape_arr = np.broadcast_to(shape_arr, (ndims, 2))except ValueError:fmt = "Unable to create correctly shaped tuple from %s"raise ValueError(fmt % (shape,))# Cast if necessaryif cast_to_int is True:shape_arr = np.round(shape_arr).astype(int)# Convert list of lists to tuple of tuplesreturn tuple(tuple(axis) for axis in shape_arr.tolist())def _validate_lengths(narray, number_elements):"""Private function which does some checks and reformats pad_width andstat_length using _normalize_shape.Parameters----------narray : ndarrayInput ndarraynumber_elements : {sequence, int}, optionalThe width of padding (pad_width) or the number of elements on the edgeof the narray used for statistics (stat_length).((before_1, after_1), ... (before_N, after_N)) unique number ofelements for each axis.((before, after),) yields same before and after constants for eachaxis.(constant,) or int is a shortcut for before = after = constant for allaxes.Returns-------_validate_lengths : tuple of tuplesint => ((int, int), (int, int), ...)[[int1, int2], [int3, int4], ...] => ((int1, int2), (int3, int4), ...)((int1, int2), (int3, int4), ...) => no change[[int1, int2], ] => ((int1, int2), (int1, int2), ...)((int1, int2), ) => ((int1, int2), (int1, int2), ...)[[int , ], ] => ((int, int), (int, int), ...)((int , ), ) => ((int, int), (int, int), ...)"""normshp = _normalize_shape(narray, number_elements)for i in normshp:chk = [1 if x is None else x for x in i]chk = [1 if x >= 0 else -1 for x in chk]if (chk[0] < 0) or (chk[1] < 0):fmt = "%s cannot contain negative values."raise ValueError(fmt % (number_elements,))return normshp
###############################################################################
# Public functions
python课程推荐。
这篇关于完美解决ImportError: cannot import name '_validate_lengths'报错问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!