This article introduces fundamental machine learning concepts using widely recognized code examples from the** ****Machine Learning Basics** course on IBM Skills Network. It covers essential techniques and core principles, making it easy for beginners to follow and feel confident in their understanding of machine learning.

Machine learning is a subset of artificial intelligence that focuses on building algorithms that can learn from data. It involves the creation of models that can make predictions or decisions without being explicitly programmed. Let’s explore some core concepts using Python.

Python is a popular language for machine learning due to its extensive libraries and ease of use. Libraries like `NumPy`

, `Pandas`

, and `Sckkit-learn`

are go-to tools for data manipulation, analysis, and modeling.

`import numpy as np`

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.linear_model import LogisticRegression

Before diving into machine learning algorithms, it’s crucial to split your data into a training set and a test set. The **training set** is used to train the model, while the **test set** is used to evaluate its performance.

`# Splitting data into training and test sets`

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

Two common types of machine learning problems are classification and regression. Classification involves predicting a class label, while regression predicts a continuous value. Let’s look at examples and code snippets for both:

`from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score`

from sklearn.datasets import load_iris# Load the Iris dataset

data = load_iris()

X = data.data

y = data.target

# Create a Logistic Regression model

model = LogisticRegression()

# Fit the model to the training data

model.fit(X_train, y_train)

# Make predictions on the test set

y_pred = model.predict(X_test)

- Accuracy: Accuracy is the ratio of correctly predicted instances to the total instances in the dataset. Formula:

`Accuracy = Number of Correct Predictions / Total Number of Predictions`

2. Confusion Matrix: A confusion matrix is a table used to visualize the performance of a classification algorithm. It shows the counts of true positive, true negative, false positive, and false negative predictions.

`from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score`

from sklearn.linear_model import LinearRegression# Load a sample dataset for regression

from sklearn.datasets import load_boston

data = load_boston()

X = data.data

y = data.target

# Create a Linear Regression model

regression_model = LinearRegression()

# Fit the model to the training data

regression_model.fit(X_train, y_train)

# Make predictions on the test set

y_pred = regression_model.predict(X_test)

Evaluating machine learning models is essential to determine their performance. Different metrics are used for classification and regression tasks. Let’s take a look at these metrics:

**Accuracy:**Accuracy is the ratio of correctly predicted instances to the total instances in the dataset. Formula:

`Accuracy = Total Number of Correct Predictions / Total Number of Predictions`

2. **Precision:** Precision represents the proportion of correctly predicted positive instances out of all instances predicted as positive. Formula:

`Precision = True Positives / (True Positives + False Positives)`

**3. Recall (Sensitivity):** Recall calculates the proportion of correctly predicted positive instances out of all actual positive instances. Formula:

`Recall = True Positives / (True Positives + False Negatives)`

**4. Confusion Matrix: **A confusion matrix is a table used to visualize the performance of a classification algorithm. It shows the counts of true positive, true negative, false positive, and false negative predictions.

Code:

`# Calculate accuracy`

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")# Calculate precision

precision = precision_score(y_test, y_pred, average='weighted')

print(f"Precision: {precision:.2f}")

# Calculate recall

recall = recall_score(y_test, y_pred, average='weighted')

print(f"Recall: {recall:.2f}")

# Calculate confusion matrix

conf_matrix = confusion_matrix(y_test, y_pred)

print("Confusion Matrix:")

print(conf_matrix)

**Mean Absolute Error (MAE):**The MAE represents the average absolute difference between the predicted values and the actual values. Formula:

`MAE = (1/n) * Σ | y_i - ŷ_i |`Where:

- n is the number of data points.

- y_i is the actual value for the i-th data point.

- ŷ_i is the predicted value for the i-th data point.

2. **Mean Squared Error (MSE):** The MSE calculates the average squared difference between predicted and actual values:

`MSE = (1/n) * Σ (y_i - ŷ_i)^2`Where:

- n is the number of data points.

- y_i is the actual value for the i-th data point.

- ŷ_i is the predicted value for the i-th data point.

Lower MSE values indicate better performance.

**3. R-squared Score (Coefficient of Determination)**: The R-squared score measures the proportion of variance predictable by the model:

`R^2 = 1 - Σ (y_i - ŷ_i)^2 / Σ (y_i - ȳ)^2`Where:

- n is the number of data points.

- y_i is the actual value for the i-th data point.

- ŷ_i is the predicted value for the i-th data point.

- ȳ is the mean of the actual values.

Higher R^2 values indicate a better fit, ranging from 0 to 1.

Code:

`# Calculate Mean Absolute Error`

mae = mean_absolute_error(y_test, y_pred)

print(f"Mean Absolute Error: {mae:.2f}")# Calculate Mean Squared Error

mse = mean_squared_error(y_test, y_pred)

print(f"Mean Squared Error: {mse:.2f}")

# Calculate R-squared

r2 = r2_score(y_test, y_pred)

print(f"R-squared: {r2:.2f}")

We have covered the most fundamental concepts of Machine Learning. We started with defining machine learning, then proceeded to explore various types of machine learning techniques. We also discussed the most widely-used regression techniques — Linear and Logistic regression — along with their evaluation metrics.

Happy learning!