python - ValueError:检查目标时出错:预期dense_3的形状为(1,)但得到的数组形状为(2,)
问题描述
当我使用澳大利亚数据集编写降雨预测代码时,我在拟合 ann 模型并运行 10 的纪元值期间遇到错误。我正在使用 numpy、pandas、matplotlib、seaborn 等库作为 importing 。对于模型的运行,我使用 Keras 进行密集和顺序搜索。我还使用标准标量来规范化 x 的值。我在这一行收到错误 - ann.fit(x_train,y_train, batch_size = 10, nb_epoch = 10, verbose = 1) 以下是我的错误 - ValueError: Error when checks target: expected dense_3 to have shape (1,)但是得到了形状为 (2,) 的数组下面是我的代码-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('weatherAUS.csv')
df.head()
df.columns
plt.figure(figsize=(14,6))
df['MaxTemp'].plot()
plt.figure(figsize=(12,7))
sns.boxplot(x='RainToday',y='MaxTemp',data = df,palette ='winter')
df['Rainfall'].plot(kind= 'hist', bins=30, color='orange', figsize= (16,7))
from sklearn.model_selection import train_test_split
df.info()
df.dropna(inplace= True)
X=df[['Rainfall','MaxTemp','MinTemp']]
y=df[['RainToday','RainTomorrow']]
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2, random_state= 41)
#normalization to X values
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
xtrain = scaler.fit_transform(x_train)
xtest = scaler.transform(x_test)
import keras
from keras.layers import Dense
from keras.models import Sequential
ann = Sequential()
ann.add(Dense(units=32, init='uniform', activation='relu', input_dim = 3))
ann.add(Dense(units=16, init='uniform', activation='relu'))
ann.add(Dense(units=1, init='uniform', activation='sigmoid'))
ann.compile(optimizer ='adam', loss= 'mean_squared_error', metrics= ['accuracy'])
ann.fit(x_train,y_train, batch_size = 10, nb_epoch = 10, verbose = 1)
Y_pred = ann.predict(x_test)
Y_pred = [1 if y>=0.5 else 0 for y in Y_pred]
print(Y_pred)
解决方案
你有代码
y=df[['RainToday','RainTomorrow']]
y 应该是单列。你试图预测明天是否会下雨。RainToday 是 X_train 中应该包含的功能。我建议您阅读使用此数据集的优秀教程。它位于此处。
推荐阅读
- linux - 使用 find 从远程同步文件
- javascript - Firebase 什么时候开始从数据库中提取数据?
- angular5 - Angular 5 - 检测到循环依赖
- google-analytics - 发生虚拟页面查看时触发标签
- java - 替换不在给定正则表达式java中的字符串
- c# - EPPlus 更改轴线的样式
- javascript - 按下 2 个键,松开 1,再次产生另一个键的 keydown
- vba - 将 Excel 选项卡导出到单独的 PDF 文件 - 跳过第一个 3 个选项卡
- excel - vba代码比较两个范围
- android - 从当前活动的事件更改以前活动的 UI