训练集给出如下数据:
测试集提供其中的部分列:
要求预测以下列的数据:
['Tdewpoint', 'Visibility', 'Windspeed', 'RH_out', 'Press_mm_hg', 'RH_9', 'T_out', 'RH_4']
使用神经网络进行预测:
import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from keras.models import Model from keras.layers import Input, Dense # 读取训练集数据 train_data = pd.read_csv('train_dataset.csv') li=train_data.columns.to_list()[2::] #print(li) goal=['Tdewpoint', 'Visibility', 'Windspeed', 'RH_out', 'Press_mm_hg', 'RH_9', 'T_out', 'RH_4'] feature=list(set(li)-set(goal)) #print(feature) #exit() # 选择特征和目标变量 X = train_data[feature] y = train_data[goal] # 划分训练集和验证集 X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42) # 构建神经网络模型 input_layer = Input(shape=(len(feature),)) hidden_layer = Dense(10, activation='relu')(input_layer) output_layer1 = Dense(1, name='output1')(hidden_layer) output_layer2 = Dense(1, name='output2')(hidden_layer) output_layer3 = Dense(1, name='output3')(hidden_layer) output_layer4 = Dense(1, name='output4')(hidden_layer) output_layer5 = Dense(1, name='output5')(hidden_layer) output_layer6 = Dense(1, name='output6')(hidden_layer) output_layer7 = Dense(1, name='output7')(hidden_layer) output_layer8 = Dense(1, name='output8')(hidden_layer) model = Model(inputs=input_layer, outputs=[output_layer1, output_layer2, output_layer3, output_layer4, output_layer5, output_layer6, output_layer7, output_layer8]) model.compile(loss='mse', optimizer='adam', metrics=['mse']) # 训练模型 model.fit(X_train, [y_train[c] for c in y_train.columns], epochs=1000, batch_size=32, verbose=1, validation_data=(X_val, [y_val[c] for c in y_val.columns])) # 读取测试集数据 test_data = pd.read_csv('test_dataset.csv') # 预测并保存结果 X_test = test_data[feature] y_pred = model.predict(X_test) submission = pd.DataFrame({'Tdewpoint': y_pred[0].flatten(), 'Visibility': y_pred[1].flatten(), 'Windspeed': y_pred[2].flatten(), 'RH_out': y_pred[3].flatten(), 'Press_mm_hg': y_pred[4].flatten(), 'RH_9': y_pred[5].flatten(), 'T_out': y_pred[6].flatten(), 'RH_4': y_pred[7].flatten()}) submission.to_csv('test_result.csv', index=False)
标签:layer,Dense,神经网络,train,output,hidden,RH From: https://www.cnblogs.com/datielaoyu/p/17436634.html