I am trying to use a neural network to predict the price of houses. Here is what the top of the dataset looks like:
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
...
I am using the ReLU activation function. When I try to evaluate my model on my test data, I get this TypeError: unsupported operand type(s) for +=: 'Dense' and 'str'
.
I looked at the types of the columns from my original dataframe, and everything looks fine.
print(df.dtypes)
## Output
#Price int64
#Beds int64
#SqFt int64
#Built int64
#Garage int64
#FullBaths float64
#HalfBaths float64
#LotSqFt float64
#dtype: object
I'm not sure if I am messing something up in my neural network to cause this error. Any help is appreciated! Here is my code for reference.
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!
I tried to recreate a minimal (not) working example of your code. It seems that you just forgot a pair of square brackets in the Sequential()
model definition.
import pandas as pd
from keras import backend as K
# Tried to recreate your dataset
df = pd.DataFrame({'Price': [485000, 430000, 445000, 485000, 430000, 445000, 485000, 430000, 445000, 485000, 430000, 445000],
'Beds': [3, 4, 3, 3, 4, 3, 3, 4, 3, 3, 4, 3],
'SqFt': [2336, 2106, 1410, 2336, 2106, 1410, 2336, 2106, 1410, 2336, 2106, 1410],
'Built': [2004, 2005, 1999, 2004, 2005, 1999, 2004, 2005, 1999, 2004, 2005, 1999],
'Garage': [2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1],
'FullBaths': [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
'HalfBaths': [1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0],
'LotSqFt': [2178.0, 2178.0, 3049.0, 2178.0, 2178.0, 3049.0, 2178.0, 2178.0, 3049.0, 2178.0, 2178.0, 3049.0]})
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)
##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')]) # Layers are enclosed in square brackets
model.compile(optimizer='sgd',
loss='mse',
metrics=['mean_squared_error'])
model.fit(X_train, Y_train, verbose=1, validation_data=(X_val, Y_val))
model.evaluate(X_test, Y_test) ##Type Error is here!
Also, I would perform training and evaluation (by calling model.fit(X_train, Y_train, verbose=1, validation_data=(X_val, Y_val))
) on the model before testing it. Otherwise, you are evaluating the test sets on a neural network with randomly initialised weights.