Python 矩阵中的唯一值

Python 矩阵中的唯一值

Python被世界各地的不同程序员用于不同的目的。Python的不同应用领域包括Web开发、数据科学、机器学习以及各种自动化过程。要继续使用各种矩阵进行工作,了解矩阵中不同的值是非常重要的。在Python中,传统的表示矩阵的方式是将其作为一个列表的列表,其中每个内部列表对应矩阵的一行。矩阵的组成部分可以是任何数据类型,包括文本、浮点数甚至整数。在本文中,我们将学习如何找到矩阵中的唯一值。

找到矩阵唯一值的不同方法

集合

这是一种非常简单的方法来找到矩阵的唯一值。我们可以将数据集简单地转换成集合,该集合将删除所有公共元素,然后我们可以很容易地找到唯一值。让我们通过一个例子来更好地理解它:

示例

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

输出

上述示例的输出结果如下:

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

Numpy

这是Python中一个非常常用的库,用于处理不同数值。我们将使用numpy库中的一个函数来查找矩阵的唯一值。该方法的示例如下:

示例

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)

输出

上述示例的输出如下:

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

列表解析

列表解析用于有效地检查列表中的每个元素。在这种方法中,我们将矩阵转换为列表,并且在完成相同操作之后,我们将找到其唯一的值。让我们举一个例子来更好地理解:

示例

# 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

输出

上述示例的输出结果如下:

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

迭代工具

这是一个具有不同功能集的库,用于以高效的方式检查列表中是否存在不同元素。我们将使用迭代工具库的函数来查找矩阵中的唯一值。让我们通过一个示例来更好地理解:

示例

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)

输出

上述示例的输出如下:

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

熊猫图书馆

这种方法在程序员需要处理大量数据和多个不同矩阵的情况下使用得很少。我们将使用熊猫图书馆的函数来查找矩阵的唯一值。让我们通过一个例子来更好地理解它:

示例

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

输出

上面示例的输出如下:

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

结论

了解可以用于查找矩阵唯一值的不同方法非常重要。上述文章描述了可以用来查找矩阵中唯一值的不同方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程