搜索
首页后端开发Python教程Python中怎样把矩阵转换为列表

这次给大家带来Python中怎样把矩阵转换为列表,Python中把矩阵转换为列表的注意事项有哪些,下面就是实战案例,一起来看一下。

这篇文章主要介绍Python的numpy库中的一些函数,做备份,以便查找。

(1)将矩阵转换为列表的函数:numpy.matrix.tolist()

返回list列表

Examples

>>> x = np.matrix(np.arange(12).reshape((3,4))); x
matrix([[ 0, 1, 2, 3],
  [ 4, 5, 6, 7],
  [ 8, 9, 10, 11]])
>>> x.tolist()
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]

(2)将数组转换为列表的函数:numpy.ndarray.tolist()

Notes:(数组能够被重新构造)

The array may be recreated, a=np.array(a.tolist()).

Examples

>>>

>>> a = np.array([1, 2])
>>> a.tolist()
[1, 2]
>>> a = np.array([[1, 2], [3, 4]])
>>> list(a)
[array([1, 2]), array([3, 4])]
>>> a.tolist()
[[1, 2], [3, 4]]

(3)numpy.mean()计算矩阵或数组的均值:

Examples

>>>

>>> a = np.array([[1, 2], [3, 4]]) #对所有元素求均值
>>> np.mean(a)
2.5
>>> np.mean(a, axis=0) #对每一列求均值
array([ 2., 3.])
>>> np.mean(a, axis=1) #对每一行求均值
array([ 1.5, 3.5])

(4)numpy.std()计算矩阵或数组的标准差:

Examples

>>>

>>> a = np.array([[1, 2], [3, 4]]) #对所有元素求标准差 
>>> np.std(a)
1.1180339887498949
>>> np.std(a, axis=0) #对每一列求标准差
array([ 1., 1.])
>>> np.std(a, axis=1) #对每一行求标准差
array([ 0.5, 0.5])

(5)numpy.newaxis为数组增加一个维度:

Examples:

>>> a=np.array([[1,2,3],[4,5,6],[7,8,9]]) #先输入3行2列的数组a
>>> b=a[:,:2] 
>>> b.shape #当数组的行与列都大于1时,不需增加维度
(3, 2)
>>> c=a[:,2] 
>>> c.shape #可以看到,当数组只有一列时,缺少列的维度
(3,)
>>> c
array([3, 6, 9])
>>> d=a[:,2,np.newaxis] #np.newaxis实现增加列的维度
>>> d
array([[3],
  [6],
  [9]])
>>> d.shape  #d的维度成了3行1列(3,1)
(3, 1)
>>> e=a[:,2,None] #None与np.newaxis实现相同的功能
>>> e
array([[3],
  [6],
  [9]])
>>> e.shape
(3, 1)

(6)numpy.random.shuffle(index): 打乱数据集(数组)的顺序:

Examples:

>>> index = [i for i in range(10)] 
>>> index 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
>>> np.random.shuffle(index) 
>>> index 
[7, 9, 3, 0, 4, 1, 5, 2, 8, 6]

(7)计算二维数组某一行或某一列的最大值最小值:

>>> import numpy as np 
>>> a = np.arange(15).reshape(5,3) #构造一个5行3列的二维数组 
>>> a 
array([[ 0, 1, 2], 
  [ 3, 4, 5], 
  [ 6, 7, 8], 
  [ 9, 10, 11], 
  [12, 13, 14]]) 
>>> b = a[:,0].min() ##取第0列的最小值,其他列同理 
>>> b 
0 
>>> c = a[0,:].max() ##取第0行的最大值,其他行同理 
>>> c 
2

(8)向数组中添加列:np.hstack()

n = np.array(np.random.randn(4,2)) 
n 
Out[153]: 
array([[ 0.17234 , -0.01480043], 
  [-0.33356669, -1.33565616], 
  [-1.11680009, 0.64230761], 
  [-0.51233174, -0.10359941]]) 
l = np.array([1,2,3,4]) 
l 
Out[155]: array([1, 2, 3, 4]) 
l.shape 
Out[156]: (4,)

可以看到,n是二维的,l是一维的,如果直接调用np.hstack()会出错:维度不同。

n = np.hstack((n,l)) 
ValueError: all the input arrays must have same number of dimensions

解决方法是将l变为二维的,可以用(5)中的方法:

n = np.hstack((n,l[:,np.newaxis])) ##注意:在使用np.hstack()时必须用()把变量括起来,因为它只接受一个变量 
n 
Out[161]: 
array([[ 0.17234 , -0.01480043, 1.  ], 
  [-0.33356669, -1.33565616, 2.  ], 
  [-1.11680009, 0.64230761, 3.  ], 
  [-0.51233174, -0.10359941, 4.  ]])

下面讲一下如何按列往一个空列表添加值:

n = np.array([[1,2,3,4,5,6],[11,22,33,44,55,66],[111,222,333,444,555,666]]) ##产生一个三行六列容易区分的数组 
n 
Out[166]: 
array([[ 1, 2, 3, 4, 5, 6], 
  [ 11, 22, 33, 44, 55, 66], 
  [111, 222, 333, 444, 555, 666]]) 
 
sample = [[]for i in range(3)] ##产生三行一列的空列表 
Out[172]: [[], [], []] 
for i in range(0,6,2): ##每间隔一列便添加到sample中 
 sample = np.hstack((sample,n[:,i,np.newaxis]))  
sample 
Out[170]: 
array([[ 1., 3., 5.], 
  [ 11., 33., 55.], 
  [ 111., 333., 555.]])

持续更新中……

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

在python中列表,数组,矩阵互相转换的方法

Python怎样求得最大公约数

以上是Python中怎样把矩阵转换为列表的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python的科学计算中如何使用阵列?Python的科学计算中如何使用阵列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何处理同一系统上的不同Python版本?您如何处理同一系统上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

与标准Python阵列相比,使用Numpy数组的一些优点是什么?与标准Python阵列相比,使用Numpy数组的一些优点是什么?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造

阵列的同质性质如何影响性能?阵列的同质性质如何影响性能?Apr 25, 2025 am 12:13 AM

数组的同质性对性能的影响是双重的:1)同质性允许编译器优化内存访问,提高性能;2)但限制了类型多样性,可能导致效率低下。总之,选择合适的数据结构至关重要。

编写可执行python脚本的最佳实践是什么?编写可执行python脚本的最佳实践是什么?Apr 25, 2025 am 12:11 AM

到CraftCraftExecutablePythcripts,lollow TheSebestPractices:1)Addashebangline(#!/usr/usr/bin/envpython3)tomakethescriptexecutable.2)setpermissionswithchmodwithchmod xyour_script.3)

Numpy数组与使用数组模块创建的数组有何不同?Numpy数组与使用数组模块创建的数组有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

Numpy数组的使用与使用Python中的数组模块阵列相比如何?Numpy数组的使用与使用Python中的数组模块阵列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模块与Python中的数组有何关系?CTYPES模块与Python中的数组有何关系?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版