In [1]:
import tensorflow as tf
from tensorflow.keras import layers
import pandas as pd
import numpy as np
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.utils import to_categorical
In [2]:
dataFolder = 'input/'
dataFile = dataFolder + "iris.csv"
dataFile
Out[2]:
In [7]:
df = pd.read_csv(dataFile)
df.head()
Out[7]:
In [4]:
X = df.iloc[:,0:4].values
y = df.iloc[:,4].values
In [5]:
print(X[0:5])
print(y[0:5])
In [34]:
print(X.shape)
print(y.shape)
Methods
fit(y): Fit label encoder.
fit_transform(y): Fit label encoder and return encoded labels.
get_params([deep]): Get parameters for this estimator.
inverse_transform(y): Transform labels back to original encoding.
set_params(**params): Set the parameters of this estimator.
transform(y): Transform labels to normalized encoding.
In [35]:
from sklearn.preprocessing import LabelEncoder
In [36]:
encoder = LabelEncoder()
y1 = encoder.fit_transform(y)
In [37]:
print(y1)
In [38]:
Y = pd.get_dummies(y1).values
In [39]:
print(Y[0:5])
In [40]:
from sklearn.model_selection import train_test_split
In [41]:
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
In [42]:
print(X_train[0:5])
In [43]:
print(y_train[0:5])
In [44]:
print(X_test[0:5])
In [45]:
print(y_test[0:5])
In [46]:
model = tf.keras.Sequential([
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(10, activation='relu'),
tf.keras.layers.Dense(3, activation='softmax')
])
model
Out[46]:
In [47]:
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
In [48]:
model.fit(X_train, y_train, batch_size=50, epochs=100)
Out[48]:
In [49]:
loss, accuracy = model.evaluate(X_test, y_test, verbose=0)
print('Test loss:', loss)
print('Test accuracy:', accuracy)
In [50]:
y_pred = model.predict(X_test)
y_pred
Out[50]:
In [51]:
actual = np.argmax(y_test,axis=1)
predicted = np.argmax(y_pred,axis=1)
In [52]:
print(f"Actual: {actual}")
In [53]:
print(f"Predicted: {predicted}")