python报错ValueError: zero-size array to reduction operation maximum which has no identity,情况之一分析与解决

在boston房价数据预测练习项目中,发现报错如下:
ValueError: zero-size array to reduction operation maximum which has no identity
此报错所对应行为

maximums, minimums, avgs = training_data.max(axis=0), training_data.min(axis=0), \
                                training_data.sum(axis=0) / training_data.shape[0]

于是网上查找对应错误解决方法,有的博主的numpy数组处array.max(axis=0)、array.min(axis=0),的确有数组为0的情况,采纳其建议加入assert array.size != 0或者if判断if array.size != 0: 均未解决报错问题(注:array应替换为你自己文件中的数组名称如training_data);于是溯源打印数组数据,直至查找到读取文件处的数据也是为0的情况,发现data = np.fromfile(datafile, sep=’ ‘, dtype=np.float32)虽能在读取boston_housing_data.csv时不报错,但读取不了其中的数据信息,具体原因未深入分析。将boston_housing_data.csv文件替换为housing.data文件,np.fromfile成功读取文件中数据,数组有值,报错问题解决。
所以报错ValueError: zero-size array to reduction operation maximum which has no identity增加一条原因为np.fromfile读取文件格式的影响导致数组为0的情况,当然不限于该函数方法读取文件引起该报错(未实践)。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年11月8日
下一篇 2023年11月8日

相关推荐