super_weight
2021-09-18 15:30
采纳率: 0%
浏览 7

回归问题的例子 住宅价格预测中遇到的 TypeError: 'int' object is not iterable 问题

数据集是Bonston Housing
代码是

from keras.datasets import boston_housing

(train_data,train_targets),(test_data,test_targets) = boston_housing.load_data()
print(train_data.shape)
print(test_data.shape)
print(train_targets)

mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std

test_data -= mean
test_data /= std

from keras import models
from keras import layers

def build_model():
    model = models.Sequential()
    model.add(layers.Dense(64,activation = 'relu',input_shape = (train_data.shape[1])))
    model.add(layers.Dense(64,activation = 'relu'))
    model.add(layers.Dense(1))
    model.compile(optimizer = 'rmsprop',loss = 'mse',metrics = ['mae'])
    return model

import numpy as np
k=4
num_val_samples = len(train_data)//k
num_epochs = 500
all_mae_histories=[]
for i in range(k):
    print('processing fold #',i)
    val_data = train_data[i * num_val_samples: (i + 1) * num_val_samples]
    val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]

    partial_train_data = np.concatenate([train_data[:i * num_val_samples],train_data[(i + 1) * num_val_samples:]],axis = 0)
    partial_train_targets = np.concatenate([train_targets[:i * num_val_samples],train_targets[(i + 1) * num_val_samples:]],axis = 0)
    model = build_model()
    history = model.fit(partial_train_data,partial_train_targets,validation_data=(val_data,val_targets),epochs=num_epochs,batch_size=1,verbose=0)
    mae_history = history.history['val_mean_absolute_error']
    all_mae_histories.append(mae_history)

在运行这段代码时出现下列问题

img

请问该如何解决这个问题

  • 收藏

相关推荐 更多相似问题