Login
网站首页 > 文章中心 > 其它

numpy的array函数作用_numpy库array函数

作者:小编 更新时间:2023-07-15 12:20:43 浏览量:459人看过

论numpy中matrix 和 array的区别,有需要的朋友可以参考下。

Numpy matrices必须是2维的,但是numpy arrays (ndarrays) 可以是多维的(1D,2D,3D----ND). Matrix是Array的一个小的分支,包含于Array。所以matrix 拥有array的所有特性。

在numpy中matrix的主要优势是:相对简单的乘法运算符号。例如,a和b是两个matrices,那么a*b,就是矩阵积。

import numpy as np

a=np.mat('4 3; 2 1')

b=np.mat('1 2; 3 4')

numpy的array函数作用_numpy库array函数-图1

print(a)

# [[4 3]

# [2 1]]

print(b)

# [[1 2]

# [3 4]]

print(a*b)

# [[13 20]

numpy的array函数作用_numpy库array函数-图2

# [ 5 8]]

matrix 和 array 都可以通过在have.Tto

return the transpose, but matrix objects also have.Hfor

the conjugate transpose, and.Ifor

the inverse.

In contrast, numpy arrays consistently abide by the rule that

operations are applied element-wise. Thus, if a and b are numpy arrays,

then a*b is the array formed by multiplying the components element-wise:

c=np.array([[4, 3], [2, 1]])

d=np.array([[1, 2], [3, 4]])

print(c*d)

# [[4 6]

# [6 4]]

To obtain the result of matrix multiplication, you use np.dot :

print(np.dot(c,d))

The**operator

also behaves differently:

print(a**2)

# [[22 15]

# [10 7]]

print(c**2)

# [[16 9]

# [ 4 1]]

numpy的array函数作用_numpy库array函数-图3

Sinceais

a matrix,a**2returns

the matrix producta*a.

Sincecis

an ndarray,c**2returns

an ndarray with each component squared element-wise.

There are other technical differences between matrix objects and

ndarrays (having to do with np.ravel, item selection and sequence

behavior).

The main advantage of numpy arrays is that they are more general

than 2-dimensional matrices. What happens when you want a 3-dimensional

array? Then you have to use an ndarray, not a matrix object. Thus,

learning to use matrix objects is more work -- you have

to learn matrix object operations, and ndarray operations.

Writing a program that uses both matrices and arrays makes your life

difficult because you have to keep track of what type of object your

variables are, lest multiplication return something you don't expect.

In contrast, if you stick solely with ndarrays, then you can do

everything matrix objects can do, and more, except with slightly

different functions/notation.

If you are willing to give up the visual appeal of numpy matrix

product notation, then I think numpy arrays are definitely the way to

go.

PS. Of course, you really don't have to choose one at the expense of the other, sincenp.asmatrixandnp.asarrayallow

you to convert one to the other (as long as the array is 2-dimensional).

One of the biggest practical differences for me of numpy ndarrays

compared to numpy matrices or matrix languages like matlab, is that the

dimension is not preserved in reduce operations. Matrices are always 2d,

while the mean of an array, for example, has one

dimension less.

For example demean rows of a matrix or array:

with matrix

>>> m = np.mat([[1,2],[2,3]])

>>> m

matrix([[1, 2],

[2, 3]])

>>> mm = m.mean(1)

>>> mm

matrix([[ 1.5],

[ 2.5]])

>>> mm.shape

(2, 1)

>>> m - mm

matrix([[-0.5, 0.5],

[-0.5, 0.5]])

with array

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

>>> a

array([[1, 2],

>>> am = a.mean(1)

>>> am.shape

(2,)

>>> am

array([ 1.5, 2.5])

>>> a - am #wrong

array([[-0.5, -0.5],

[ 0.5, 0.5]])

>>> a - am[:, np.newaxis] #right

array([[-0.5, 0.5],

I also think that mixing arrays and matrices gives rise to many

"happy" debugging hours. However, scipy.sparse matrices are always

matrices in terms of operators like multiplication.

matrix是array的分支,matrix和array在很多时候都是通用的,你用哪一个都一样。但这时候,官方建议大家如果两个可以通用,那就选择array,因为array更灵活,速度更快,很多人把二维的array也翻译成矩阵。

但是matrix的优势就是相对简单的运算符号,比如两个矩阵相乘,就是用符号*,但是array相乘不能这么用,得用方法.dot()

array的优势就是不仅仅表示二维,还能表示3、4、5...维,而且在大部分Python程序里,array也是更常用的。

需求:根据已有的多个列表,利用numpy.array()函数创建三维以上数组

格式概述: 每一维用一个 [] 括起,不同维之间用 , 逗号间隔,最后总体再用 [] 括起!!!

说明 :列表肯定是一维的,多个列表一行一行堆叠形成二维,多个这样的二维构成三维,以此类推可得更高维矩阵(一般3维以上就不用numpy.array()这种方法创建了)。

注意 :高维数组,以三维(5,2,3)为例:前面的5代表页数,即表示(2,3)这样的二维矩阵有5个。即: 前面的数,永远代表比它"低一维"的数组有多少个 !

(1)创建二维数组的例子:

(2)创建三维数组的例子1:(2,3,3)

(3)创建三维数组的例子2:(4,2,3)

补充:最快验证自己创建的数组是否满足自己的维度需求的方式,就是看打印的结果中, 最外面有几个 ] 中括号,有几个 ] 就是几维数组 !如本文中第3个例子,打印结果最外层有3个 ],说明满足3维的要求。

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章