Posted on

Decision Tree Algorithm in Machine Learning: Concepts, Techniques, and Python Scikit Learn Example

decision tree algorithm concepts using scikit-learn in python

Machine learning is a subfield of artificial intelligence that involves the development of algorithms that can learn from data and make predictions or decisions based on patterns learned from the data. Decision trees are one of the most widely used and interpretable machine learning algorithms that can be used for both classification and regression tasks. They are particularly popular in fields such as finance, healthcare, marketing, and customer analytics due to their ability to provide understandable and transparent models.

In this article, we will provide a comprehensive overview of decision trees, covering their concepts, techniques, and practical implementation using Python. We will start by explaining the basic concepts of decision trees, including tree structure, node types, and decision rules. We will then delve into the techniques for constructing decision trees, such as entropy, information gain, and Gini impurity, as well as tree pruning methods for improving model performance. Next, we will discuss feature selection techniques in decision trees, including splitting rules, attribute selection measures, and handling missing values. Finally, we will explore methods for interpreting decision tree models, including model visualization, feature importance analysis, and model explanation.

Important decision tree concepts

Decision trees are tree-like structures that represent decision-making processes or decisions based on the input features. They consist of nodes, edges, and leaves, where nodes represent decision points, edges represent decisions or outcomes, and leaves represent the final prediction or decision. Each node in a decision tree corresponds to a feature or attribute, and the tree is constructed recursively by splitting the data based on the values of the features until a decision or prediction is reached.

Elements of a Decision Tree Algorithm
Elements of a Decision Tree

There are several important concepts to understand in decision trees:

  1. Root Node: The topmost node in a decision tree, also known as the root node, represents the feature that provides the best split of the data based on a selected splitting criterion.
  2. Internal Nodes: Internal nodes in a decision tree represent decision points where the data is split into different branches based on the feature values. Internal nodes contain decision rules that determine the splitting criterion and the branching direction.
  3. Leaf Nodes: Leaf nodes in a decision tree represent the final decision or prediction. They do not have any outgoing edges and provide the output or prediction for the input data based on the majority class or mean/median value, depending on whether it’s a classification or regression problem.
  4. Decision Rules: Decision rules in a decision tree are determined based on the selected splitting criterion, which measures the impurity or randomness of the data. The decision rule at each node determines the feature value that is used to split the data into different branches.
  5. Impurity Measures: Impurity measures are used to determine the splitting criterion in decision trees. Common impurity measures include entropy, information gain, and Gini impurity. These measures quantify the randomness or impurity of the data at each node, and the split that minimizes the impurity is selected as the splitting criterion.

Become a Machine Learning Engineer with Experience and implement decision trees in production environments

Decision Tree Construction Techniques

The process of constructing a decision tree involves recursively splitting the data based on the values of the features until a stopping criterion is met. There are several techniques for constructing decision trees, including entropy, information gain, and Gini impurity.

Entropy

Entropy is a measure of the randomness or impurity of the data at a node in a decision tree. It is defined as the sum of the negative logarithm of the probabilities of all classes in the data, multiplied by their probabilities. The formula for entropy is given as:

Entropy = – Σ p(i) * log2(p(i))

where p(i) is the probability of class i in the data at a node. The goal of entropy-based decision tree construction is to minimize the entropy or maximize the information gain at each split, which leads to a more pure and accurate decision tree.

Information Gain

Information gain is another commonly used criterion for decision tree construction. It measures the reduction in entropy or increase in information at a node after a particular split. Information gain is calculated as the difference between the entropy of the parent node and the weighted average of the entropies of the child nodes after the split. The formula for information gain is given as:

Information Gain = Entropy(parent) – Σ (|Sv|/|S|) * Entropy(Sv)

where Sv is the subset of data after the split based on a particular feature value, and |S| and |Sv| are the total number of samples in the parent node and the subset Sv, respectively. The decision rule that leads to the highest information gain is selected as the splitting criterion.

Gini Impurity

Gini impurity is another impurity measure used in decision tree construction. It measures the probability of misclassification of a randomly chosen sample at a node. The formula for Gini impurity is given as:

Gini Impurity = 1 – Σ p(i)^2

where p(i) is the probability of class i in the data at a node. Similar to entropy and information gain, the goal of Gini impurity-based decision tree construction is to minimize the Gini impurity or maximize the Gini gain at each split.

Become a Machine Learning Engineer with Experience and implement decision trees in production environments

Decision Trees in Python Scikit-Learn (sklearn)

Python provides several libraries for implementing decision trees, such as scikit-learn, XGBoost, and LightGBM. Here, we will illustrate an example of decision tree classifier implementation using scikit-learn, one of the most popular machine learning libraries in Python.

Download the dataset here: Iris dataset uci | Kaggle

# Import the required libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score

# Load the dataset
data = pd.read_csv('iris.csv')  # Load the iris dataset

# Split the dataset into features and labels
X = data.iloc[:, :-1]  # Features
y = data.iloc[:, -1]  # Labels

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Initialize the decision tree classifier
clf = DecisionTreeClassifier()

# Train the decision tree classifier
clf.fit(X_train, y_train)

# Make predictions on the test set
y_pred = clf.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy: {:.2f}%".format(accuracy * 100))

In this example, we load the popular Iris dataset, split it into features (X) and labels (y), and then split it into training and testing sets using the train_test_split function from scikit-learn. We then initialize a decision tree classifier using the DecisionTreeClassifier class from scikit-learn, fit the classifier to the training data using the fit method, and make predictions on the test data using the predict method. Finally, we calculate the accuracy of the decision tree classifier using the accuracy_score function from scikit-learn.

Become a Machine Learning Engineer with Experience and implement decision trees in production environments

Overfitting in Decision Trees and how to prevent overfitting

Overfitting is a common problem in decision trees where the model becomes too complex and captures noise instead of the underlying patterns in the data. As a result, the tree performs well on the training data but poorly on new, unseen data.

To prevent overfitting in decision trees, we can use the following techniques:

Use more data to prevent overfitting

Overfitting can occur when a model is trained on a limited amount of data, causing it to capture noise rather than the underlying patterns. Collecting more data can help the model generalize better, reducing the likelihood of overfitting.

  • Collect more data from various sources
  • Use data augmentation techniques to create synthetic data

Set a minimum number of samples for each leaf node

A leaf node is a terminal node in a decision tree that contains the final classification decision. Setting a minimum number of samples for each leaf node can help prevent the model from splitting the data too finely, which can lead to overfitting.

from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier(min_samples_leaf=5)

Prune and visualize the decision tree

Decision trees are prone to overfitting, which means they can become too complex and fit the training data too closely, resulting in poor generalization performance on unseen data. Pruning is a technique used to prevent overfitting by removing unnecessary branches or nodes from a decision tree.

Pre-pruning

Pre-pruning is a pruning technique that involves stopping the tree construction process before it reaches its maximum depth or minimum number of samples per leaf. This prevents the tree from becoming too deep or too complex, and helps in creating a simpler and more interpretable decision tree. Pre-pruning can be done by setting a maximum depth for the tree, a minimum number of samples per leaf, or a maximum number of leaf nodes.

from sklearn.tree import DecisionTreeClassifier

# Set the maximum depth for the tree
max_depth = 5

# Set the minimum number of samples per leaf
min_samples_leaf = 10

# Create a decision tree classifier with pre-pruning
clf = DecisionTreeClassifier(max_depth=max_depth, min_samples_leaf=min_samples_leaf)

# Fit the model on the training data
clf.fit(X_train, y_train)

# Evaluate the model on the test data
y_pred = clf.predict(X_test)

Post-pruning

Post-pruning is a pruning technique that involves constructing the decision tree to its maximum depth or allowing it to overfit the training data, and then pruning back the unnecessary branches or nodes. This is done by evaluating the performance of the tree on a validation set or using a pruning criterion such as cost-complexity pruning. Cost-complexity pruning involves calculating the cost of adding a new node or branch to the tree, and pruning back the nodes or branches that do not improve the performance significantly.

from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import export_text

# Create a decision tree classifier without pruning
clf = DecisionTreeClassifier()

# Fit the model on the training data
clf.fit(X_train, y_train)

# Evaluate the model on the validation data. This is the baseline score
score = clf.score(X_val, y_val)

# Print the decision tree before pruning
print(export_text(clf))

# Prune the decision tree using cost-complexity pruning
ccp_alphas = clf.cost_complexity_pruning_path(X_train, y_train).ccp_alphas
for ccp_alpha in ccp_alphas:
    pruned_clf = DecisionTreeClassifier(ccp_alpha=ccp_alpha)
    pruned_clf.fit(X_train, y_train)
    pruned_score = pruned_clf.score(X_val, y_val)
    if pruned_score > score:
        score = pruned_score
        clf = pruned_clf

# Print the decision tree after pruning
print(export_text(clf))

Use cross-validation to evaluate model performance

Cross-validation is a technique for evaluating the performance of a model by training and testing it on different subsets of the data. This can help prevent overfitting by testing the model’s ability to generalize to new data.

In this example we use cross_val_score from scikit liearn.

from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier()
scores = cross_val_score(dtc, X, y, cv=10)
print("Cross-validation scores: {}".format(scores))

Limit the depth of the tree

Limiting the depth of the tree can prevent the model from becoming too complex and overfitting to the training data. This can be done by setting a maximum depth or a minimum number of samples required for a node to be split.

from sklearn.tree import DecisionTreeClassifier
dtc = DecisionTreeClassifier(max_depth=5)

Use ensemble methods like random forests or boosting

Ensemble methods combine multiple decision trees to improve the model’s accuracy and prevent overfitting. Random forests create a collection of decision trees by randomly sampling the data and features for each tree, while boosting iteratively trains decision trees on the residual errors of the previous trees.

Here is an example of using the GradientBoostingClassifier from scikit learn.

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

# Generate synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_informative=5, n_classes=2, random_state=42)

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# Fit gradient boosting classifier to training data
gb = GradientBoostingClassifier(n_estimators=100, max_depth=5, learning_rate=0.1, random_state=42)
gb.fit(X_train, y_train)

# Evaluate performance on test data
print("Accuracy: {:.2f}".format(gb.score(X_test, y_test)))

Feature selection and engineering to reduce noise in the data

Feature selection involves selecting the most relevant features for the model, while feature engineering involves creating new features or transforming existing ones to better capture the underlying patterns in the data. This can help reduce noise in the data and prevent the model from overfitting to irrelevant or noisy features.

from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
X_new = SelectKBest(chi2, k=10).fit_transform(X, y)

Feature Selection Techniques in Decision Trees

Feature selection is an important step in machine learning to identify the most relevant features or attributes that contribute the most to the prediction or decision-making process. In decision trees, feature selection is typically done during the tree construction process when determining the splitting criterion. There are several techniques for feature selection in decision trees:

Feature Importance

Decision trees can also provide a measure of feature importance, which indicates the relative importance of each feature in the decision-making process. Feature importance is calculated based on the number of times a feature is used for splitting across all nodes in the tree and the improvement in the impurity measure (such as entropy or Gini impurity) achieved by each split. Features with higher importance values are considered more relevant and contribute more to the decision-making process.

Recursive Feature Elimination

Recursive feature elimination is a technique that recursively removes less important features from the decision tree based on their importance values. The decision tree is repeatedly trained with the remaining features, and the feature with the lowest importance value is removed at each iteration. This process is repeated until a desired number of features or a desired level of feature importance is achieved.

Become a Machine Learning Engineer with Experience and implement decision trees in production environments

Sources

  1. Quinlan, J. R. (1986). Induction of decision trees. Machine learning, 1(1), 81-106. Link: https://link.springer.com/article/10.1007/BF00116251
  2. Hastie, T., Tibshirani, R., & Friedman, J. (2009). The elements of statistical learning: data mining, inference, and prediction. Springer Science & Business Media. Link: https://web.stanford.edu/~hastie/Papers/ESLII.pdf
  3. Bishop, C. M. (2006). Pattern recognition and machine learning. Springer. Link: https://www.springer.com/gp/book/9780387310732
  4. Pedregosa, F., Varoquaux, G., Gramfort, A., Michel, V., Thirion, B., Grisel, O., … & Vanderplas, J. (2011). Scikit-learn: Machine learning in Python. Journal of machine learning research, 12(Oct), 2825-2830. Link: https://jmlr.csail.mit.edu/papers/v12/pedregosa11a.html
  5. Kohavi, R., & Quinlan, J. R. (2002). Data mining tasks and methods: Classification: decision-tree discovery. Handbook of data mining and knowledge discovery, 267-276. Link: https://dl.acm.org/doi/abs/10.1007/978-1-4615-0943-3_19
  6. W. Loh, (2014). Fifty Years of Classification and Regression Trees 1. Link: https://www.semanticscholar.org/paper/Fifty-Years-of-Classification-and-Regression-Trees-Loh/f1c3683cacc3dc7898f3603753af87565f8ad677?p2df

Frequently asked questions about decision trees in machine learning

  1. What is a decision tree in machine learning?

    A decision tree is a graphical representation of a decision-making process or decision rules, where each internal node represents a decision based on a feature or attribute, and each leaf node represents an outcome or decision class.

  2. What are the advantages of using decision trees?

    Decision trees are easy to understand and interpret, can handle both categorical and numerical data, require minimal data preparation, can handle missing values, and are capable of handling both classification and regression tasks.

  3. What are the common splitting criteria used in decision tree algorithms?

    Some common splitting criteria used in decision tree algorithms include Gini impurity, entropy, and information gain, which are used to determine the best attribute for splitting the data at each node.

  4. How can decision trees be used for feature selection?

    Decision trees can be used for feature selection by analyzing the feature importance or feature ranking obtained from the decision tree, which can help identify the most important features for making accurate predictions.

  5. What are the methods to avoid overfitting in decision trees?

    Some methods to avoid overfitting in decision trees include pruning techniques such as pre-pruning (e.g., limiting the depth of the tree) and post-pruning (e.g., pruning the tree after it is fully grown and then removing less important nodes), and using ensemble methods such as random forests and boosting.

  6. What are the limitations of decision trees?

    Some limitations of decision trees include their susceptibility to overfitting, sensitivity to small changes in the data, lack of robustness to noise and outliers, and difficulty in handling continuous or large-scale datasets.

  7. What are the common applications of decision trees in real-world problems?

    Decision trees are commonly used in various real-world problems, including classification tasks such as spam detection, medical diagnosis, and credit risk assessment, as well as regression tasks such as housing price prediction, demand forecasting, and customer churn prediction.

  8. Can decision trees handle missing values in the data?

    Yes, decision trees can handle missing values in the data by using techniques such as surrogate splitting, where an alternative splitting rule is used when the value of a certain attribute is missing for a data point.

  9. Can decision trees be used for multi-class classification problems?

    Yes, decision trees can be used for multi-class classification problems by extending the binary splitting criteria to handle multiple classes, such as one-vs-rest or one-vs-one approaches.

  10. How can I implement decision trees in Python?

    Decision trees can be implemented in Python using popular machine learning libraries such as scikit-learn, TensorFlow, and PyTorch, which provide built-in functions and classes for training and evaluating decision tree models.

  11. Is decision tree a supervised or unsupervised algorithm?

    A decision tree is a supervised learning algorithm that is used for classification and regression modeling.

  12. What is pruning in decision trees?

    Pruning is a technique used in decision tree algorithms to reduce the size of the tree by removing nodes or branches that do not contribute significantly to the accuracy of the model. This helps to avoid overfitting and improve the generalization performance of the model.

  13. What are the benefits of pruning?

    Pruning helps to simplify and interpret the decision tree model by reducing its size and complexity. It also improves the generalization performance of the model by reducing overfitting and increasing accuracy on new, unseen data.

  14. What are the different types of pruning for decision trees?

    There are two main types of pruning: pre-pruning and post-pruning. Pre-pruning involves stopping the tree construction process before it reaches its maximum depth or minimum number of samples per leaf, while post-pruning involves constructing the decision tree to its maximum depth and then pruning back unnecessary branches or nodes.

  15. How is pruning performed in decision trees?

    Pruning can be performed by setting a maximum depth for the tree, a minimum number of samples per leaf, or a maximum number of leaf nodes for pre-pruning. For post-pruning, the model is trained on the training data, evaluated on a validation set, and then unnecessary branches or nodes are pruned based on a pruning criterion such as cost-complexity pruning.

  16. When should decision trees be pruned?

    Pruning should be used when the decision tree model is too complex or overfits the training data. It should also be used when the size of the decision tree becomes impractical for interpretation or implementation.

  17. Are there any drawbacks to pruning?

    One potential drawback of pruning is that it can result in a loss of information or accuracy if too many nodes or branches are pruned. Additionally, pruning can be computationally expensive, especially for large datasets or complex decision trees.

Posted on

SKLEARN LOGISTIC REGRESSION multiclass (more than 2) classification with Python scikit-learn

multiclass logistic regression with sklearn python

Logistic Regression is a commonly used machine learning algorithm for binary classification problems, where the goal is to predict one of two possible outcomes. However, in some cases, the target variable has more than two classes. In such cases, a multiclass classification problem is encountered. In this article, we will see how to create a logistic regression model using the scikit-learn library for multiclass classification problems.

Multinomial classification

Multinomial logistic regression is used when the dependent variable in question is nominal (equivalently categorical, meaning that it falls into any one of a set of categories that cannot be ordered in any meaningful way) and for which there are more than two categories. Some examples would be:

  • Which major will a college student choose, given their grades, stated likes and dislikes, etc.? 
  • Which blood type does a person have, given the results of various diagnostic tests? 
  • In a hands-free mobile phone dialing application, which person’s name was spoken, given various properties of the speech signal? 
  • Which candidate will a person vote for, given particular demographic characteristics? 
  • Which country will a firm locate an office in, given the characteristics of the firm and of the various candidate countries? 

These are all statistical classification problems. They all have in common a dependent variable to be predicted that comes from one of a limited set of items that cannot be meaningfully ordered, as well as a set of independent variables (also known as features, explanators, etc.), which are used to predict the dependent variable. Multinomial logistic regression is a particular solution to classification problems that use a linear combination of the observed features and some problem-specific parameters to estimate the probability of each particular value of the dependent variable. The best values of the parameters for a given problem are usually determined from some training data (e.g. some people for whom both the diagnostic test results and blood types are known, or some examples of known words being spoken).

Common Approaches

  • One-vs-Rest (OvR)
  • Softmax Regression (Multinomial Logistic Regression)
  • One vs One(OvO)

Multiclass classification problems are usually tackled in two ways – One-vs-Rest (OvR), One-vs-One (OvO) and using the softmax function. In the OvA / OvR approach, a separate binary classifier is trained for each class, where one class is considered positive and all other classes are considered negative. In the OvO approach, a separate binary classifier is trained for each pair of classes. For example, if there are k classes, then k(k-1)/2 classifiers will be trained in the OvO approach.

In this article, we will be using the OvR and softmax approach to create a logistic regression model for multiclass classification.

One-vs-Rest (OvR)

One-vs-rest (OvR for short, also referred to as One-vs-All or OvA) is a heuristic method for using binary classification algorithms for multi-class classification.

It involves splitting the multi-class dataset into multiple binary classification problems. A binary classifier is then trained on each binary classification problem and predictions are made using the model that is the most confident.

For example, given a multi-class classification problem with examples for each class ‘red,’ ‘blue,’ and ‘green‘. This could be divided into three binary classification datasets as follows:

  • Binary Classification Problem 1: red vs [blue, green]
  • Binary Classification Problem 2: blue vs [red, green]
  • Binary Classification Problem 3: green vs [red, blue]

A possible downside of this approach is that it requires one model to be created for each class. For example, three classes require three models. This could be an issue for large datasets (e.g. millions of rows), slow models (e.g. neural networks), or very large numbers of classes (e.g. hundreds of classes).

This approach requires that each model predicts a class membership probability or a probability-like score. The argmax of these scores (class index with the largest score) is then used to predict a class.

As such, the implementation of these algorithms in the scikit-learn library implements the OvR strategy by default when using these algorithms for multi-class classification.

Multi class logistic regression using one vs rest (OVR) strategy

The strategy for handling multi-class classification can be set via the “multi_class” argument and can be set to “ovr” for the one-vs-rest strategy when using sklearn’s LogisticRegression class from linear_model.

To start, we need to import the required libraries:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

Next, we will load the load_iris dataset from the sklearn.datasets library, which is a commonly used dataset for multiclass classification problems:

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

The load_iris dataset contains information about the sepal length, sepal width, petal length, and petal width of 150 iris flowers. The target variable is the species of the iris flower, which has three classes – 0, 1, and 2.

Next, we will split the data into training and testing sets. 80%-20% split:

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

Training the multiclass logistic regression model

Now, we can create a logistic regression model and train it on the training data:

model = LogisticRegression(solver='lbfgs', multi_class='ovr')
model.fit(X_train, y_train)

The multi_class parameter is set to ‘ovr’ to indicate that we are using the OvA approach for multiclass classification. The solver parameter is set to ‘lbfgs’ which is a suitable solver for small datasets like the load_iris dataset.

Next, we can evaluate the performance of the model on the test data:

y_pred = model.predict(X_test)
accuracy = np.mean(y_pred == y_test)
print("Accuracy:", accuracy)

The predict method is used to make predictions on the test data, and the accuracy of the predictions is calculated by comparing the predicted values with the actual values.

Finally, we can use the trained model to make predictions on new data:

new_data = np.array([[5.1, 3.5, 1.4, 0.2]])
y_pred = model.predict(new_data)
print("Prediction:", y_pred)

In this example, we have taken a single new data point with sepal length 5.1, sepal width 3.5, petal length 1.4, and petal width 0.2. The model will return the predicted class for this data point.

Become a Machine Learning Engineer with Experience

Softmax Regression (Multinomial Logistic Regression)

The inputs to the multinomial logistic regression are the features we have in the dataset. Suppose if we are going to predict the Iris flower species type, the features will be the flower sepal length, width and petal length and width parameters will be our features. These features will be treated as the inputs for the multinomial logistic regression.

The keynote to remember here is the features values are always numerical. If the features are not numerical, we need to convert them into numerical values using the proper categorical data analysis techniques.

Linear Model

The linear model equation is the same as the linear equation in the linear regression model. You can see this linear equation in the image. Where the X is the set of inputs, Suppose from the image we can say X is a matrix. Which contains all the feature( numerical values) X = [x1,x2,x3]. Where W is another matrix includes the same input number of coefficients W = [w1,w2,w3].

In this example, the linear model output will be the w1x1, w2x2, w3*x3

Softmax Function 

The softmax function is a mathematical function that takes a vector of real numbers as input and outputs a probability distribution over the classes. It is often used in machine learning for multiclass classification problems, including neural networks and logistic regression models.

The softmax function is defined as:

softmax function used for multi class / multinomial logistic regression

The softmax function transforms the input vector into a probability distribution over the classes, where each class is assigned a probability between 0 and 1, and the sum of the probabilities is 1. The class with the highest probability is then selected as the predicted class.

The softmax function is a generalization of the logistic function used in binary classification. In binary classification, the logistic function is used to output a single probability value between 0 and 1, representing the probability of the input belonging to the positive class.

The softmax function is different from the sigmoid function, which is another function used in machine learning for binary classification. The sigmoid function outputs a value between 0 and 1, which can be interpreted as the probability of the input belonging to the positive class.

Cross Entropy

The cross-entropy is the last stage of multinomial logistic regression. Uses the cross-entropy function to find the similarity distance between the probabilities calculated from the softmax function and the target one-hot-encoding matrix.

Cross-entropy is a distance calculation function which takes the calculated probabilities from softmax function and the created one-hot-encoding matrix to calculate the distance. For the right target class, the distance value will be smaller, and the distance values will be larger for the wrong target class.

Multi class logistic regression using sklearn multinomial parameter

Multiclass logistic regression using softmax function (multinomial)

In the previous example, we created a logistic regression model for multiclass classification using the One-vs-All approach. In the softmax approach, the output of the logistic regression model is a vector of probabilities for each class. The class with the highest probability is then selected as the predicted class.

To use the softmax approach with logistic regression in scikit-learn, we need to set the multi_class parameter to ‘multinomial’ and the solver parameter to a solver that supports the multinomial loss function, such as ‘lbfgs’, ‘newton-cg’, or ‘sag’. Here’s an example of how to create a logistic regression model with multi_class set to ‘multinomial’:

import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split

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

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

model = LogisticRegression(solver='lbfgs', multi_class='multinomial')
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
accuracy = np.mean(y_pred == y_test)
print("Accuracy:", accuracy)

new_data = np.array([[5.1, 3.5, 1.4, 0.2]])
y_pred = model.predict(new_data)
print("Prediction:", y_pred)

In this example, we have set the multi_class parameter to ‘multinomial’ and the solver parameter to ‘lbfgs’. The lbfgs solver is suitable for small datasets like the load_iris dataset. We then train the logistic regression model on the training data and evaluate its performance on the test data.

We can also use the predict_proba method to get the probability estimates for each class for a given input. Here’s an example:

probabilities = model.predict_proba(new_data)
print("Probabilities:", probabilities)

In this example, we have used the predict_proba method to get the probability estimates for each class for the new data point. The output is a vector of probabilities for each class.

It’s important to note that the logistic regression model is a linear model and may not perform well on complex non-linear datasets. In such cases, other algorithms like decision trees, random forests, and support vector machines may perform better.

Conclusion

In conclusion, we have seen how to create a logistic regression model using the scikit-learn library for multiclass classification problems using the OvA and softmax approach. The softmax approach can be more accurate than the One-vs-All approach but can also be more computationally expensive. We have used the load_iris dataset for demonstration purposes but the same steps can be applied to any multiclass classification problem. It’s important to choose the right algorithm based on the characteristics of the dataset and the problem requirements.

  1. Can logistic regression be used for multiclass classification?

    Logistic regression is a binary classification model. To support multi-class classification problems, we would need to split the classification problem into multiple steps i.e. classify pairs of classes.

  2. Can you use logistic regression for a classification problem with three classes?

    Yes, we can apply logistic regression on 3 class classification problem. Use One Vs rest method for 3 class classification in logistic regression.

  3. When do I use predict_proba() instead of predict()?

    The predict() method is used to predict the actual class while predict_proba() method can be used to infer the class probabilities (i.e. the probability that a particular data point falls into the underlying classes). It is usually sufficient to use the predict() method to obtain the class labels directly. However, if you wish to futher fine tune your classification model e.g. threshold tuning, then you would need to use predict_proba()

  4. What is softmax function?

    The softmax function is a function that turns a vector of K real values into a vector of K real values that sum to 1. The input values can be positive, negative, zero, or greater than one, but the softmax transforms them into values between 0 and 1, so that they can be interpreted as probabilities. Learn more in this article.

  5. Why and when is Softmax used in logistic regression?

    The softmax function is used in classification algorithms where there is a need to obtain probability or probability distribution as the output. Some of these algorithms are the following: Neural networks. Multinomial logistic regression (Softmax regression)

  6. Why use softmax for classification?

    Softmax classifiers give you probabilities for each class label. It's much easier for us as humans to interpret probabilities to infer the class labels.

Posted on Leave a comment

sklearn Linear Regression in Python with sci-kit learn and easy examples

linear regression sklearn in python

Linear regression is a statistical method used for analyzing the relationship between a dependent variable and one or more independent variables. It is widely used in various fields, such as finance, economics, and engineering, to model the relationship between variables and make predictions. In this article, we will learn how to create a linear regression model using the scikit-learn library in Python.

Scikit-learn (also known as sklearn) is a popular Python library for machine learning that provides simple and efficient tools for data mining and data analysis. It provides a wide range of algorithms and models, including linear regression. In this article, we will use the sklearn library to create a linear regression model to predict the relationship between two variables.

Before we dive into the code, let’s first understand the basic concepts of linear regression.

Understanding Linear Regression

Linear regression is a method that models the relationship between a dependent variable (also known as the response variable or target variable) and one or more independent variables (also known as predictor variables or features). The goal of linear regression is to find the line of best fit that best predicts the dependent variable based on the independent variables.

In a simple linear regression, the relationship between the dependent variable and the independent variable is represented by the equation:

y = b0 + b1x

where y is the dependent variable, x is the independent variable, b0 is the intercept, and b1 is the slope.

The intercept b0 is the value of y when x is equal to zero, and the slope b1 represents the change in y for every unit change in x.

In multiple linear regression, the relationship between the dependent variable and multiple independent variables is represented by the equation:

y = b0 + b1x1 + b2x2 + ... + bnxn

where y is the dependent variable, x1, x2, …, xn are the independent variables, b0 is the intercept, and b1, b2, …, bn are the slopes.

Creating a Linear Regression Model in Python

Now that we have a basic understanding of linear regression, let’s dive into the code to create a linear regression model using the sklearn library in Python.

The first step is to import the necessary libraries and load the data. We will use the pandas library to load the data and the scikit-learn library to create the linear regression model.

Become a Data Analyst with Work Experience

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

Next, we will load the data into a pandas DataFrame. In this example, we will use a simple dataset that contains the height and weight of a group of individuals. The data consists of two columns, the height in inches and the weight in pounds. The goal is to fit a linear regression model to this data to find the relationship between the height and weight of individuals. The data can be represented in a 2-dimensional array, where each row represents a sample (an individual), and each column represents a feature (height and weight). The X data is the height of individuals and the y data is their corresponding weight.

height (inches)weight (pounds)
65150
70170
72175
68160
71170
Heights and Weights of Individuals for a Linear Regression Model Exercise
# Load the data
df = pd.read_excel('data.xlsx')

Next, we will split the data into two arrays: X and y. X contains the independent variable (height) and y contains the dependent variable (weight).

# Split the data into X (independent variable) and y (dependent variable)
X = df['height'].values.reshape(-1, 1)
y = df['weight'].values

It’s always a good idea to check the shape of the data to ensure that it has been loaded correctly. We can use the shape attribute to check the shape of the arrays X and y.

# Check the shape of the data
print(X.shape)
print(y.shape)

The output should show that X has n rows and 1 column and y has n rows, where n is the number of samples in the dataset.

Perform simple cross validation

One common method for performing cross-validation on the data is to split the data into training and testing sets using the train_test_split function from the model_selection module of scikit-learn.

In this example, the data is first split into the X data, which is the height of individuals, and the y data, which is their corresponding weight. Then, the train_test_split function is used to split the data into training and testing sets. The test_size argument specifies the proportion of the data to use for testing, and the random_state argument sets the seed for the random number generator used to split the data.

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

Train the linear regression model

Now that we have split the data into X and y, we can create a linear regression model using the LinearRegression class from the scikit-learn library. This same package is used to load and train the logistic regression model for classification. Learn more here.

# Create a linear regression model
reg = LinearRegression()

Next, we will fit the linear regression model to the data using the fit method.

# Fit the model to the data
reg.fit(X_train, y_train)

After fitting the model, we can access the intercept and coefficients using the intercept_ and coef_ attributes, respectively.

# Print the intercept and coefficients
print(reg.intercept_)
print(reg.coef_)

The intercept and coefficients represent the parameters b0 and b1 in the equation y = b0 + b1x, respectively.

Finally, we can use the predict method to make predictions for new data.

# Make predictions for new data
new_data = np.array([[65]]) # Height of 65 inches
prediction = reg.predict(new_data)
print(prediction)

This will output the predicted weight for a person with a height of 65 inches.

Cost functions for linear regression models

There are several cost functions that can be used to evaluate the linear regression model. Here are a few common ones:

  1. Mean Squared Error (MSE): MSE is the average of the squared differences between the predicted values and the actual values. The lower the MSE, the better the fit of the model. MSE is expressed as:
MSE = 1/n * Σ(y_i - y_i_pred)^2

where n is the number of samples, y_i is the actual value, and y_i_pred is the predicted value.

  1. Root Mean Squared Error (RMSE): RMSE is the square root of MSE. It is expressed as:
RMSE = √(1/n * Σ(y_i - y_i_pred)^2)
  1. Mean Absolute Error (MAE): MAE is the average of the absolute differences between the predicted values and the actual values. The lower the MAE, the better the fit of the model. MAE is expressed as:
MAE = 1/n * Σ|y_i - y_i_pred|
  1. R-Squared (R^2) a.k.a the coefficient of determination: R^2 is a measure of the goodness of fit of the linear regression model. It is the proportion of the variance in the dependent variable that is predictable from the independent variable. The R^2 value ranges from 0 to 1, where a value of 1 indicates a perfect fit and a value of 0 indicates a poor fit.

In scikit-learn, these cost functions can be easily computed using the mean_squared_error, mean_absolute_error, and r2_score functions from the metrics module. For example:

from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

y_pred = model.predict(X_test)

# Mean Squared Error
mse = mean_squared_error(y_test, y_pred)
print("Mean Squared Error:", mse)

# Root Mean Squared Error
rmse = mean_squared_error(y_test, y_pred, squared = False)
print("Root Mean Squared Error:", rmse)

# Mean Absolute Error
mae = mean_absolute_error(y_test, y_pred)
print("Mean Absolute Error:", mae)

# R-Squared
r2 = r2_score(y_test, y_pred)
print("R-Squared:", r2)

These cost functions provide different perspectives on the performance of the linear regression model and can be used to choose the best model for a given problem.

Conclusion

In this article, we learned how to create a linear regression model using the scikit-learn library in Python. We first split the data into X and y, created a linear regression model, fit the model to the data, and finally made predictions for new data.

Linear regression is a simple and powerful method for analyzing the relationship between variables. By using the scikit-learn library in Python, we can easily create and fit linear regression models to our data and make predictions.

Frequently Asked Questions about Linear Regression with Sklearn in Python

  1. Which Python library is best for linear regression?

    scikit-learn (sklearn) is one of the best Python libraries for statistical analysis and machine learning and it is adapted for training models and making predictions. It offers several options for numerical calculations and statistical modelling. LinearRegression is an important sub-module to perform linear regression modelling.

  2. What is linear regression used for?

    Linear regression analysis is used to predict the value of a target variable based on the value of one or more independent variables. The variable you want to predict / explain is called the dependent or target variable. The variable you are using to predict the dependent variable's value is called the independent or feature variable.

  3. What are the 2 most common models of regression analysis?

    The most common models are simple linear and multiple linear. Nonlinear regression analysis is commonly used for more complicated data sets in which the dependent and independent variables show a nonlinear relationship. Regression analysis offers numerous applications in various disciplines.

  4. What are the advantages of linear regression?

    The biggest advantage of linear regression models is linearity: It makes the estimation procedure simple and, most importantly, these linear equations have an easy to understand interpretation on a modular level (i.e. the weights).

  5. What is the difference between correlation and linear regression?

    Correlation quantifies the strength of the linear relationship between a pair of variables, whereas regression expresses the relationship in the form of an equation.

  6. What is LinearRegression in Sklearn?

    LinearRegression fits a linear model with coefficients w = (w1, …, wp) to minimize the residual sum of squares between the observed targets in the dataset, and the targets predicted by the linear approximation.

  7. What is the full form of sklearn?

    scikit-learn (also known as sklearn) is a free software machine learning library for the Python programming language.

  8. What is the syntax for linear regression model in Python?

    from sklearn.linear_model import LinearRegression
    lr = LinearRegression()
    lr.fit(X,y)
    lr.score()
    lr.predict(new_data)

Posted on Leave a comment

Basic Python Data Structures – Lists, tuples, sets, dictionaries

lists tuples sets dictionaries python

List methods

  • list.append(x)
    Add an item to the end of the list. Equivalent to a[len(a):] = [x].
  • list.extend(iterable)
    Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
  • list.insert(i, x)
    Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
  • list.remove(x)
    Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.
  • list.pop([i])
    Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
  • list.clear()
    Remove all items from the list. Equivalent to del a[:].
  • list.index(x[, start[, end]])
    Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
  • list.count(x)
    Return the number of times x appears in the list.
  • list.sort(*, key=None, reverse=False)
    Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
  • list.reverse()
    Reverse the elements of the list in place.
  • list.copy()
    Return a shallow copy of the list. Equivalent to a[:].

Tuple methods

  • tuple.count()
    Returns the number of times a specified value occurs in a tuple.
  • tuple.index()
    Searches the tuple for a specified value and returns the position of where it was found.

Set methods

  • set.add()
    Adds an element to the set.
  • set.clear()
    Removes all the elements from the set.
  • set.copy()
    Returns a copy of the set.
  • set.difference()
    Returns a set containing the difference between two or more sets.
  • set.difference_update()
    Removes the items in this set that are also included in another, specified set.
  • set.discard()
    Remove the specified item.
  • set.intersection()
    Returns a set, that is the intersection of two or more sets.
  • set.intersection_update()
    Removes the items in this set that are not present in other, specified set(s).
  • set.isdisjoint()
    Returns whether two sets have a intersection or not.
  • set.issubset()
    Returns whether another set contains this set or not.
  • set.issuperset()
    Returns whether this set contains another set or not.
  • set.pop()
    Removes an element from the set.
  • set.remove()
    Removes the specified element.
  • set.symmetric_difference()
    Returns a set with the symmetric differences of two sets.
  • set.symmetric_difference_update()
    Inserts the symmetric differences from this set and another.
  • set.union()
    Return a set containing the union of sets.
  • set.update()
    Update the set with another set, or any other iterable.

Dictionary methods

  • dict.clear()
    Removes all the elements from the dictionary.
  • dict.copy()
    Returns a copy of the dictionary.
  • dict.fromkeys()
    Returns a dictionary with the specified keys and value.
  • dict.get()
    Returns the value of the specified key.
  • dict.items()
    Returns a list containing a tuple for each key value pair.
  • dict.keys()
    Returns a list containing the dictionary’s keys.
  • dict.pop()
    Removes the element with the specified key.
  • dict.popitem()
    Removes the last inserted key-value pair.
  • dict.setdefault()
    Returns the value of the specified key. If the key does not exist: insert the key, with the specified value.
  • dict.update()
    Updates the dictionary with the specified key-value pairs.
  • dict.values()
    Returns a list of all the values in the dictionary.