Python - Unique Values in Matrix


Python is used for different purpose by different programmers spread all over the world. The different field of application of python are Web Development, data science, machine learning and also to perform various processes with automation. To continue working with various matrices it is very important to have knowledge about different value present in matrix. The traditional way to express a matrix in Python is as a list of lists, where each inner list corresponds to a row of the matrix. The matrix's components can be of any data type, including texts, floats, and even integers. In this article we will learn how to find unique values in matrix.

Different Method to Find Unique Value of Matrix

Set

This is a very easy method to find unique values of matrix. We can simply convert the data set into set which remove all the common elements and then we can find the unique values very easily. Let’s take an example to understand it in a better way:

Example

def unique_values_of_matrix(data): # The data is given as input to the function unique_values_of_matrix
    different_values = set() # An empty set is created
    for row in data:
        for element in row:  # We check each element in the input given and then all the unique elements are added to the new set created (different_values)
            different_values.add(element)
    return different_values

# Example usage
Names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'Alaric'],
    ['Stefen', 'Damon', 'Tyler']
]  # The input of the data is given

unique_values = unique_values_of_matrix(Names)  # The function unique_values_of_matrix is run
print(unique_values)  # The output will display all the unique values present in the matrix

Output

The output of the above example is as follows:

{'Stefen', 'Tyler', 'Matt', 'Alaric', 'Jack', 'Damon', 'John', 'Sam', 'Daniel'}} 

Numpy

This is a very commonly used library in python used for the purpose of working with different numerical. We will use one function from the numpy library to find the unique value of the matrix. The example of this method is as follows:

Example

import numpy as np  # Do not forget to import numpy or else error might occur

# Example 
Names = np.array([
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'Alaric'],
    ['Stefen', 'Damon', 'Tyler']
]) # np.array defines the matrix data as array

different_values = np.unique(Names) # np.unique helps to find the unique values from the matrix
print(different_values)

Output

The output of the above example will be as follows:

['Alaric' 'Damon' 'Daniel' 'Jack' 'John' 'Matt' 'Sam' 'Stefen' 'Tyler']

List Comprehension

List comprehension is used to check each element in the list effectively. In this method we will convert the matrix into list and after doing the same, we will find its unique values. Let’s take an example to understand it in a better way:

Example

# Example 
Names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'Alaric'],
    ['Stefen', 'Damon', 'Tyler']
]  # The input of the data is given

different_values = list({element for row in Names for element in row}) # With the help of list comprehension we check all the unique values in the matrix and then convert it back into a list
print(different_values) # The different unique values present in the list will be displayed

Output

The output of the above example will be as follows:

['Alaric', 'Damon', 'Matt', 'John', 'Jack', 'Daniel', 'Stefen', 'Sam', 'Tyler']

Itertools

This is a library with different set of functions which used for checking of different elements present in a list with efficiency. We will use the functions of itertools library to find the unique values in the matrix. Let’s take an example to understand it in a better way:

Example

from itertools import chain  # Do not forget to import itertools or else error might occur

# Example 
Names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'Alaric'],
    ['Stefen', 'Damon', 'Tyler']
]  # The input of the data is given

different_values = set(chain(*Names)) # The chain function of the itertool library is used to merge all the data from the matrix and flatten it and the set function is used to find the unique values of the matrix
print(different_values)

Output

The output of the above example will be as follows:

{'Tyler', 'Jack', 'Stefen', 'Alaric', 'Sam', 'Damon', 'Daniel', 'Matt', 'John'}

Panda Library

This method is used in rare cases when the programmer has to work with large amount of data in the matrix and many different matrices. We will use the functions of the panda’s library to find the unique values of matrix. Let’s take an example to understand it in a better way:

Example

import pandas as pd  # Do not forget to import the pandas library or else error might occur

# Example matrix
Names = [
    ['John', 'Sam', 'Daniel'],
    ['Jack', 'Matt', 'Alaric'],
    ['Stefen', 'Damon', 'Tyler']
]  # The input of the data is given

new_dataframe = pd.DataFrame(Names)  # A new data frame is created from the input
different_values = new_dataframe.stack().unique() # Stack function will help to stack the data into one place and unique() function will find the unique values
print(different_values) # The different unique values present in the list will be displayed

Output

The output of the above example is as follows:

['John' 'Sam' 'Daniel' 'Jack' 'Matt' 'Alaric' 'Stefen' 'Damon' 'Tyler']

Conclusion

It is very important to have knowledge of the different methods that can be used to find the unique values of the matrix. The above article describes different methods that can be followed to find the unique values present in the matrix.

Updated on: 01-Aug-2023

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements