我正在尝试使用神经网络来预测房价。这是数据集顶部的样子:
Price Beds SqFt Built Garage FullBaths HalfBaths LotSqFt
485000 3 2336 2004 2 2.0 1.0 2178.0
430000 4 2106 2005 2 2.0 1.0 2178.0
445000 3 1410 1999 1 2.0 0.0 3049.0
...
我正在使用 ReLU 激活函数。当我尝试根据我的测试数据评估我的模型时,我得到了这个TypeError: unsupported operand type(s) for +=: 'Dense' and 'str'
。
我查看了原始数据框中的列类型,一切看起来都很好。
print(df.dtypes)
## Output
#Price int64
#Beds int64
#SqFt int64
#Built int64
#Garage int64
#FullBaths float64
#HalfBaths float64
#LotSqFt float64
#dtype: object
我不确定我是否在我的神经网络中搞砸了一些东西来导致这个错误。任何帮助表示赞赏!这是我的代码供参考。
- 为网络准备数据
dataset = df.values
X = dataset[:, 1:8]
Y = dataset[:,0]
## Normalize X-Values
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
X_scale
##Partition Data
from sklearn.model_selection import train_test_split
X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)
X_val, X_test, Y_val, Y_test = train_test_split(X_val_and_test, Y_val_and_test, test_size=0.5)
print(X_train.shape, X_val.shape, X_test.shape, Y_train.shape, Y_val.shape, Y_test.shape)
- 开始模型构建
from keras.models import Sequential
from keras.layers import Dense
model = Sequential(
Dense(32, activation='relu', input_shape=(7,)),
Dense(1, activation='linear'))
model.compile(optimizer='sgd',
loss='mse',
metrics=['mean_squared_error'])
model.evaluate(X_test, Y_test)[1] ##Type Error is here!