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的情况,当然不限于该函数方法读取文件引起该报错(未实践)。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>