You can run this notebook in Binder, in Colab, in Deepnote or in Kaggle.

[1]:
!pip install --quiet climetlab tensorflow

Machine learning example

[2]:
from tensorflow.keras.layers import Input, Dense, Flatten
from tensorflow.keras.models import Sequential
[3]:
import climetlab as cml

Load the high-low data set and plot all the fields and their label

[4]:
highlow = cml.load_dataset("high-low")
for field, label in highlow.fields():
    cml.plot_map(field, width=256, title=highlow.title(label))
../_images/examples_07-high-lows_5_0.png
../_images/examples_07-high-lows_5_1.png
../_images/examples_07-high-lows_5_2.png
../_images/examples_07-high-lows_5_3.png
../_images/examples_07-high-lows_5_4.png
../_images/examples_07-high-lows_5_5.png
../_images/examples_07-high-lows_5_6.png
../_images/examples_07-high-lows_5_7.png
../_images/examples_07-high-lows_5_8.png
../_images/examples_07-high-lows_5_9.png
../_images/examples_07-high-lows_5_10.png
../_images/examples_07-high-lows_5_11.png
../_images/examples_07-high-lows_5_12.png
../_images/examples_07-high-lows_5_13.png
../_images/examples_07-high-lows_5_14.png
../_images/examples_07-high-lows_5_15.png
../_images/examples_07-high-lows_5_16.png
../_images/examples_07-high-lows_5_17.png
../_images/examples_07-high-lows_5_18.png
../_images/examples_07-high-lows_5_19.png
../_images/examples_07-high-lows_5_20.png
../_images/examples_07-high-lows_5_21.png
../_images/examples_07-high-lows_5_22.png
../_images/examples_07-high-lows_5_23.png
../_images/examples_07-high-lows_5_24.png
../_images/examples_07-high-lows_5_25.png
../_images/examples_07-high-lows_5_26.png
../_images/examples_07-high-lows_5_27.png
../_images/examples_07-high-lows_5_28.png
../_images/examples_07-high-lows_5_29.png
../_images/examples_07-high-lows_5_30.png
../_images/examples_07-high-lows_5_31.png
../_images/examples_07-high-lows_5_32.png
../_images/examples_07-high-lows_5_33.png
../_images/examples_07-high-lows_5_34.png
../_images/examples_07-high-lows_5_35.png
../_images/examples_07-high-lows_5_36.png

Get the train and test sets

[5]:
(x_train, y_train, f_train), (x_test, y_test, f_test) = highlow.load_data(
    test_size=0.3, fields=True
)

Build the model

[6]:
model = Sequential()
model.add(Input(shape=x_train[0].shape))
model.add(Flatten())
model.add(Dense(64, activation="sigmoid"))
model.add(Dense(4, activation="softmax"))
[7]:
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
print(model.summary())
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
flatten (Flatten)            (None, 441)               0
_________________________________________________________________
dense (Dense)                (None, 64)                28288
_________________________________________________________________
dense_1 (Dense)              (None, 4)                 260
=================================================================
Total params: 28,548
Trainable params: 28,548
Non-trainable params: 0
_________________________________________________________________
None

Train the model

[8]:
h = model.fit(x_train, y_train, epochs=100, verbose=0)

Evaluate the model on the test set

[9]:
model.evaluate(x_test, y_test)
1/1 [==============================] - 0s 1ms/step - loss: 0.2691 - accuracy: 0.9167
[9]:
[0.26906612515449524, 0.9166666865348816]

Plot the predictions

[10]:
predicted = model.predict(x_test)

for p, f in zip(predicted, f_test):
    cml.plot_map(f, width=256, title=highlow.title(p))
../_images/examples_07-high-lows_16_0.png
../_images/examples_07-high-lows_16_1.png
../_images/examples_07-high-lows_16_2.png
../_images/examples_07-high-lows_16_3.png
../_images/examples_07-high-lows_16_4.png
../_images/examples_07-high-lows_16_5.png
../_images/examples_07-high-lows_16_6.png
../_images/examples_07-high-lows_16_7.png
../_images/examples_07-high-lows_16_8.png
../_images/examples_07-high-lows_16_9.png
../_images/examples_07-high-lows_16_10.png
../_images/examples_07-high-lows_16_11.png