<jupyter_start><jupyter_text>2. Getting started with Auto-Keras  2.2. ANN classifier for identifying handwritten digits  Load MNIST<jupyter_code>from keras.datasets import mnist
def load_mnist():
    return mnist.load_data()<jupyter_output><empty_output><jupyter_text>MNIST with pure MLP<jupyter_code>from autokeras import MlpModule
from autokeras.backend.torch.loss_function import classification_loss
from autokeras.nn.metric import Accuracy
from autokeras.backend.torch import DataTransformerMlp
from autokeras.preprocessor import OneHotEncoder
import numpy as np

def to_one_hot(y):
    y_encoder = OneHotEncoder()
    y_encoder.fit(y)
    y = y_encoder.transform(y)
    return y, y_encoder

(x_train, y_train), (x_test, y_test) = load_mnist()
x_train = np.squeeze(x_train.reshape((x_train.shape[0], -1)))
x_test = np.squeeze(x_test.reshape((x_test.shape[0], -1)))
print(x_train.shape)
y_train, y_encoder = to_one_hot(y_train)
y_test, _ = to_one_hot(y_test)
data_transformer = DataTransformerMlp(x_train)
train_data = data_transformer.transform_train(x_train, y_train)
test_data = data_transformer.transform_train(x_test, y_test)

mlpModule = MlpModule(verbose=True, loss=classification_loss, metric=Accuracy)
mlpModule.fit(n_output_node=y_encoder.n_classes,
              input_shape=(-1, x_train.shape[1]),
              train_data=train_data,
              test_data=test_data,
              time_limit=20 * 60)<jupyter_output><empty_output><jupyter_text>MNIST with ImageClassifier using MLP<jupyter_code>from autokeras.image.image_supervised import ImageClassifier

(x_train, y_train), (x_test, y_test) = load_mnist()
x_train = np.squeeze(x_train.reshape((x_train.shape[0], -1)))
x_test = np.squeeze(x_test.reshape((x_test.shape[0], -1)))
mlpModule = MlpModule(verbose=True, loss=classification_loss, metric=Accuracy)
imageClf = ImageClassifier(verbose=True)
imageClf.cnn = mlpModule
imageClf.data_transformer = DataTransformerMlp(x_train)
imageClf.fit(x_train, y_train, time_limit=20 * 60)<jupyter_output><empty_output><jupyter_text>2.3. ANN model for predicting house prices  Our data set<jupyter_code># Any housing data set, we're going to use: 
# https://www.kaggle.com/apratim87/housingdata/downloads/housingdata.zip/1
import numpy as np
from autokeras.backend.torch.loss_function import regression_loss
from autokeras.nn.metric import MSE


def load_housing():
    train_size = 400
    data = np.loadtxt('housingdata.csv', delimiter=',')
    data_train = data[:train_size, :]
    data_test = data[train_size:, :]
    
    return (data_train[:, :-1], data_train[:, -1]), \
           (data_test[:, :-1], data_test[:, -1])
           
    

(x_train, y_train), (x_test, y_test) = load_housing()
data_transformer = DataTransformerMlp(x_train)
train_data = data_transformer.transform_train(x_train, y_train)
test_data = data_transformer.transform_train(x_test, y_test)

mlpModule = MlpModule(path='temp', verbose=True, loss=regression_loss, metric=MSE)
mlpModule.fit(n_output_node=1,
              input_shape=(-1, x_train.shape[1]),
              train_data=train_data,
              test_data=test_data,
              time_limit=20 * 60)<jupyter_output><empty_output><jupyter_text>2.4. Visualizing the best ANN<jupyter_code>import os
from graphviz import Digraph
# pip install graphviz
from autokeras.utils import pickle_from_file


def to_pdf(graph, path):
    dot = Digraph(comment='Houses models')

    for index, node in enumerate(graph.node_list):
        dot.node(str(index), str(node.shape))

    for u in range(graph.n_nodes):
        for v, layer_id in graph.adj_list[u]:
            dot.edge(str(u), str(v), str(graph.layer_list[layer_id]))

    dot.render(path)


def visualize(path):
    module = pickle_from_file(os.path.join(path, 'module'))
    module.searcher.path = path
    for item in module.searcher.history:
        model_id = item['model_id']
        graph = module.searcher.load_model_by_id(model_id)
        to_pdf(graph, os.path.join(path, str(model_id)))
 

visualize('temp')<jupyter_output><empty_output><jupyter_text>2.5. Exploring more data sets<jupyter_code>import pandas as pd

pd.read_csv('housingdata.csv', delimiter = ',')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11553120533587423, 'fraction_numerical': 0.010481181515007145, 'mean_word_length': 5.007153075822604, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '378459', 'license': 'MIT', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1516, 'n_words': 271, 'path': 'Section 2.ipynb', 'repo_name': 'PacktPublishing/Hands-On-Machine-Learning-with-Auto-Keras', 'size': '4198'}