<jupyter_start><jupyter_text>A Simple ExampleThe first step is to prepare your data. Here we use the [Titanicdataset](https://www.kaggle.com/c/titanic) as an example.<jupyter_code>import tensorflow as tf
import autokeras as ak

TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"
TEST_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/eval.csv"

train_file_path = tf.keras.utils.get_file("train.csv", TRAIN_DATA_URL)
test_file_path = tf.keras.utils.get_file("eval.csv", TEST_DATA_URL)<jupyter_output><empty_output><jupyter_text>The second step is to run the[StructuredDataClassifier](/structured_data_classifier).As a quick demo, we set epochs to 10.You can also leave the epochs unspecified for an adaptive number of epochs.<jupyter_code># Initialize the structured data classifier.
clf = ak.StructuredDataClassifier(
    overwrite=True,
    max_trials=3) # It tries 3 different models.
# Feed the structured data classifier with training data.
clf.fit(
    # The path to the train.csv file.
    train_file_path,
    # The name of the label column.
    'survived',
    epochs=10)
# Predict with the best model.
predicted_y = clf.predict(test_file_path)
# Evaluate the best model with testing data.
print(clf.evaluate(test_file_path, 'survived'))<jupyter_output><empty_output><jupyter_text>Data FormatThe AutoKeras StructuredDataClassifier is quite flexible for the data format.The example above shows how to use the CSV files directly. Besides CSV files, it alsosupports numpy.ndarray, pandas.DataFrame or [tf.data.Dataset](https://www.tensorflow.org/api_docs/python/tf/data/Dataset?version=stable). The data should betwo-dimensional with numerical or categorical values.For the classification labels,AutoKeras accepts both plain labels, i.e. strings or integers, and one-hot encodedencoded labels, i.e. vectors of 0s and 1s.The labels can be numpy.ndarray, pandas.DataFrame, or pandas.Series.The following examples show how the data can be prepared with numpy.ndarray,pandas.DataFrame, and tensorflow.data.Dataset.<jupyter_code>import pandas as pd
import numpy as np
# x_train as pandas.DataFrame, y_train as pandas.Series
x_train = pd.read_csv(train_file_path)
print(type(x_train)) # pandas.DataFrame
y_train = x_train.pop('survived')
print(type(y_train)) # pandas.Series

# You can also use pandas.DataFrame for y_train.
y_train = pd.DataFrame(y_train)
print(type(y_train)) # pandas.DataFrame

# You can also use numpy.ndarray for x_train and y_train.
x_train = x_train.to_numpy()
y_train = y_train.to_numpy()
print(type(x_train)) # numpy.ndarray
print(type(y_train)) # numpy.ndarray

# Preparing testing data.
x_test = pd.read_csv(test_file_path)
y_test = x_test.pop('survived')

# It tries 10 different models.
clf = ak.StructuredDataClassifier(
    overwrite=True,
    max_trials=3)
# Feed the structured data classifier with training data.
clf.fit(x_train, y_train, epochs=10)
# Predict with the best model.
predicted_y = clf.predict(x_test)
# Evaluate the best model with testing data.
print(clf.evaluate(x_test, y_test))<jupyter_output><empty_output><jupyter_text>The following code shows how to convert numpy.ndarray to tf.data.Dataset.<jupyter_code>train_set = tf.data.Dataset.from_tensor_slices((x_train.astype(np.unicode), y_train))
test_set = tf.data.Dataset.from_tensor_slices((x_test.to_numpy().astype(np.unicode), y_test))

clf = ak.StructuredDataClassifier(
    overwrite=True,
    max_trials=3)
# Feed the tensorflow Dataset to the classifier.
clf.fit(train_set, epochs=10)
# Predict with the best model.
predicted_y = clf.predict(test_set)
# Evaluate the best model with testing data.
print(clf.evaluate(test_set))<jupyter_output><empty_output><jupyter_text>You can also specify the column names and types for the data as follows.The `column_names` is optional if the training data already have the column names, e.g.pandas.DataFrame, CSV file.Any column, whose type is not specified will be inferred from the training data.<jupyter_code># Initialize the structured data classifier.
clf = ak.StructuredDataClassifier(
    column_names=[
        'sex',
        'age',
        'n_siblings_spouses',
        'parch',
        'fare',
        'class',
        'deck',
        'embark_town',
        'alone'],
    column_types={'sex': 'categorical', 'fare': 'numerical'},
    max_trials=10, # It tries 10 different models.
    overwrite=True,
)<jupyter_output><empty_output><jupyter_text>Validation DataBy default, AutoKeras use the last 20% of training data as validation data.As shown in the example below, you can use `validation_split` to specify the percentage.<jupyter_code>clf.fit(x_train,
        y_train,
        # Split the training data and use the last 15% as validation data.
        validation_split=0.15,
        epochs=10)<jupyter_output><empty_output><jupyter_text>You can also use your own validation setinstead of splitting it from the training data with `validation_data`.<jupyter_code>split = 500
x_val = x_train[split:]
y_val = y_train[split:]
x_train = x_train[:split]
y_train = y_train[:split]
clf.fit(x_train,
        y_train,
        # Use your own validation set.
        validation_data=(x_val, y_val),
        epochs=10)<jupyter_output><empty_output><jupyter_text>Customized Search SpaceFor advanced users, you may customize your search space by using[AutoModel](/auto_model/automodel-class) instead of[StructuredDataClassifier](/structured_data_classifier). You can configure the[StructuredDataBlock](/block/structureddatablock-class) for some high-levelconfigurations, e.g., `categorical_encoding` for whether to use the[CategoricalToNumerical](/block/categoricaltonumerical-class). You can also do not specify thesearguments, which would leave the different choices to be tuned automatically. Seethe following example for detail.<jupyter_code>import autokeras as ak

input_node = ak.StructuredDataInput()
output_node = ak.StructuredDataBlock(categorical_encoding=True)(input_node)
output_node = ak.ClassificationHead()(output_node)
clf = ak.AutoModel(
    inputs=input_node, 
    outputs=output_node, 
    overwrite=True,
    max_trials=3)
clf.fit(x_train, y_train, epochs=10)<jupyter_output><empty_output><jupyter_text>The usage of [AutoModel](/auto_model/automodel-class) is similar to the[functional API](https://www.tensorflow.org/guide/keras/functional) of Keras.Basically, you are building a graph, whose edges are blocks and the nodes are intermediate outputs of blocks.To add an edge from `input_node` to `output_node` with`output_node = ak.[some_block]([block_args])(input_node)`.You can even also use more fine grained blocks to customize the search space evenfurther. See the following example.<jupyter_code>import autokeras as ak

input_node = ak.StructuredDataInput()
output_node = ak.CategoricalToNumerical()(input_node)
output_node = ak.DenseBlock()(output_node)
output_node = ak.ClassificationHead()(output_node)
clf = ak.AutoModel(
    inputs=input_node, 
    outputs=output_node, 
    overwrite=True,
    max_trials=1)
clf.fit(x_train, y_train, epochs=1)
clf.predict(x_train)<jupyter_output><empty_output><jupyter_text>You can also export the best model found by AutoKeras as a Keras Model.<jupyter_code>model = clf.export_model()
model.summary()
print(x_train.dtype)
# numpy array in object (mixed type) is not supported.
# convert it to unicode.
model.predict(x_train.astype(np.unicode))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09535353535353536, 'fraction_numerical': 0.0052525252525252525, 'mean_word_length': 5.4350086655112655, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 5, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '63011', 'license': 'Apache-2.0', 'n_tokens_mistral': 2476, 'n_tokens_neox': 2385, 'n_words': 647, 'path': 'docs/ipynb/structured_data_classification.ipynb', 'repo_name': 'Hareesh-ganapathy/autokeras', 'size': '7425'}