10 Oct 2024

Machine learning with Python: An introduction to scikit-learn and TensorFlow

Machine learning is an important field of computer science that deals with the development of algorithms that can learn from data without being explicitly programmed. It involves the use of statistical and computational methods to analyze and interpret complex data patterns, and make predictions based on them. Machine learning has numerous applications in various fields, including healthcare, finance, education, and social media. In this blog post, we will introduce two popular machine learning libraries in Python: scikit-learn and TensorFlow.

Scikit-Learn

Scikit-learn is a popular open-source machine learning library for Python. It is built on top of other scientific computing libraries such as NumPy and SciPy and provides a simple and efficient tool for data mining and data analysis. Scikit-learn offers a wide range of algorithms and functions for supervised and unsupervised learning, dimensionality reduction, model selection, and preprocessing of data. Some of the popular algorithms supported by scikit-learn are linear regression, logistic regression, k-nearest neighbors, decision trees, random forests, and support vector machines.

To get started with scikit-learn, you first need to install it on your computer. You can do this using pip, the Python package manager. Once you have installed scikit-learn, you can import it into your Python script or Jupyter Notebook using the following code:

import sklearn

To demonstrate how to use scikit-learn, let us consider an example of building a linear regression model to predict the price of a house based on its size and number of bedrooms. We can start by loading the necessary libraries and data

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

# Load the data
data = pd.read_csv('house_prices.csv')
X = data[['size', 'bedrooms']].values
y = data['price'].values

# Split the data into training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create a linear regression model and fit it to the training data
model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions on the test data
y_pred = model.predict(X_test)

# Calculate the mean squared error
from sklearn.metrics import mean_squared_error
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)

In this example, we first load the house prices data from a CSV file and split it into training and testing sets using the train_test_split function from scikit-learn. We then create a linear regression model using the LinearRegression class and fit it to the training data using the fit method. Finally, we make predictions on the test data using the predict method and calculate the mean squared error using the mean_squared_error function from scikit-learn.

TensorFlow

TensorFlow is another popular open-source machine learning library for Python developed by Google. It provides a flexible and efficient platform for building and training machine learning models, especially deep learning models. TensorFlow is built around the concept of computational graphs, which are directed graphs that represent mathematical operations and data dependencies. TensorFlow allows users to define and manipulate these graphs using Python code, and then execute them efficiently on CPUs, GPUs, or TPUs.

To get started with TensorFlow, you first need to install it on your computer. You can do this using pip, the Python package manager. Once you have installed TensorFlow, you can import it into your Python script or Jupyter Notebook using the following code:

import tensorflow as tf

To demonstrate how to use TensorFlow, we can consider an example of building a neural network to classify images of handwritten digits from the MNIST dataset. We can start by loading the necessary libraries and data:

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten

# Load the data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the data
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

# Convert the labels to one-hot encoding
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

# Create a neural network model
model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dropout(0.2),
    Dense(10, activation='softmax')
])

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

# Train the model
model.fit(x_train, y_train, batch_size=128, epochs=10, validation_data=(x_test, y_test))

# Evaluate the model
score = model.evaluate(x_test, y_test, verbose=0)
print("Test loss:", score[0])
print("Test accuracy:", score[1])

In this example, we first load the MNIST dataset using the mnist.load_data function from Keras, and normalize the data to have pixel values between 0 and 1. We then convert the labels to one-hot encoding using the to_categorical function from Keras. We create a neural network model using the Sequential class from Keras, which allows us to stack layers in a linear fashion. The model consists of a flatten layer to convert the 2D image data to 1D, a dense layer with 128 neurons and a ReLU activation function, a dropout layer to prevent overfitting, and a dense layer with 10 neurons and a softmax activation function to output probabilities for each class. We compile the model using the compile method, specifying the optimizer, loss function, and metrics to use during training. We train the model using the fit method, specifying the batch size, number of epochs, and validation data. Finally, we evaluate the model using the evaluate method and print the test loss and accuracy.

Conclusion

Scikit-learn and TensorFlow are two powerful machine learning libraries in Python that offer a wide range of algorithms and functions for data analysis, modeling, and prediction. While scikit-learn is more suited for traditional machine learning tasks such as regression and classification, TensorFlow is more suited for deep learning tasks such as image recognition and natural language processing. By using these libraries, developers can easily implement and experiment with different machine learning models and algorithms in Python.

You may also like

Python Scientific Machine Learning: SciPy, scikit-learn, and PyTorch Libraries

This blog post provides an overview of three Python libraries for sc...

Continue reading

Mastering Python Networking - Python Networking Sockets http and More

Mastering Python networking - we provide an introduction to Python n...

Continue reading

Simplifying Data Analysis with Python Pandas

Python Pandas is an open-source library that simplifies data analysi...

Continue reading