将 Python 字典转换为 NumPy 数组的不同方法
NumPy 是 Python 中用于执行数值计算和科学计算的流行库之一。它还允许我们使用大型多维数组和矩阵。numpy 库中内置了许多函数和模块,可用于处理不同维度的数组。
将字典转换为 NumPy 数组
我们可以使用 Numpy 库中提供的不同模块和函数将字典转换为 NumPy 数组。让我们逐一看看每种方法。
使用 numpy.array() 函数
在 NumPy 中,我们有一个函数,即 array(),它用于通过传递任何数据结构(如字典、列表等)来创建数组。以下是使用 NumPy 库的 array() 函数将 Python 字典转换为 NumPy 数组的语法。
import numpy numpy.array(dictionary_name)
其中,
numpy 是库的名称。
array 是创建数组。
dictionary_name 是作为字典的输入参数。
示例
在此示例中,我们将使用 array() 函数通过将字典作为输入参数传递来创建 NumPy 数组,然后该字典将转换为 NumPy 数组。
import numpy as np
dic = {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'],
'Age': [1, 30, 25, 1, 55],
'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
print("The dictionary to be converted into array:",dic)
arr = np.array(list(dic.values()))
print("The array created from the dictionary:",arr)
print(type(arr))
输出
The dictionary to be converted into array: {'Name': ['Karthikeya', 'Anil', 'Srivatsav', 'Riyaansh', 'Prasad'], 'Age': [1, 30, 25, 1, 55], 'Skill': ['python', 'Salesforce', 'Networking', 'python', 'Management']}
The array created from the dictionary: [['Karthikeya' 'Anil' 'Srivatsav' 'Riyaansh' 'Prasad']
['1' '30' '25' '1' '55']
['python' 'Salesforce' 'Networking' 'python' 'Management']]
<class 'numpy.ndarray'>
使用 numpy.asarray() 函数
numpy 库中还有另一个函数,即 asarray(),它具有与 array() 函数类似的功能。此函数也将字典值作为输入,并返回 numpy 数组作为输出。以下是使用 asarray() 函数的语法。
np.asarray(dictionary)
示例
在下面的示例中,我们将字典作为输入参数传递给 as array() 函数,以将字典转换为 numpy 数组。
import numpy as np
dic = {'Age': [1, 30, 25, 1, 55],
'year': [2002,2020,1995,1900,2023]}
print("The dictionary to be converted into array:",dic)
arr = np.asarray(list(dic.values()))
print("The array created from the dictionary:",arr)
print(type(arr))
输出
The dictionary to be converted into array: {'Age': [1, 30, 25, 1, 55], 'year': [2002, 2020, 1995, 1900, 2023]}
The array created from the dictionary: [[ 1 30 25 1 55]
[2002 2020 1995 1900 2023]]
<class 'numpy.ndarray'>
使用循环
使用循环,我们可以将字典转换为numpy数组。以下是将字典转换为numpy数组的示例。
import numpy as np
dic = {'Age': 20, 'year': 2023,'class':10}
arr = np.zeros(len(dic))
i = 0
for key in dic:
arr[i] = dic[key]
i += 1
print(arr)
输出
[ 20. 2023. 10.]
使用 numpy.fromiter() 函数
在 Numpy 中,还有另一个函数,即 fromiter(),用于将字典转换为数组。此函数将字典值和数据类型作为输入参数。以下是语法。
numpy.fromiter(dictionary,dtype = datatype)
示例
在此示例中,我们将通过将字典作为输入参数传递给 fromiter() 函数,将字典转换为数组。
import numpy as np
dic = {'Age': 20, 'year': 2023,'class':10}
print("The dictionary to be converted into array:",dic)
arr = np.fromiter(dic.values(),dtype = int)
print("The array created from the dictionary:",arr)
print(type(arr))
输出
The dictionary to be converted into array: {'Age': 20, 'year': 2023, 'class': 10}
The array created from the dictionary: [ 20 2023 10]
<class 'numpy.ndarray'>
使用 numpy.column_stack() 函数
当我们想要将字典转换为多行数组时,我们可以使用 numpy 数组的 column_stack() 函数。以下是语法。
numpy.column_stack(dictionary[key] for key in dictionary)
示例
在这里,我们将字典作为输入参数传递给 column_stack() 函数,然后字典将转换为具有多行的数组。
import numpy as np
dic = {'Age': [20,20], 'year': [2023,2022],'class':[10,9]}
print("The dictionary to be converted into array:",dic)
arr = np.column_stack(dic[key] for key in dic)
print("The array created from the dictionary:",arr)
print(type(arr))
输出
The dictionary to be converted into array: {'Age': [20, 20], 'year': [2023, 2022], 'class': [10, 9]}
The array created from the dictionary: [[ 20 2023 10]
[ 20 2022 9]]
<class 'numpy.ndarray'>
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

