search
HomeBackend DevelopmentPython TutorialMethods to convert matrices into lists and other functions in Python's numpy library_python

Below I will share with you a method for converting matrices into lists and other functions in Python's numpy library. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together

This article mainly introduces some functions in Python’s numpy library and makes a backup for easy search.

(1) Function to convert matrix to list: numpy.matrix.tolist()

Return list 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) Convert the array Function to convert to list: numpy.ndarray.tolist()

#Notes: (The array can be reconstructed)

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() calculates the mean of a matrix or array:

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() calculates the standard deviation of a matrix or array:

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 adds a dimension to the array:

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): Disrupt the order of the data set (array):

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) Calculate a certain row or a certain number of two-dimensional array Maximum and minimum value of a column:

>>> 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) Add columns to the array: 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,)

As you can see, n is two-dimensional and l is one-dimensional. If you call np.hstack( directly ) will give an error: the dimensions are different.

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

The solution is to change l into two-dimensional, you can use the method in (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.  ]])

Let’s talk about how to add values ​​to an empty list by column:

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.]])

Continuously updating...

Related recommendations:


Python's numpy library

Python NumPy library installation and usage notes

The above is the detailed content of Methods to convert matrices into lists and other functions in Python's numpy library_python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
详细讲解Python之Seaborn(数据可视化)详细讲解Python之Seaborn(数据可视化)Apr 21, 2022 pm 06:08 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

详细了解Python进程池与进程锁详细了解Python进程池与进程锁May 10, 2022 pm 06:11 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

Python自动化实践之筛选简历Python自动化实践之筛选简历Jun 07, 2022 pm 06:59 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

归纳总结Python标准库归纳总结Python标准库May 03, 2022 am 09:00 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于标准库总结的相关问题,下面一起来看一下,希望对大家有帮助。

Python数据类型详解之字符串、数字Python数据类型详解之字符串、数字Apr 27, 2022 pm 07:27 PM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

分享10款高效的VSCode插件,总有一款能够惊艳到你!!分享10款高效的VSCode插件,总有一款能够惊艳到你!!Mar 09, 2021 am 10:15 AM

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

详细介绍python的numpy模块详细介绍python的numpy模块May 19, 2022 am 11:43 AM

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。

python中文是什么意思python中文是什么意思Jun 24, 2019 pm 02:22 PM

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools