Problems with numpy in Tensorflow

Question:

from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np

data = keras.datasets.boston_housing

(x_train, x_test), (y_train, y_test) = data.load_data()

model = keras.Sequential([
    keras.layers.InputLayer(13),
    keras.layers.Dense(3, activation="relu"),
    keras.layers.Dense(1, activation="sigmoid")
])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics="accuracy")

model.fit(x_train, y_train, epochs=10)

predict = model.predict(x_test)

for i in range(10):
    plt.grid(False)
    plt.imshow(x_test[i], cmap=plt.cm.binary)
    plt.suptitle("Actual: " + y_test[i])
    plt.title("Prediction: " + np.argmax(predict[i]))
    plt.show()`

That is my code and I need help.

I expect the normal thing that some graphs show up and it says tht everything has worked finde. But it does not. Error code:

Traceback (most recent call last):
  File "C:UsersnamePycharmProjectsNeural networkfirst_self_approach.py", line 21, in <module>
    model.fit(x_train, y_train, epochs=10)

  File "C:UsersnameAppDataLocalProgramsPythonPython310libsite-packageskerasutilstraceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None

  File "C:UsersnameAppDataLocalProgramsPythonPython310libsite-packageskerasenginedata_adapter.py", line 1859, in _get_tensor_types
    return (tf.Tensor, np.ndarray, pd.Series, pd.DataFrame)

AttributeError: module 'pandas' has no attribute 'Series'
Asked By: Rick Astley

||

Answers:

Meh, there are many mistakes here.

  1. this is not a classification problem, it’s a regression problem.
  2. the order to load the data is wrong.
  3. How can you plot your data with imshow? you don’t even have images.

Here I give you a working example:

from tensorflow import keras 
import matplotlib.pyplot as plt 
import numpy as np

data = keras.datasets.boston_housing

(x_train, y_train), (x_test, y_test) = data.load_data()

model = keras.Sequential([
    keras.layers.Dense(3, activation="relu", input_shape=(13,)),
    keras.layers.Dense(1)
])

model.compile(optimizer="adam", loss="mse")

model.summary()

model.fit(x_train, y_train, epochs=20)

predict = model.predict(x_test)
plt.scatter(y_test, predict)
plt.show()

There still a lot of things you can do to improve this model

enter image description here

I still get the error

Answered By: Rick Astley