Practical Machine Learning Examples: Scikit-learn, Keras and TensorFlow

Practical Machine Learning Examples: Scikit-learn, Keras and TensorFlow

Machine learning has transitioned from a futuristic concept to an integral part of our daily lives. From personalized recommendations on streaming platforms to fraud detection in financial transactions, machine learning algorithms are silently working behind the scenes. This article delves into the practical applications of machine learning, focusing on three popular libraries: Scikit-learn, Keras, and TensorFlow. We’ll explore diverse examples, demonstrating how these tools empower developers to build intelligent systems.

Scikit-learn: Simplicity and Power for Classical Machine Learning

Scikit-learn is a versatile library built on top of NumPy, SciPy, and Matplotlib, offering a comprehensive suite of tools for classical machine learning tasks. Its user-friendly API and efficient implementations make it ideal for both beginners and experienced practitioners.

1. Iris Flower Classification:

This classic example demonstrates supervised learning using the Iris dataset. The dataset contains measurements of sepal length, sepal width, petal length, and petal width for three different species of Iris flowers: Setosa, Versicolor, and Virginica.

“`python
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score

Load the Iris dataset

iris = load_iris()
X, y = iris.data, iris.target

Split the data into training and testing sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

Create a K-Nearest Neighbors classifier

knn = KNeighborsClassifier(n_neighbors=3)

Train the classifier

knn.fit(X_train, y_train)

Make predictions on the test set

y_pred = knn.predict(X_test)

Evaluate the classifier’s accuracy

accuracy = accuracy_score(y_test, y_pred)
print(f”Accuracy: {accuracy}”)
“`

This code snippet loads the dataset, splits it into training and testing sets, trains a K-Nearest Neighbors classifier, and evaluates its performance using accuracy.

2. Spam Detection with Naive Bayes:

Naive Bayes classifiers are widely used for text classification tasks like spam detection. This example demonstrates how to build a spam filter using Scikit-learn.

“`python
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline

Sample data (replace with real data)

spam_emails = [“free money now!!!”, “win a lottery”, “cheap viagra”]
ham_emails = [“meeting agenda”, “project update”, “important information”]

Create training data

X = spam_emails + ham_emails
y = [1] * len(spam_emails) + [0] * len(ham_emails) # 1 for spam, 0 for ham

Create a pipeline

text_clf = Pipeline([
(‘vect’, CountVectorizer()),
(‘clf’, MultinomialNB()),
])

Train the classifier

text_clf.fit(X, y)

Predict the class of new emails

new_emails = [“congratulations you won”, “please review the document”]
predictions = text_clf.predict(new_emails)

print(predictions) # Output: [1 0]
“`

This example uses a pipeline to combine text vectorization and classification, simplifying the process.

Keras: Building Neural Networks with Ease

Keras provides a high-level API for building and training neural networks. Its simplicity and flexibility make it a popular choice for deep learning projects.

3. Image Classification with MNIST:

The MNIST dataset, containing handwritten digits, is a benchmark for image classification tasks.

“`python
import tensorflow as tf
from tensorflow import keras

Load the MNIST dataset

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

Normalize the data

x_train = x_train.astype(“float32”) / 255
x_test = x_test.astype(“float32”) / 255

Reshape the data

x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)

Create a convolutional neural network

model = keras.models.Sequential([
keras.layers.Conv2D(32, (3, 3), activation=’relu’, input_shape=(28, 28, 1)),
keras.layers.MaxPooling2D((2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(10, activation=’softmax’)
])

Compile the model

model.compile(optimizer=’adam’,
loss=’sparse_categorical_crossentropy’,
metrics=[‘accuracy’])

Train the model

model.fit(x_train, y_train, epochs=5)

Evaluate the model

loss, accuracy = model.evaluate(x_test, y_test)
print(f”Test accuracy: {accuracy}”)
“`

This code defines a convolutional neural network (CNN), compiles it, trains it on the MNIST dataset, and evaluates its performance.

4. Sentiment Analysis with Recurrent Neural Networks:

Recurrent Neural Networks (RNNs) are well-suited for sequential data like text. This example demonstrates sentiment analysis using an LSTM network.

“`python
from tensorflow.keras.layers import Embedding, LSTM, Dense
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.datasets import imdb

Load the IMDB dataset

(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=10000)

Pad sequences to a fixed length

x_train = pad_sequences(x_train, maxlen=200)
x_test = pad_sequences(x_test, maxlen=200)

Create an LSTM model

model = Sequential()
model.add(Embedding(10000, 32))
model.add(LSTM(32))
model.add(Dense(1, activation=’sigmoid’))

Compile the model

model.compile(optimizer=’adam’, loss=’binary_crossentropy’, metrics=[‘accuracy’])

Train the model

model.fit(x_train, y_train, epochs=3)

Evaluate the model

loss, accuracy = model.evaluate(x_test, y_test)
print(f”Test accuracy: {accuracy}”)

“`

This example uses an LSTM network to classify movie reviews as positive or negative.

TensorFlow: A Powerful Framework for Deep Learning

TensorFlow provides a low-level API for building and training machine learning models. It offers greater flexibility and control compared to Keras, making it suitable for complex projects.

5. Linear Regression with TensorFlow:

This example demonstrates simple linear regression using TensorFlow.

“`python
import tensorflow as tf
import numpy as np

Create sample data

X = np.array([1, 2, 3, 4, 5], dtype=float)
y = np.array([2, 4, 5, 4, 5], dtype=float)

Define the model

model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1])
])

Compile the model

model.compile(optimizer=’sgd’, loss=’mean_squared_error’)

Train the model

model.fit(X, y, epochs=100)

Make predictions

predictions = model.predict([6.0])
print(predictions)
“`

6. Building a Custom CNN with TensorFlow:

While Keras provides a convenient way to build CNNs, TensorFlow allows for greater customization. This example shows how to construct a custom CNN layer.

“`python
import tensorflow as tf

class MyConv2D(tf.keras.layers.Layer):
def init(self, filters, kernel_size, strides=1, padding=’VALID’, activation=None):
super(MyConv2D, self).init()
self.filters = filters
self.kernel_size = kernel_size
self.strides = strides
self.padding = padding
self.activation = activation

def build(self, input_shape):
    self.w = self.add_weight(shape=(self.kernel_size[0], self.kernel_size[1], input_shape[-1], self.filters),
                             initializer='random_normal',
                             trainable=True)
    self.b = self.add_weight(shape=(self.filters,),
                             initializer='zeros',
                             trainable=True)

def call(self, inputs):
    x = tf.nn.conv2d(inputs, self.w, strides=[1, self.strides, self.strides, 1], padding=self.padding)
    x = tf.nn.bias_add(x, self.b)
    if self.activation is not None:
        x = self.activation(x)
    return x

Example usage:

input_tensor = tf.random.normal([1, 28, 28, 1])
conv_layer = MyConv2D(32, (3, 3), activation=tf.nn.relu)
output_tensor = conv_layer(input_tensor)

print(output_tensor.shape)

“`

This example demonstrates how to build a custom convolutional layer with TensorFlow, providing more control over the network architecture.

Looking Forward: The Expanding Landscape of Machine Learning

This article has provided a glimpse into the practical applications of machine learning using Scikit-learn, Keras, and TensorFlow. We explored various examples, ranging from simple classification tasks to more complex neural networks. As the field of machine learning continues to evolve, these tools will undoubtedly play a crucial role in shaping the future of intelligent systems. The continued development of these libraries, along with the emergence of new techniques and algorithms, promises even more powerful and accessible machine learning solutions for years to come. Experimentation and continuous learning are key to mastering these tools and harnessing the full potential of machine learning.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top