我有一个json格式列表.json中有3000多个记录。我正在尝试在python中使用keras和tensorflowjs训练我的模型,但我需要遍历此列表数据并逐个获取对象数据。我需要知道如何使用python遍历此数据,以便我的模型开始读取值。我只需要遍历特定变量中的numpy数组。这样每个变量中的数据在每次迭代中都可以是唯一的。我对python的经验不好,所以如果您可以编辑我的代码,那将有很大帮助
[
{
"": 0,
"attitude.roll": 1.528132,
"attitude.pitch": -0.733896,
"attitude.yaw": 0.696372,
"gravity.x": 0.741895,
"gravity.y": 0.669768,
"gravity.z": -0.031672000000000006,
"rotationRate.x": 0.316738,
"rotationRate.y": 0.77818,
"rotationRate.z": 1.0827639999999998,
"userAcceleration.x": 0.294894,
"userAcceleration.y": -0.18449300000000002,
"userAcceleration.z": 0.377542
},
{
"": 1,
"attitude.roll": 1.527992,
"attitude.pitch": -0.716987,
"attitude.yaw": 0.677762,
"gravity.x": 0.753099,
"gravity.y": 0.657116,
"gravity.z": -0.032255,
"rotationRate.x": 0.842032,
"rotationRate.y": 0.424446,
"rotationRate.z": 0.643574,
"userAcceleration.x": 0.21940500000000002,
"userAcceleration.y": 0.035845999999999996,
"userAcceleration.z": 0.11486600000000001
}
]
这是我的Python代码
import json
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout
import tensorflowjs as tfjs
with open("C:\\Users\\TechProBox\\Desktop\\Model.json") as f:
data = json.load(f)
x1 = np.array(data['attiude.roll'])
y1 = np.array(data['attitude.pitch'])
z1 = np.array(data['attitude.yaw'])
x2 = np.array(data['gravity.x'])
y2 = np.array(data['gravity.y'])
z2 = np.array(data['gravity.z'])
x3 = np.array(data['rotationRate.x'])
y3 = np.array(data['rotationRate.y'])
z3 = np.array(data['rotationRate.z'])
x4 = np.array(data['userAcceleration.x'])
y4 = np.array(data['userAcceleration.y'])
z4 = np.array(data['userAcceleration.z'])
x1_train = x1[:-10000]
y1_train = y1[:-10000]
z1_train = z1[:-10000]
x2_train = x2[:-10000]
y2_train = y2[:-10000]
z2_train = z2[:-10000]
x3_train = x3[:-10000]
y3_train = y3[:-10000]
z3_train = z3[:-10000]
x4_train = x4[:-10000]
y4_train = y4[:-10000]
z4_train = z4[:-10000]
x1_test = x1[:-10000]
y1_test = y1[:-10000]
z1_test = z1[:-10000]
x2_test = x2[:-10000]
y2_test = y2[:-10000]
z2_test = z2[:-10000]
x3_test = x3[:-10000]
y3_test = y3[:-10000]
z3_test = z3[:-10000]
x4_test = x4[:-10000]
y4_test = y4[:-10000]
z4_test = z4[:-10000]
model = Sequential()
model.add(Dense(64, activation='relu', input_dim=6))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
adam = keras.optimizers.Adam(lr=0.0001)
model.compile(loss='categorical_crossentropy',
optimizer=adam,
metrics=['accuracy'])
model.fit(x1_train, y1_train, z1_train, x2_train, y2_train, z2_train,x3_train, y3_train, z3_train,
x4_train, y4_train, z4_train,
epochs=14,
batch_size=128)
score = model.evaluate(x1_test, y2_test, z3_test, x2_test, y2_test, z2_test, x3_test, y3_test, z3_test,
x4_test, y4_test, z4_test, batch_size=128)
print(score)
model.save("Keras-64*2-10epoch")
tfjs.converters.save_keras_model(model,"tfjsv3")