<jupyter_start><jupyter_text>MULTILABEL CLASSFICATION    Tensorflow 2를 이용한 코드 예제  Configure Learning Environment<jupyter_code># !pip install git+https://github.com/nockchun/rspy --force
import rspy as rsp
rsp.setSystemWarning(off=True)
import numpy as np
import matplotlib.pyplot as plt

import pandas as pd
import seaborn as sns
import tensorflow as tf
from tensorflow.keras import utils, layers, models, losses
np.set_printoptions(linewidth=200, precision=2)
sns.set_style("whitegrid")
tf.__version__<jupyter_output><empty_output><jupyter_text>Prepare Data<jupyter_code>dfJuice = pd.read_excel("resources/juice.xlsx")
dfJuice<jupyter_output><empty_output><jupyter_text>Multi-Label<jupyter_code>dfMulti = dfJuice.replace({"맛":{"없다":0, "있다":1}})
dfMulti = dfMulti.astype({"믹스":"int", "매출":"int"})
dfMulti
data = dfMulti[["오랜지", "딸기", "포도"]].to_numpy()
label = dfMulti[["맛", "믹스", "매출"]].values
data.shape, label.shape<jupyter_output><empty_output><jupyter_text>Generate Model<jupyter_code>model = models.Sequential([
    layers.Input([3]),
    layers.Dense(30, activation=tf.keras.activations.relu),
    layers.Dense(3, activation=tf.keras.activations.sigmoid)
])
model.summary()
utils.plot_model(model, 'model.png', show_shapes=True)
model.compile(
    loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.01)
)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>history = model.fit(
    data, label, epochs=500, verbose=0
)
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.plot(history.history["loss"])
plt.show()<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code>predictedAll = np.array([[]])
predictedLabelAll = np.array([[]])
for itemData, itemLabel in zip(data, label):
    predicted = model.predict([itemData.tolist()])
    predictedLabel = np.where(predicted > 0.5, 1, 0)
    predictedAll = np.hstack((predictedAll, predicted))
    predictedLabelAll = np.hstack((predictedLabelAll, predictedLabel))
rsp.showMulti(
    predictedAll.reshape(-1,3),
    predictedLabelAll.reshape(-1,3),
    label
)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14933837429111532, 'fraction_numerical': 0.012287334593572778, 'mean_word_length': 7.26953125, 'pattern_counts': {'":': 5, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '108474', 'license': 'Apache-2.0', 'n_tokens_mistral': 831, 'n_tokens_neox': 829, 'n_words': 120, 'path': 'lecture_source/machine_learning/0210_multilabel_classification.ipynb', 'repo_name': 'nockchun/rsnet', 'size': '2116'}