# Python Scipy Softmax – [Detailed Guide]

We are going to learn about the “Python Scipy Softmax” where we will use the softmax function on the given array or the specific axis of the array, and also cover the following topics.

• What is the softmax function?
• Python Scipy Softmax
• How to apply the Scipy softmax function along the columns
• How to use the Scipy softmax function along the rows
• How to compute the log softmax of the data or array
• How to perform log softmax on the axis of the array

## What is the softmax function?

A vector of K real numbers is transformed into a probability distribution of K possible outcomes using the softmax function, also known as softargmax or the normalized exponential function.

Multinomial logistic regression uses it as an expansion of the logistic function to several dimensions. In order to normalize a neural network’s output to a probability distribution over projected output classes based on Luce’s choice axiom, the softmax function is frequently utilized as the final activation function.

A vector z of K real numbers is provided as input by the softmax function, which normalises it into a probability distribution with K probabilities that are proportional to the exponentials of the input values.

In other words, some vector components before applying softmax might be negative or greater than one, and they might not add up to 1. However, after applying softmax, each component will be in the range {(0,1)(0,1)}, and the components will add up to 1, so they can be interpreted as probabilities. Additionally, higher probabilities will follow from greater input components.

In the next subsection, we will learn how to use the softmax function using the Python Scipy method.

## Python Scipy Softmax

A method called `softmax()` in the Python Scipy module `scipy.special` modifies each element of an array by dividing the exponential of each element by the sum of the exponentials of all the elements.

The syntax is given below.

``scipy.special.softmax(x, axis=0)``

Where parameters are:

• x(array_data): It is the array of data as input.
• axis(None, int): Calculate values along the specified axis.

The result of the `softmax() `method is `s`, which is an array of the same size as x. The result will equal one of type ndarray along the chosen axis.

Using the Python code listed below, import the necessary libraries.

``````from scipy import special
import numpy as np``````

Use the code in the next section to create a number array.

``````array_num = np.array([[2, 0.2, 0.5, 1],
[2,  7,   1, -1],
[12,  3,  2, 10]])``````

Using the code below, apply the softmax function to the entire array to alter each value.

``special.softmax(array_num)``

This is how to apply the softmax on the given array of data using the method `softmax()` of Python Scipy.

## Python Scipy Softmax Axis Columns

The Python Scipy method `softmax()` accepts a parameter to `axis` for modifying each element of an array by dividing the exponential of each element by the sum of the exponentials of all the elements, along the specific axis like rows and columns.

Here in this section, we will apply the softmax along the axis =0 which represents the columns.

Using the Python code listed below, import the necessary libraries.

``````from scipy import special
import numpy as np``````

Use the code in the next section to create a number array.

``````array_num = np.array([[12, 0.5, 0.5, 1],
[2,  5,   1, -1],
[2,  8,  2, 10]])``````

Using the code below, apply the softmax function to the columns (which is axis =0 ) to alter each value.

``special.softmax(array_num,axis=0)``

This is how to compute the softmax transformation along the columns or axis = 0 using the parameter `axis` of method `softmax()` of Python Scipy.

## Python Scipy Softmax Axis Rows

We have already learned how to apply the softmax transformation along the column using the parameter `axis` of method `sofmax()`.

Here in this section, we will apply the softmax along the axis =1 which represents the rows.

Using the Python code listed below, import the necessary libraries.

``````from scipy import special
import numpy as np``````

Use the code in the next section to create a number array.

``````array_num = np.array([[12, 0.5, 0.5, 1],
[2,  5,   1, -1],
[2,  8,  2, 10]])``````

Using the code below, apply the softmax function to the rows (which is axis =1 ) to alter each value.

``special.softmax(array_num,axis=1)``

This is how to compute the softmax transformation along the rows or axis = 1 using the parameter `axis` of method `softmax()` of Python Scipy.

## Python Scipy Log Softmax

In mathematics, log softmax is the log of the softmax function. The usage of log probabilities over probabilities—a log probability is just a probability’s logarithm—is at the core of employing log-softmax over softmax.

Using log softmax instead of softmax has a number of benefits, such as enhanced gradient optimization and numerical performance. The Python Scipy has a method `log_softmax()` in a module `scipy.special` that calculate the softmax function’s logarithm.

The syntax is given below.

``scipy.special.log_softmax(x, axis=None)``

Where parameters are:

• x(array_data): It is input array.
• axis(int, tuple of ints): Value-computed along the axis. Softmax will be calculated across the full array x by default, which is None.

The method `log_softmax()` returns `s` (an array that has the shape of x’s form. Along the designated axis, the exponential of the result will add up to 1. A scalar is given back if x is a scalar) of type scalar or ndrray.

Let’s take an example and compute the log softmax of the array by following the below steps:

Using the Python code listed below, import the necessary libraries.

``````from scipy import special
import numpy as np``````

Use the code in the next section to create a number array.

``x_data = np.array([1500.0, 2.0])``

Using the code below, apply the log softmax function.

``log_softmax(x_data)``

This is how to compute the log softmax of the given array using the method `log_softmax()` of Python Scipy.

## Python Scipy Log Softmax Axis

We will use the parameter `axis` of the method `log_softmax()` of Python Scipy to apply the log softmax along the specific axis like rows and columns.

Let’s take an example by following the below steps:

Import the required libraries or methods using the below python code.

``````from scipy import special
import numpy as np``````

Create an array which is the real GDP growth percentage of the USA states `California = 7.8`, `Texas = 5.6`, `New York = 5.0` and `Florida = 6.9` using the below code.

``usa_state = np.array([7.8,5.6,5.0,6.9])``

Now apply the log softmax function along the axis = 0 to the created array using the below code.

``special.log_softmax(usa_state, axis = 0)``
This is how to apply the log softmax function to the data using the method `log_softmax()` of Python Scipy.