<jupyter_start><jupyter_text>AutoKerasディープラーニングモデルを自動構築するPythonライブラリ データセットの読み込み(ボストン住宅価格)<jupyter_code>from sklearn.datasets import load_boston
import pandas as pd
from sklearn.model_selection import train_test_split

boston = load_boston()
df_x_boston = pd.DataFrame(boston['data'], columns=boston['feature_names'])
df_y_boston = pd.DataFrame(boston['target'], columns=['MEDV'])
df_boston = pd.concat([df_x_boston, df_y_boston], axis=1)

x_train, x_test, y_train, y_test = train_test_split(df_x_boston, df_y_boston, test_size=0.2, random_state=1)

df_train = pd.concat([x_train, y_train], axis=1)
df_test = pd.concat([x_test, y_test], axis=1)
x_train
y_train<jupyter_output><empty_output><jupyter_text>AutoKeras動作確認<jupyter_code>import autokeras as ak
# Initialize the structured data regressor.
reg = ak.StructuredDataRegressor(
    overwrite=True, max_trials=10,
    project_name="structured_data_regressor",
    metrics=["mean_squared_error", "mean_absolute_error"],
    seed=1234
)  # It tries 10 different models.
# Feed the structured data regressor with training data.
reg.fit(
    x=x_train,
    y=y_train,
    epochs=200,
)
from sklearn.metrics import r2_score
from sklearn.metrics import mean_absolute_error

pred = reg.predict(x_test)
r2 = r2_score(y_test, pred)
mae = mean_absolute_error(y_test, pred)

print("R2 : %.3f" % r2)
print("MAE : %.3f" % mae)<jupyter_output>4/4 [==============================] - 0s 3ms/step
R2 : 0.907
MAE : 2.349<jupyter_text>OptunaのGBRの結果```R2 : 0.929MAE : 2.028```とほぼ同程度の数値が得られた．次にデータを標準化して試してみる．<jupyter_code>from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
sc.fit(x_train)
x_train_std = pd.DataFrame(sc.transform(x_train), columns=boston['feature_names'])
x_test_std = pd.DataFrame(sc.transform(x_test), columns=boston['feature_names'])<jupyter_output><empty_output><jupyter_text>ついでにTensorBoardで可視化もできるみたいなので，試してみる．<jupyter_code>std_dir='structured_data_regressor-std'
!mkdir -p $std_dir
!ls
from tensorflow import keras

tensorboard_callback_train = keras.callbacks.TensorBoard(log_dir=std_dir)
# Initialize the structured data regressor.
reg_std = ak.StructuredDataRegressor(
    overwrite=True, max_trials=10,
    project_name=std_dir,
    metrics=["mean_squared_error", "mean_absolute_error"],
    seed=1234
)  # It tries 10 different models.
# Feed the structured data regressor with training data.
reg_std.fit(
    x=x_train_std,
    y=y_train,
    epochs=200,
    callbacks=[tensorboard_callback_train],
)
pred_std = reg_std.predict(x_test_std)
r2_std = r2_score(y_test, pred_std)
mae_std = mean_absolute_error(y_test, pred_std)

print("R2 : %.3f" % r2_std)
print("MAE : %.3f" % mae_std)<jupyter_output>4/4 [==============================] - 0s 5ms/step
R2 : 0.912
MAE : 2.260<jupyter_text>桁が変わるような劇的な変化はなかった．<jupyter_code>%load_ext tensorboard<jupyter_output><empty_output><jupyter_text>Jupyter上ではうまく表示できるときとできないときがある．  ```docker exec```で起動しているコンテナにbashでログインして，``` tensorboard --logdir $std_dir --host=0.0.0.0 --port 35000```でTensorBoardを起動後，ブラウザで```http://localhost:10002```でアクセスできる．<jupyter_code>#%tensorboard --logdir $std_dir --host=0.0.0.0 --port 10002<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1406003159557662, 'fraction_numerical': 0.031279620853080566, 'mean_word_length': 7.375661375661376, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '211741', 'license': 'Apache-2.0', 'n_tokens_mistral': 1421, 'n_tokens_neox': 1333, 'n_words': 193, 'path': 'python/automated_machine_learning/AutoKeras/StructuredDataRegressor.ipynb', 'repo_name': 'ryoma-jp/samples', 'size': '3165'}