<jupyter_start><jupyter_text>Iris dataset in Keras library  Imports<jupyter_code>import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.utils import to_categorical
from sklearn.utils import shuffle
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix

print("TensorFlow version:", tf.__version__)<jupyter_output>TensorFlow version: 2.2.0<jupyter_text>Load data<jupyter_code># Load data
np_data = pd.read_csv("../data/iris.csv").values

# Split data into X and y
X_raw = np_data[:,0:-1].astype(float)
y_raw = np_data[:,-1]

# Shuffle data
X_raw, y_raw = shuffle(X_raw, y_raw, random_state=0)

# Convert class label strings to integers
encoder = LabelEncoder()
encoder.fit(y_raw)
y = encoder.transform(y_raw)

# Normalize data to avoid high input values
scaler = StandardScaler()
scaler.fit(X_raw)
X = scaler.transform(X_raw)

# Convert labels to one-hot vector
y = to_categorical(y, 3)

# Print some stuff
print("Example:")
print(X[0], "->", y_raw[0], "=", y[0])
print("")
print("Data shape:", X.shape)<jupyter_output>Example:
[-0.05250608 -0.58776353  0.76275864  1.57942861] -> Iris-virginica = [0. 0. 1.]

Data shape: (150, 4)<jupyter_text>Train-test split<jupyter_code># Split data into 80% training and 20% testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)

# Print some stuff
print("Training data shape:", X_train.shape)
print("Testing data shape:", X_test.shape)<jupyter_output>Training data shape: (120, 4)
Testing data shape: (30, 4)<jupyter_text>Train and evaluate NN model on all data<jupyter_code># Create neural network model
model = Sequential()
model.add(Dense(64, input_dim=4, activation="relu", kernel_initializer="he_normal"))
model.add(Dense(3, activation="softmax"))

# Compile model
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

# Train the model on all data
model.fit(X, y, epochs=200, batch_size=32, verbose=0)

# Evaluate on all data
score = model.evaluate(X, y, verbose=0)

# Print results
print("Accuracy: {0:.2f}%".format(score[1]*100))<jupyter_output>Accuracy: 98.67%<jupyter_text>Train and evaluate NN model on test data<jupyter_code># Create neural network model
model = Sequential()
model.add(Dense(64, input_dim=4, activation="relu", kernel_initializer="he_normal"))
model.add(Dense(3, activation="softmax"))

# Compile model
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])

# Train the model on training data
model.fit(X_train, y_train, epochs=200, batch_size=32, verbose=0)

# Evaluate on test data
score = model.evaluate(X_test, y_test, verbose=0)

# Print results
print("Accuracy: {0:.2f}%".format(score[1]*100))<jupyter_output>Accuracy: 93.33%<jupyter_text>Confusion matrix<jupyter_code># Make predictions
y_pred = model.predict(X_test)

# Confusion matrix
conf_mx = confusion_matrix(
    np.argmax(y_test,axis=1), 
    np.argmax(y_pred, axis=1))
print(conf_mx)<jupyter_output>[[13  0  0]
 [ 0  7  0]
 [ 0  2  8]]<jupyter_text>Predict new examples<jupyter_code># Create two new examples
example = [
    [5.1,3.5,1.4,0.2],
    [5.4,3.3,1.2,0.1]
]

# Normalize values
example = scaler.transform(example)

# Make prediction
res = model.predict(example)
print("Prediction:", np.argmax(res, axis=1))<jupyter_output>Prediction: [0 0]
{'fraction_non_alphanumeric': 0.13188041352333055, 'fraction_numerical': 0.039396479463537304, 'mean_word_length': 6.473903966597077, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '578515', 'license': 'MIT', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1294, 'n_words': 309, 'path': 'Keras/Iris Keras.ipynb', 'repo_name': 'jhagelback/ml-sk', 'size': '3579'}