具有多个输入和输出的读入CSVfile

2024-06-16 13:01:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下表,想找出输入和输出之间的关系,以便作出预测

稍后我要输入加热器功率、电压、加热器效率和加热器质量的输入值,并生成输出预测

在表中您可以看到我有4个输入参数和3个输出参数

table

我已经创建了一个代码。输入和输出的值手动写入数组

导入

import tensorflow as tf
import numpy as np

设置培训数据

inputMatrix = np.array([(100,230,0.95,100),
                        (200,245,0.99,121),
                        ( 40,250,0.91,123)],dtype=float)
outputMatrix = np.array([(120, 5,120),
                         (123,24,100),
                         (154, 3,121)],dtype=float)
for i,c in enumerate(inputMatrix):
print("{}Input Matrix={}Output Matrix".format(c,outputMatrix[i]))

创建模型

l0 = tf.keras.layers.Dense(units = 4, input_shape = [4])
l1 = tf.keras.layers.Dense(units = 64)
l2 = tf.keras.layers.Dense(units = 128)
l3 = tf.keras.layers.Dense(units = 3)

model = tf.keras.Sequential([l0,l1,l2,l3])

编译模型

model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))

训练模型

history = model.fit(inputMatrix,outputMatrix,epochs=500,verbose=False)
print("Finished training the model!")

显示培训统计信息

import matplotlib.pyplot as plt
plt.xlabel('Epoch Number')
plt.ylabel('Loss Magnitude')
plt.plot(history.history['loss'])

使用模型预测值

print(model.predict(np.array([120,260,0.98,110]).reshape(1,4)))

我现在想从csv文件中自动读取表。数据应按输入、输出和读入分开

我该怎么做?在这里使用数组有意义吗?还是有更好的可能性

我怀疑我的方法是否基本正确。我的密码似乎很短。还是我必须为我的问题选择不同的方法


Tags: 模型importmodellayerstfasnpplt