学习《python深度学习(NDLewis)》第六章,代码出了一个bug,原代码为:
# 使用相关性函数,检查预测值和实际值之间的相关性
from scipy.stats.stats import pearsonr
correl = pearsonr(pred3_test,y_test)
print("Test correlation = ",correl[0])
print("Test correlation ^ 2 = ",correl[0] * correl[0])
其中计算 correl时,报了一个错误:
File "D:\PyCharm\PyCharm 2019.2.3\workspace\Deepling\BostonHousePricePrediction.py", line 98, in <module>
correl = pearsonr(pred3_test,y_test)
File "D:\Anaconda\lib\site-packages\scipy\stats\stats.py", line 3410, in pearsonr
xmean = x.mean(dtype=dtype)
File "D:\Anaconda\lib\site-packages\numpy\core\_methods.py", line 75, in _mean
ret = umr_sum(arr, axis, dtype, out, keepdims)
TypeError: No loop matching the specified signature and casting
was found for ufunc add
大概是说,在计算相关性时的x参数(这里指的是 pred3_test)的类型不匹配,搜了一下,大概其他人的解决办法是,转换一下类型:
x = x.astype('float64')
或者
x = np.arrary(x,dtype= 'float64')
结果bug还是没有解决,然后就根据提示出错的文件,去看一下源码:stats.py
# dtype is the data type for the calculations. This expression ensures
# that the data type is at least 64 bit floating point. It might have
# more precision if the input is, for example, np.longdouble.
dtype = type(1.0 + x[0] + y[0])
if n == 2:
return dtype(np.sign(x[1] - x[0])*np.sign(y[1] - y[0])), 1.0
xmean = x.mean(dtype=dtype)
ymean = y.mean(dtype=dtype)
# By using `astype(dtype)`, we ensure that the intermediate calculations
# use at least 64 bit floating point.
xm = x.astype(dtype) - xmean
ym = y.astype(dtype) - ymean
这里计算 x.mean时用的dtype 就在上面几行定义了。回到我自己的程序,输出这些类型看一下:
print(type(pred3_test))
print(type(pred3_test[0]))
发现两个都是:
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
应该是数据的维度有冗余,里面的放float64类型的数组外面多包裹了一个维度。去除多余的维度就可以了:
pred3_test = np.squeeze(pred3_test)
y_test = np.squeeze(y_test)
执行结果:
Test correlation = 0.8501812635529712
Test correlation ^ 2 = 0.7228081808965267
|