Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using Convolutions on the IRIS dataset

In the famous iris dataset, I have tried, different ML models such as linear regression, SVM, Decision Trees, and Random Forests. I want to use a convolution network. I saw this in a quora post and found it to be interesting. This is just for curiosity, but how can I implement this?
Post link : https://www.quora.com/How-can-I-train-an-Iris-data-set-in-a-CNN-algorithm

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use this code below:

# Create your features and Y
X = np.array(df.drop("Species", axis=1))

y = np.array(df.Species).reshape(-1, 1)


# Encoding Target Y to One-Hot format
encoder = OneHotEncoder(sparse=False)
y = encoder.fit_transform(y)

# You can then use train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, stratify=y)

# Important to reshape your data here to feed it to the CNN.
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))


# Build the model and train
model = tf.keras.Sequential()

model.add(tf.keras.layers.Convolution1D(filters=2, kernel_size=1,input_shape=(4,1), name='Conv1'))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(10, activation='relu', name='Dense1'))
model.add(tf.keras.layers.Dense(10, activation='relu', name='Dense2'))
model.add(tf.keras.layers.Dense(3, activation='softmax', name='output'))

optimizer = tf.keras.optimizers.Adam(lr=0.001)
model.compile(optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

model.summary()

model.fit(X_train, y_train, validation_data=(X_test, y_test) ,epochs=1000,) 


# Test preditions
y_pred = model.predict(X_test)

# Get argmax of both y_test and y_pred to get classes
y_pred = np.argmax(y_pred, axis=1)
y_test = np.argmax(y_test, axis=1)

# Accuracy and Classification Report
print(f"Accuracy:{accuracy_score(y_test, y_pred)*100}%")
print(classification_report(y_test, y_pred))

This is it!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading