>- **🍨 本文为[🔗365天深度学习训练营](https://mp.weixin.qq.com/s/rbOOmire8OocQ90QM78DRA) 中的学习记录博客**
>- **🍖 原作者:[K同学啊 | 接辅导、项目定制](https://mtyjkh.blog.csdn.net/)**
我的环境:
- 系统环境:WSL2+Ubuntu22.04
- 语言环境:Python3.8.18
- 编译器:vscode+jupyter notebook
- 深度学习环境:TensorFlow2.10.0
本期的代码用的是多层感知机:
model = Sequential()
model.add(Dense(units=24, activation='tanh'))
model.add(Dense(units=18, activation='tanh'))
model.add(Dense(units=23, activation='tanh'))
model.add(Dropout(0.5))
model.add(Dense(units=12, activation='tanh'))
model.add(Dropout(0.2))
model.add(Dense(units=1, activation='sigmoid'))
这个模型总共设置了四个全连接隐含层,两个 Dropout 层,以及一个输出层。
在隐含层中使用了双曲正切(tanh)作为激活函数,引入网络的非线性特性。
本次任务就是判断第二天是否降雨,所以在输出层使用了“S型函数(sigmoid) 作为激活问题,适用于二分类问题,输出可以被解释为概率。
本期任务很简单,主要是代码非常的长,全部敲对需要很多时间
如果出现了报错,请优先检查是不是代码敲错了
相关系数的计算部分有个报错:
plt.figure(figsize=(15,13))
ax = sns.heatmap(data.corr(), square=True, annot=True, fmt='.2f')
ax.set_xticklabels(ax.get_xticklabels(),rotation=90)
plt.show
{
"name": "ValueError",
"message": "could not convert string to float: 'Albury'",
"stack": "---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[14], line 2
1 plt.figure(figsize=(15,13))
----> 2 ax = sns.heatmap(data.corr(), square=True, annot=True, fmt='.2f')
3 ax.set_xticklabels(ax.get_xticklabels(),rotation=90)
4 plt.show
File ~/anaconda3/envs/tensorflow/lib/python3.8/site-packages/pandas/core/frame.py:10054, in DataFrame.corr(self, method, min_periods, numeric_only)
10052 cols = data.columns
10053 idx = cols.copy()
> 10054 mat = data.to_numpy(dtype=float, na_value=np.nan, copy=False)
10056 if method == \"pearson\":
10057 correl = libalgos.nancorr(mat, minp=min_periods)
File ~/anaconda3/envs/tensorflow/lib/python3.8/site-packages/pandas/core/frame.py:1838, in DataFrame.to_numpy(self, dtype, copy, na_value)
1836 if dtype is not None:
1837 dtype = np.dtype(dtype)
-> 1838 result = self._mgr.as_array(dtype=dtype, copy=copy, na_value=na_value)
1839 if result.dtype is not dtype:
1840 result = np.array(result, dtype=dtype, copy=False)
File ~/anaconda3/envs/tensorflow/lib/python3.8/site-packages/pandas/core/internals/managers.py:1732, in BlockManager.as_array(self, dtype, copy, na_value)
1730 arr.flags.writeable = False
1731 else:
-> 1732 arr = self._interleave(dtype=dtype, na_value=na_value)
1733 # The underlying data was copied within _interleave, so no need
1734 # to further copy if copy=True or setting na_value
1736 if na_value is not lib.no_default:
File ~/anaconda3/envs/tensorflow/lib/python3.8/site-packages/pandas/core/internals/managers.py:1794, in BlockManager._interleave(self, dtype, na_value)
1792 else:
1793 arr = blk.get_values(dtype)
-> 1794 result[rl.indexer] = arr
1795 itemmask[rl.indexer] = 1
1797 if not itemmask.all():
ValueError: could not convert string to float: 'Albury'"
}
翻译一下这个报错:在执行 data.corr()
操作时,有一个包含字符串的列'Albury'
。
相关性计算通常要求输入数值数据进行计算。而'Albury'
是一个字符串,字符和字符本质上没有什么相关性可言,程序无法将他们转换成浮点数进行计算,从而导致了 ValueError。
检查数据集:
发现这些列他们存储的都是字符串而不是数字,所以会报错
解决方法一般有两种:
1.直接排除非数值列,只考虑数值列进行相关性计算。
2.将字符串以编码或者其他什么形式,转换为数值型进行相关性计算。
我的做法是排除非数值列,使用 select_dtypes
方法选择只包含数值型的列。代码如下:
numeric_data = data.select_dtypes(include='number')
correlation_matrix = numeric_data.corr()
plt.figure(figsize=(15, 13))
ax = sns.heatmap(correlation_matrix, square=True, annot=True, fmt='.2f')
ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
plt.show()
输出为: