Basic Machine Learning with Python (Mini-Project Included)

Jovan
4 min readMar 30, 2023

Introduction

Machine learning is a subfield of artificial intelligence that allows systems to automatically learn and improve from experience without being explicitly programmed. It has become an important tool for solving a wide range of problems in various fields, including finance, healthcare, education, and many more. In this article, we will explore the basics of machine learning with code examples and a mini project.

Prerequisites

Before diving into machine learning, you should have a basic understanding of programming concepts and some knowledge of Python programming language. If you are new to Python, you can learn the basics by following the official Python tutorial. Additionally, you should be familiar with some mathematical concepts such as calculus and linear algebra.

Getting Started with Machine Learning

Machine learning involves a series of steps that include data collection, data preprocessing, model building, and evaluation. In this section, we will cover each of these steps and provide some code examples.

1. Data Collection

The first step in machine learning is to collect data. The data can be in various forms such as structured data, images, audio, or text. Depending on the problem you are trying to solve, you may need to collect data from various sources.

For this tutorial, we will be using a popular dataset called the Iris dataset. The Iris dataset contains measurements of different species of Iris flowers. We will be using this dataset to build a machine learning model that can classify different species of Iris flowers based on their measurements.

To load the Iris dataset in Python, we can use the scikit-learn library as follows:

from sklearn.datasets import load_iris

iris = load_iris()
X = iris.data
y = iris.targetp

2. Data Preprocessing

After collecting the data, the next step is to preprocess the data. This involves cleaning and transforming the data into a format that can be used by the machine learning model.

In our Iris dataset, we can preprocess the data by splitting it into training and testing sets. We will use 80% of the data for training the model and 20% of the data for testing the model.

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)

3. Model Building

Once the data is preprocessed, we can start building the machine learning model. There are various machine learning algorithms that can be used for different types of problems. In this tutorial, we will be using the K-Nearest Neighbors (KNN) algorithm to classify the Iris flowers.

To build a KNN model in Python, we can use the scikit-learn library as follows:

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X_train, y_train)

4. Model Evaluation

After building the model, we need to evaluate its performance. There are various metrics that can be used to evaluate the performance of a machine learning model such as accuracy, precision, recall, and F1 score.

In our Iris dataset, we can evaluate the performance of our KNN model by calculating its accuracy on the test data.

from sklearn.metrics import accuracy_score

y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

The output should be something like: Accuracy: 1.0, which means our model was able to correctly classify all the test samples.

Mini Project: Image Classification with CNN

In this mini project, we will build a deep learning model using Convolutional Neural Networks (CNN) to classify images of cats and dogs.

1. Data Collection

We will be using a dataset called the Dogs and Cats dataset, which contains images of dogs and cats. You can download the dataset from Kaggle or use the following command to download it directly in Python:

!wget https://www.dropbox.com/s/5op3w2yr5jklg5k/cats_dogs_dataset.zip

2. Data Preprocessing

After downloading the dataset, we need to preprocess the data by resizing the images to a fixed size and normalizing the pixel values. We can use the Keras library to preprocess the data as follows:

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
'cats_dogs_dataset/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary')

test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
'cats_dogs_dataset/test',
target_size=(150, 150),
batch_size=32,
class_mode='binary')

3. Model Building

Next, we will build a CNN model using Keras. The model will have 3 convolutional layers, followed by max pooling layers, and then 2 fully connected layers.

from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

model = Sequential()

model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.summary()

4. Model Training and Evaluation

After building the model, we can train it on the training data and evaluate its performance on the test data.

model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])

history = model.fit(
train_generator,
steps_per_epoch=train_generator.samples/train_generator.batch_size,
epochs=10,
validation_data=test_generator,
validation_steps=test_generator.samples/test_generator.batch_size)

test_loss, test_acc = model.evaluate(test_generator, verbose=2)
print('Test accuracy:', test_acc)

The output should be something like: Test accuracy: 0.8414, which means our model was able to correctly classify 84% of the test images.

Conclusion

In this article, we covered the basics of machine learning with code examples and a mini project. We learned how to collect and preprocess data, build a machine learning model, and evaluate its performance. We also built a deep learning model using CNN to classify images of cats and dogs. Machine learning is a vast field with many applications, and we have only scratched the surface in this tutorial. I hope this article has provided a good starting point for you to explore machine learning further.

--

--

Jovan

I code and write random stories. Buy me a coffee if you like what you're reading - https://www.paypal.me/ltcjovan