<jupyter_start><jupyter_text>Expore dataset<jupyter_code>df = pd.read_csv('iris.csv')

display(df.info())
display(df.head())
display(df.describe())

# Labels = 3 flower species
display(df['species'].value_counts())
# Plot the distribution of each feature vs others (1 - 1)

# We have 3 flower species: 
#     - virginica, 
#     - setosa,
#     - versicolor

# 4 features: 
#     - sepal_length,
#     - sepal_width,
#     - petal_length,
#     - petal_width

sns.pairplot(df, hue="species")<jupyter_output>/home/dangkhoa/.conda/envs/py36/lib/python3.6/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>Feature Engineering<jupyter_code># convert flower specie names -> num
target_names = df['species'].unique()
target_dict = {n:i for i, n in enumerate(target_names)}

target_dict
X = df.drop('species', axis=1).values

X[:5]
# convert flower specie names -> num
y = df['species'].map(target_dict)

# One-hot encoding y
from keras.utils.np_utils import to_categorical
y_cat = to_categorical(y)

y_cat[:5]<jupyter_output>Using TensorFlow backend.<jupyter_text>Split Train/Test set<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    X, y_cat,
    test_size=0.2)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam

model = Sequential()

model.add(Dense(3, input_shape=(4,), activation='softmax'))

model.compile(Adam(lr=0.1),
              loss='categorical_crossentropy',
              metrics=['accuracy'])
# Train
result = model.fit(
    X_train, y_train,
    epochs=20, validation_split=0.1)<jupyter_output>Train on 108 samples, validate on 12 samples
Epoch 1/20
108/108 [==============================] - 1s 10ms/step - loss: 2.5284 - acc: 0.2870 - val_loss: 1.8779 - val_acc: 0.3333
Epoch 2/20
108/108 [==============================] - 0s 243us/step - loss: 1.8587 - acc: 0.3611 - val_loss: 1.7864 - val_acc: 0.5833
Epoch 3/20
108/108 [==============================] - 0s 281us/step - loss: 1.1970 - acc: 0.6759 - val_loss: 0.6499 - val_acc: 0.6667
Epoch 4/20
108/108 [==============================] - 0s 151us/step - loss: 0.7574 - acc: 0.6759 - val_loss: 0.6714 - val_acc: 0.6667
Epoch 5/20
108/108 [==============================] - 0s 394us/step - loss: 0.5513 - acc: 0.7685 - val_loss: 0.6720 - val_acc: 0.5833
Epoch 6/20
108/108 [==============================] - 0s 184us/step - loss: 0.5705 - acc: 0.6852 - val_loss: 0.4521 - val_acc: 0.6667
Epoch 7/20
108/108 [==============================] - 0s 452us/step - loss: 0.4254 - acc: 0.8056 - val_loss: 0.4917 - val_acc: 0.6667
Epoch 8/20
108/108[...]<jupyter_text>Evaluate model<jupyter_code>plt.plot(result.history['acc'])
plt.plot(result.history['val_acc'])

plt.legend(['Training', 'Validation'])
plt.title('Accuracy')
plt.xlabel('Epochs')
from sklearn.metrics import accuracy_score

# Training set
y_train_pred = model.predict(X_train)

y_train_pred_label = [np.argmax(y, axis=None, out=None) for y in y_train_pred] # reverse One-hot encoding
y_train_label = [np.argmax(y, axis=None, out=None) for y in y_train] # reverse One-hot encoding


print("The Accuracy score on the Train set is:\t{:0.3f}".format(
    accuracy_score(
        y_train_label,
        y_train_pred_label)))<jupyter_output>The Accuracy score on the Train set is:	0.958<jupyter_text>Evaluate test set  Accuracy<jupyter_code>from sklearn.metrics import accuracy_score

# Test set
y_test_pred = model.predict(X_test)

y_test_pred_label = [np.argmax(y, axis=None, out=None) for y in y_test_pred] # reverse One-hot encoding
y_test_label = [np.argmax(y, axis=None, out=None) for y in y_test] # reverse One-hot encoding


print("The Accuracy score on the Test set is:\t{:0.3f}".format(
    accuracy_score(
        y_test_label,
        y_test_pred_label)))<jupyter_output>The Accuracy score on the Test set is:	0.900<jupyter_text>Report<jupyter_code>from sklearn.metrics import classification_report

print(classification_report(y_test_label, y_test_pred_label))<jupyter_output>precision    recall  f1-score   support

          0       1.00      1.00      1.00         7
          1       0.90      0.82      0.86        11
          2       0.85      0.92      0.88        12

avg / total       0.90      0.90      0.90        30<jupyter_text>Confusion matrix<jupyter_code>from sklearn.metrics import confusion_matrix

confusion_matrix(y_test_label, y_test_pred_label)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1612836864842625, 'fraction_numerical': 0.06768154700678873, 'mean_word_length': 4.09643605870021, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '555185', 'license': 'Unlicense', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1750, 'n_words': 400, 'path': 'CourseUdemy_Keras/3_Deep_Learning/2_Multiclass_Classification.ipynb', 'repo_name': 'dangkhoa1992/Advanced-Machine-Learning-Specialization', 'size': '4861'}