基于Python3的科学运算与常用算法-第1,2章

第一章 开篇说明写作的中心思想、路线图、内容

本系列是使用Ubuntu 64位操作系统上,anaconda环境下,采用jupyter notebook 实现python3科学计算的文章,应用于青岛科技大学信息学院科学计算与数据挖掘等多个课程中python教学。需要安装的模块,numpy ,matplotlib,scipy,sympy,pandas等科学计算常用模块。由于匆忙成稿,个人能力所限,显得粗糙,并且有几个地方是错误的。主要参考了Numerical Python 2nd Edition

中心思想

路线图

内容

Python科学计算环境要求

第二章 基本数据与基本运算

其中的加减乘除四则运算,完全像笔记本能给予你演算能力。你只需要用进行如下的运算即可:

4*6
24
3/9
0.3333333333333333

还可以通过如下的方式查看输入和输出

In[58]
'4*6'

Out[59]
0.3333333333333333

对比如下两个等式,你会发现输出一个给出了计算结果,一个结果被隐藏了,没有在输出中显示

3+6
9
3+6 ;

赋值操作

x =9 ; x
9

TAB键,具有提示作用,始终是很有用的。

import os
os.wait

问号?的作用

import math
math.cos?
#ls #文件列表
files = !ls
len(files)
89
#files #释放后可以看到当前文件夹下列表。

#这对于文档搜索,查找很有帮助

调用外部的python 文档和外部的C编写的程序

调用外部的python程序

#python fib.py #jupyter错误的调用,但控制台调用正确。
#fib.py
from fib import *
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
def fib(n):
    """
    Return a list of the first n Fibonacci numbers.定义波那契数列
    """
    f0, f1 = 0, 1
    f = [1] * n
    for i in range(1, n):
        f[i] = f0 + f1
        f0, f1 = f1, f[i]
        
        
    return f
print(fib(10))
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
%run fib.py #正确的方法是采用魔法命令.
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
import fib #如果想使用该文件
#%timeit fib(100)
import numpy as np
def random_walker_max_distance(M, N):
     """
    Simulate N random walkers taking M steps, and return the 
    largest distance
    from the starting point achieved by any of the random walkers.
    """
trajectories = [np.random.randn(M).cumsum() for _ in range(N)]
return np.max(np.abs(trajectories))
import numpy as np

def random_walker_max_distance(M,N):
    """仿真N步随机行走,返回最大的距离。"""
    trajectories = [np.random.randn(M).cumsum() for _ in range(N)]
    return np.max(np.abs(trajectories))
%prun random_walker_max_distance(400,10000)

关于语句 for _ in range(N) 的说明
以斐波那契数列为例

#求前20项的斐波那契数
a = 0
b = 1
for _ in range(20):
(a, b) = (b, a + b)
print(a, end=’ ')
1
2
3
4
5
6
其中’_’ 是一个循环标志,也可以用i,j 等其他字母代替,下面的循环中不会用到,起到的是循环此数的作用
就像C语言中
————————————————
版权声明:本文为CSDN博主「小小探索者」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhuyin6553/article/details/90289879

    20010 function calls in 0.178 seconds

Ordered by: internal time

ncalls tottime percall cumtime percall filename:lineno(function)
10000 0.128 0.000 0.128 0.000 {method ‘randn’ of ‘mtrand.RandomState’ objects}
10000 0.021 0.000 0.021 0.000 {method ‘cumsum’ of ‘numpy.ndarray’ objects}
1 0.020 0.020 0.176 0.176 :3(random_walker_max_distance)
1 0.005 0.005 0.154 0.154 :5()
1 0.002 0.002 0.178 0.178 :1()
1 0.002 0.002 0.002 0.002 {method ‘reduce’ of ‘numpy.ufunc’ objects}
1 0.000 0.000 0.178 0.178 {built-in method builtins.exec}
1 0.000 0.000 0.002 0.002 fromnumeric.py:69(_wrapreduction)
1 0.000 0.000 0.002 0.002 fromnumeric.py:2397(amax)
1 0.000 0.000 0.000 0.000 fromnumeric.py:70()
1 0.000 0.000 0.000 0.000 {method ‘disable’ of ‘_lsprof.Profiler’ objects}
1 0.000 0.000 0.000 0.000 {method ‘items’ of ‘dict’ objects}

关于%run 和 %prun 的说明

当%timeit和%time命令用于测量经过的时间时在计算运行时,它们不提供任何有关哪个部分的详细信息计算需要更多的时间。这样的分析需要更复杂的代码的分析器,如python标准库模块cprofile提供的分析器。python profiler在ipython中可以通过命令%prun(for语句)访问和%运行时使用标记–p(用于运行外部脚本文件)。探查器的输出是相当详细的,可以使用%p run和%run-p的可选标志进行自定义命令(请参阅%prun?有关可用选项的详细说明)。

%prun? #Run a statement through the python code profiler
UsageError: Line magic function `%prun?` not found.
import sympy
sympy.init_printing()
x = sympy.symbols("x")
i = sympy.Integral(x**2+3*x,(x,0,1))
i

∫ 0 1 ( x 2 + 3 x )   d x \displaystyle \int\limits_{0}^{1} \left(x^{2} + 3 x\right)\, dx 01(x2+3x)dx

矢量、矩阵以及多维阵列

#构造以上数据,首选numpy模块
import numpy as np
#ndarray 类的内部特征函数,因为不与其它的阵列进行运算,而是该类內禀的属性,为了区别运算属性,特记为,内属性
data = np.array([[1, 2], [3, 4], [5, 6]])#创建数据
type(data)
numpy.ndarray
data
array([[1, 2],
       [3, 4],
       [5, 6]])
data.ndim #阵列的维度,二维的。

2 \displaystyle 2 2

data.shape #阵列的行与列的数量。

( 3 ,   2 ) \displaystyle \left( 3, \ 2\right) (3, 2)

data.size

6 \displaystyle 6 6

data.dtype 
dtype('int64')
data.nbytes

48 \displaystyle 48 48

np.array([1, 2, 3], dtype=np.int)
np.array([1, 2, 3], dtype=np.float)
np.array([1, 2, 3], dtype=np.complex)
array([1.+0.j, 2.+0.j, 3.+0.j])

一旦创建了阵列,dtype就无法更改了,所以,以上三个过程分别建立了三个不同的阵列。

阵列的实部和虚部

data.real
array([[1, 2],
       [3, 4],
       [5, 6]])
data.imag
array([[0, 0],
       [0, 0],
       [0, 0]])
data[1].real
#下标也就是中括号所示的值,就是矩阵的或者行列式的下标,然而,与matalb不同的是,它的下标是从0开始计数的
array([3, 4])

创建阵列的常用函数

np.zeros((2,3))
array([[0., 0., 0.],
       [0., 0., 0.]])
np.ones(4)
array([1., 1., 1., 1.])
np.ones((4,5))
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
np.full(10,4.8)
array([4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8, 4.8])
np.full((4,5),6)
array([[6, 6, 6, 6, 6],
       [6, 6, 6, 6, 6],
       [6, 6, 6, 6, 6],
       [6, 6, 6, 6, 6]])
x1 = np.empty(5)
x1.fill(5.0)
x1
array([5., 5., 5., 5., 5.])
np.empty(5)
array([5., 5., 5., 5., 5.])
np.empty(5)
array([5., 5., 5., 5., 5.])
np.empty(6)# 产生的空矩阵是随机的,包括其中的矩阵元也是随机的。
array([0., 0., 0., 0., 0., 0.])

创建特定大小和数据类型的数组,但不初始化

数组到任何特定的值,我们都可以使用函数np.empty。优势

例如,使用这个函数,而不是np.zeros,它创建一个初始化的数组

用零值元素,就是可以避免初始化步骤。如果所有元素都是

保证稍后在代码中初始化,这可以节省一点时间,特别是

当使用大型数组时。

np.arange(0.0, 10, 1)#产生一个阵列,从0.0开始,到10,其中的间隔为2
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
np.arange(0.0, 10, 3)# 但是没有最后那个数
array([0., 3., 6., 9.])
np.linspace(0,10,11)
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
np.linspace(0,10,15)#均分为15个等份
array([ 0.        ,  0.71428571,  1.42857143,  2.14285714,  2.85714286,
        3.57142857,  4.28571429,  5.        ,  5.71428571,  6.42857143,
        7.14285714,  7.85714286,  8.57142857,  9.28571429, 10.        ])
np.logspace(0,2,6)
array([  1.        ,   2.51188643,   6.30957344,  15.84893192,
        39.81071706, 100.        ])
x = np.array([-1, 0, 1])
y = np.array([-2, 0, 2])
 X, Y = np.meshgrid(x, y)
X
array([[-1,  0,  1],
       [-1,  0,  1],
       [-1,  0,  1]])
Y
array([[-2, -2, -2],
       [ 0,  0,  0],
       [ 2,  2,  2]])
import matplotlib.pyplot as plt
plt.plot(X,Y)
[,
 ,
 ]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KJm6LpZc-1569549016578)(output_83_1.png)]

从其它数组为模板创建新数组

np.ones_like(X)#继承了其它矩阵的维度、数据类型等特征。
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])
X
array([[-1,  0,  1],
       [-1,  0,  1],
       [-1,  0,  1]])

np.zeros_like,np.full_like ,np.empty_like 等等

np.identity(4)#创建单位矩阵
array([[1., 0., 0., 0.],
       [0., 1., 0., 0.],
       [0., 0., 1., 0.],
       [0., 0., 0., 1.]])
#但更加广泛的eye矩阵,能够产生更加灵活的应用,包括建立上面的单位矩阵
np.eye(3, k=1)
array([[0., 1., 0.],
       [0., 0., 1.],
       [0., 0., 0.]])
np.eye(3, k=-1)
array([[0., 0., 0.],
       [1., 0., 0.],
       [0., 1., 0.]])
np.diag(np.arange(0, 20, 5))#对角矩阵,并且可以让对角矩阵元的数据产生变换。
array([[ 0,  0,  0,  0],
       [ 0,  5,  0,  0],
       [ 0,  0, 10,  0],
       [ 0,  0,  0, 15]])
np.diag(np.linspace(0, 20, 5))
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  5.,  0.,  0.,  0.],
       [ 0.,  0., 10.,  0.,  0.],
       [ 0.,  0.,  0., 15.,  0.],
       [ 0.,  0.,  0.,  0., 20.]])

一维阵列

a = np.arange(0, 21)
a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20])
a[3]
3
a[-3] #倒着数第三个数的意思
18
a[2:6]
array([2, 3, 4, 5])

可以使用m:n这样的表达式选择一系列元素,其中

选择以m开头以n-1结尾的元素(注意,第n个元素是

不包括在内)。切片m:n也可以更明确地写成m:n:1,其中

数字1指定应该选择m和n之间的每个元素。选择

m和n之间的每一个第二元素,使用m:n:2,要选择每个p元素,请使用

M:N:P,等等。如果p为负,则元素从

m到n+1(这意味着在这种情况下m必须大于n)。

a[:] #选中全部的元素
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20])
a[3:]
array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
       20])
a[3:-1]
array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
a[3:-1:4] #双冒号结构,是选中从3,到-1,也就是倒数第二哥数,然选择的时候采用隔着4个选一个的原则
array([ 3,  7, 11, 15, 19])
a[-1]# 最后的那个数
20
a[0]# 最前面的那个数
0
a[1:-1] # 用冒号算符,就不是最后那个数!你说,你说,找谁说理去
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19])
a[1:21] #这才是全部的值。
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20])
a[1:22]# 超出了范围,也是可以容错的,拯救了懒得数数的人
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19, 20])
a[:5]
array([0, 1, 2, 3, 4])
a[::-2]
array([20, 18, 16, 14, 12, 10,  8,  6,  4,  2,  0])
#总之,冒号算符,用处很大,但是务必清楚它指代的是什么,与单独的数是不一样的

f = lambda m, n: n + 10 * m
A = np.fromfunction(f, (6, 6), dtype=int)
A
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])
ff = lambda m,n:m+n*2
ff = lambda m,n:m+n*2

AS = np.fromfunction(ff,(6, 6),dtype=int)

AS

np.fromfunction?
AS
array([[ 0,  2,  4,  6,  8, 10],
       [ 1,  3,  5,  7,  9, 11],
       [ 2,  4,  6,  8, 10, 12],
       [ 3,  5,  7,  9, 11, 13],
       [ 4,  6,  8, 10, 12, 14],
       [ 5,  7,  9, 11, 13, 15]])
ff = lambda m,n:m*2+n

BS = np.fromfunction(ff,(6, 6),dtype=int)

BS
array([[ 0,  1,  2,  3,  4,  5],
       [ 2,  3,  4,  5,  6,  7],
       [ 4,  5,  6,  7,  8,  9],
       [ 6,  7,  8,  9, 10, 11],
       [ 8,  9, 10, 11, 12, 13],
       [10, 11, 12, 13, 14, 15]])

通过上面的分析也可以看出,矩阵元就是 f(m,n)

np.fromfunction(lambda i, j: i == j, (3, 3), dtype=int)
array([[ True, False, False],
       [False,  True, False],
       [False, False,  True]])
np.fromfunction(lambda i, j: i == j, (6, 6), dtype=int)
array([[ True, False, False, False, False, False],
       [False,  True, False, False, False, False],
       [False, False,  True, False, False, False],
       [False, False, False,  True, False, False],
       [False, False, False, False,  True, False],
       [False, False, False, False, False,  True]])
def func(i):
    return 2*i

a = np.fromfunction(func,(5,))
a
array([0., 2., 4., 6., 8.])

关于数组中的部分特殊矩阵(实际上就是多维数组)

这个说法本身就带了些混乱,但是混乱在这里是不可避免的。array具有操作灵活、运算速度快的优势,二在进行符号运算之类的特殊场合,矩阵就具有了表示上的优势。所以建议能够用array就用array。

AS[:,1] #抽取第二行
array([2, 3, 4, 5, 6, 7])
AS[1,:] #抽取第二列,同事变成行表示
array([ 1,  3,  5,  7,  9, 11])
AS[:3,:3] #抽取分块矩阵,中的左上角分块矩阵
array([[0, 2, 4],
       [1, 3, 5],
       [2, 4, 6]])
AS
array([[ 0,  2,  4,  6,  8, 10],
       [ 1,  3,  5,  7,  9, 11],
       [ 2,  4,  6,  8, 10, 12],
       [ 3,  5,  7,  9, 11, 13],
       [ 4,  6,  8, 10, 12, 14],
       [ 5,  7,  9, 11, 13, 15]])
AS[3:, :3] #lower left off-diagonal block matrix
array([[3, 5, 7],
       [4, 6, 8],
       [5, 7, 9]])
AS[::2, ::2] # every second element starting from 0, 0
array([[ 0,  4,  8],
       [ 2,  6, 10],
       [ 4,  8, 12]])
AS[1::2, 1::3] #从1,1开始每第二和第三个元素。从1开始到最后,每隔2个位置。也就是,
array([[ 3,  9],
       [ 5, 11],
       [ 7, 13]])
可以看出,是把矩阵中的AS(2,2),AS(2,5)AS(4,2)AS(4,5),AS(6,2)AS(6,5),这时候表示的下标就是通常意义下的下标,航和列都是从1开始的,而不是从零开始的。
AS[1::3, 1::3]# 1::3,的意思是,从第行开始,每隔着3行,包括最大的那个3的整数行。或者是列也是相同的。
array([[ 3,  9],
       [ 6, 12]])

第2行,第2列那个数表示为 AS(2,2),依次有,AS(2,5)

AS[1:3]
array([[ 1,  3,  5,  7,  9, 11],
       [ 2,  4,  6,  8, 10, 12]])
AS
array([[ 0,  2,  4,  6,  8, 10],
       [ 1,  3,  5,  7,  9, 11],
       [ 2,  4,  6,  8, 10, 12],
       [ 3,  5,  7,  9, 11, 13],
       [ 4,  6,  8, 10, 12, 14],
       [ 5,  7,  9, 11, 13, 15]])

总结:

的确,python的矩阵操作符号,:的用法过于灵活,掌握起来很不方便,特别是用过MATLAB的同学们,更改过程肯定是不容易的。但只要多加锻炼,掌握后就可以非常灵活的应用矩阵,就能够充分利用Python矩阵操作的便利性。

B = AS[1:3,1:4]
B
array([[3, 5, 7],
       [4, 6, 8]])
B[:,:] =0
B
array([[0, 0, 0],
       [0, 0, 0]])
A
array([[ 0,  1,  2,  3,  4,  5],
       [10, 11, 12, 13, 14, 15],
       [20, 21, 22, 23, 24, 25],
       [30, 31, 32, 33, 34, 35],
       [40, 41, 42, 43, 44, 45],
       [50, 51, 52, 53, 54, 55]])
AS
array([[ 0,  2,  4,  6,  8, 10],
       [ 1,  0,  0,  0,  9, 11],
       [ 2,  0,  0,  0, 10, 12],
       [ 3,  5,  7,  9, 11, 13],
       [ 4,  6,  8, 10, 12, 14],
       [ 5,  7,  9, 11, 13, 15]])

这里真的存在严重的关联问题,用直接赋值的方法,必然有关联改变的问题

如何消除这种关联

C = B[1:3,1:3].copy()#用copy函数彻底分开
C
array([[0, 0]])
C[:,:] = 1000
C
array([[1000, 1000]])
B
array([[0, 0, 0],
       [0, 0, 0]])
#用指标选取矩阵元
A = np.linspace(0,1,11)
A
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. ])
A[np.array([0,2,4])]#被称为花式索引
array([0. , 0.2, 0.4])
#等效于下面的形式
A[[0,2,4]] #被称为花式索引 fancy indexing
#但是显然后者更加简单
array([0. , 0.2, 0.4])
#有时候布尔矩阵非常有用,python可以允许采用如下简单形式产生布尔矩阵
A>0.5
array([False, False, False, False, False, False,  True,  True,  True,
        True,  True])
#在进行如下的使用方法就可以完成,从阵列中找到大于,小于某数的方法。
A[A>0.5]#被称为布尔索引 boolean - valued indexing
array([0.6, 0.7, 0.8, 0.9, 1. ])
这是值得庆贺的一件事情。
A = np.arange(10)
indices = [2, 4, 6]
B = A[indices]
B[0] = -1  # this does not affect A
A

#花式索引并不改变原来的矩阵,所以前面用冒号索引的东西,称为原来矩阵的一个切片!!!
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
A[indices] = -1  # this alters A
A
array([ 0,  1, -1,  3, -1,  5, -1,  7,  8,  9])
A = np.arange(10)
B = A[A > 5]   
B[0] = -1  # this does not affect A
A
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
A[A > 5] = -1  # this alters A
A
array([ 0,  1,  2,  3,  4,  5, -1, -1, -1, -1])
from IPython.display import Image
from IPython.core.display import HTML
Image(url= "./narrayData.png")

上面图像对矩阵和指标的关系做了详细的说明,请务必记清楚了。

阵列的Reshaping 和 Resizing

import numpy as np
#通过reshaping 同时可以明确矩阵的结构,维度,以及表示方式。
data = np.array([[1,2],[3,4]])
data
array([[1, 2],
       [3, 4]])
np.reshape(data,(1,4))
array([[1, 2, 3, 4]])
data.reshape(4)
array([1, 2, 3, 4])

#可以明确的是,array()函数括号内就是某个量,可以是完整的阵列或者矩阵,但是不能是多个量,只能是一个量。
重塑数组的形状不需要修改底层数组数据;它只需要

通过重新定义数组的strips属性,更改数据的解释方式。

这种操作的一个例子是2×2数组(矩阵),它被重新解释为

1×4数组(矢量)。在numpy中,函数np.reforme或ndarray类方法

可用于重新配置底层数据的解释方式。它需要一个

data = np.array([[1,2],[3,4]])
data
array([[1, 2],
       [3, 4]])
data.flatten()
array([1, 2, 3, 4])
data.flatten().shape

( 4 ) \displaystyle \left( 4\right) (4)

#通过flatten函数,把二维阵列变为一维阵列

灵活的vstack 和 hstack

data= np.arange(5)
data
array([0, 1, 2, 3, 4])
np.vstack((data,data,data))
array([[0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4],
       [0, 1, 2, 3, 4]])
np.hstack((data,data,data))
array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4])
data = np.random.normal(size=(5, 10, 15))
data
array([[[ 4.63263992e-01,  1.57231830e+00,  1.28624699e+00,
          8.02534493e-01, -1.45538857e+00,  1.23957319e+00,
          1.60485203e+00,  3.35433682e-01,  1.41454548e+00,
          1.80504909e-02,  2.48805260e-01, -9.19335965e-02,
          1.57978869e+00, -1.10057133e+00, -1.36833463e+00],
        [ 1.24242675e-01, -1.64997667e+00, -3.08183053e-01,
          1.32689452e+00,  3.19190683e-01,  1.55995142e+00,
          8.01088249e-01, -9.71248954e-01,  1.20171815e-01,
          1.03372518e+00, -2.86983948e-01, -6.27489916e-01,
         -9.22643980e-02, -9.17784701e-02, -2.74552044e-01],
        [ 2.16710100e+00, -1.58640423e+00, -1.36065498e+00,
         -1.03039431e+00,  7.55795257e-01, -2.49624308e-01,
          1.02056645e+00,  6.62217209e-01,  6.85790076e-04,
         -6.90690945e-01, -5.06088577e-02, -3.49912939e-01,
          6.81303055e-01, -2.57376552e-01,  8.49541806e-01],
        [-2.10067212e-02, -1.91148705e+00, -1.77140609e+00,
         -7.44344596e-01, -2.22506263e-01, -1.51951464e+00,
         -3.87093743e-01,  1.80673141e+00, -1.64147633e+00,
         -4.30883133e-01, -6.95590644e-02, -9.15788838e-01,
          5.70793359e-01, -6.32110376e-01,  2.49961842e+00],
        [ 4.94648663e-01, -3.86692435e-01,  1.46208608e+00,
         -1.08363931e+00, -1.17961102e+00,  1.19055503e-01,
         -1.03778800e+00,  5.27878715e-01, -4.61602526e-01,
         -3.54731356e-02, -5.30393864e-01, -1.00306318e-01,
          5.07037546e-02,  9.78793496e-01,  7.14955558e-01],
        [-4.47358729e-01,  1.16824890e+00,  2.73868535e+00,
          2.41536357e-01,  3.53693717e-01, -1.37407977e+00,
         -5.28762385e-01, -6.79820507e-01,  1.31283635e+00,
         -1.48684145e+00,  6.34903217e-01, -1.57185446e-01,
         -1.21672990e+00, -3.77414898e-01, -2.11371035e-01],
        [-2.23198664e+00, -7.52372391e-01,  8.52025141e-01,
         -1.04124595e+00, -1.63421610e+00, -2.10306372e-02,
          8.45607061e-01, -8.06313118e-02,  9.63856516e-01,
         -7.39314276e-01,  1.05821468e+00,  1.03089650e-01,
          9.54449363e-01, -4.70534917e-01,  2.06230343e+00],
        [ 6.94182515e-01,  3.49879772e-01, -1.01828589e+00,
          2.96517622e-02, -1.92526112e-01,  7.15006335e-01,
         -3.88845827e-01, -6.65125837e-01, -2.05674093e-01,
          6.21817478e-01, -1.17362080e-01, -1.11124138e+00,
         -2.46215766e-01, -1.03299460e-01,  1.44675043e-01],
        [ 1.35082476e-01,  4.37672012e-01,  4.30128647e-01,
         -1.24189843e-01, -5.29715937e-01,  2.73220062e-01,
          6.14513181e-01, -1.81398943e-01,  3.78597192e-01,
          1.05232248e+00,  1.00798030e+00,  8.34452511e-01,
         -1.02545720e+00,  1.41762742e+00, -1.83771292e+00],
        [ 3.70545360e-01,  7.17000310e-01,  8.87755178e-01,
          7.11147253e-01,  2.31738222e-01,  1.23849041e+00,
          5.84559320e-01, -1.63064205e+00, -6.52242763e-01,
         -1.10946763e+00,  2.46248292e+00,  2.89051529e-01,
          2.50168700e-01,  1.28510542e+00,  5.22811500e-01]],

       [[ 7.14929871e-01,  1.84605861e+00,  6.37686769e-01,
          1.63533308e+00,  1.24718039e+00, -7.95206492e-02,
          6.31004666e-01,  9.99480614e-01,  1.31628111e+00,
          1.82807297e+00, -1.16969237e+00, -8.38657143e-01,
         -7.48745560e-01, -1.06419157e+00, -1.24966979e-01],
        [-4.67898474e-01,  6.14252689e-01, -2.08729378e+00,
          3.52662105e-02, -9.24861541e-01, -2.65427321e-01,
         -1.96767392e-01, -2.51690302e+00,  1.30447843e-01,
          1.04570471e+00, -3.56691615e-01, -2.85593948e-01,
         -2.71288756e+00, -8.40685470e-02,  5.73063298e-01],
        [-7.88232246e-01, -3.50308610e-01,  1.19211026e-01,
          7.16618750e-02, -1.35000200e+00,  1.62493566e+00,
          1.41823957e+00,  1.11418734e+00, -8.17085068e-01,
         -6.61138104e-01, -8.10426332e-01, -1.55709524e+00,
          7.37188622e-01,  1.40982633e-01, -2.06509314e+00],
        [ 7.01341023e-01, -8.01473099e-01,  2.58654968e+00,
          1.40386588e+00,  6.49344885e-01,  4.67856522e-01,
         -5.84408147e-01, -3.32700431e-01, -1.01294873e+00,
          1.69727560e-01,  7.77644505e-02, -7.46616884e-01,
          3.11427389e-01, -1.66474655e+00, -3.09646714e-01],
        [-6.73607304e-01,  5.98711084e-01,  1.34369718e+00,
          2.36376276e-01, -8.36232667e-01, -5.81983250e-02,
         -5.29937692e-01,  1.18592520e+00,  6.86696699e-01,
         -1.02863351e+00, -8.66404249e-01,  5.38358097e-01,
          4.90888836e-01,  1.53929525e+00, -3.60663535e-01],
        [-2.57157495e-01, -7.37528940e-01,  2.92713167e-02,
          1.38837098e+00,  1.44148986e+00,  5.59848758e-01,
          7.60409607e-02,  3.22120168e-01, -1.26298673e+00,
          6.76041395e-01,  1.12865532e+00,  1.50757334e-01,
         -3.38581175e-01, -5.99198902e-01,  7.81042351e-01],
        [ 6.85963245e-02, -7.33593319e-02, -1.52709036e+00,
          7.77427257e-01, -1.75680094e-01, -2.98381930e-01,
          1.04089360e+00, -1.67495820e+00,  1.21643696e+00,
          5.01444282e-01, -1.36686268e+00,  6.37206487e-03,
         -3.69424296e-01,  7.95946705e-01,  4.83919238e-01],
        [ 1.46049323e-01, -1.30537736e+00, -8.37382071e-01,
          1.50884911e+00,  2.97171538e-01, -6.47910546e-01,
          7.36338095e-01, -2.28990416e+00,  5.64894084e-01,
         -7.17908596e-01,  9.44359866e-01,  6.57621500e-01,
         -7.95773924e-01,  6.85435278e-02,  6.24368074e-01],
        [ 4.69907579e-01, -2.68557602e-01, -1.55453152e+00,
          1.29725705e-01,  3.21903094e-01,  1.27556041e+00,
         -6.66932768e-01,  3.78352771e-01, -2.46115445e-01,
          2.37451600e+00, -1.18117322e+00, -6.66422650e-01,
         -6.52321673e-01, -1.44575326e-01, -4.72056343e-01],
        [-1.86574856e+00, -4.74104300e-01,  3.63342100e-01,
          1.70830041e-01, -8.65279744e-01,  1.34837622e-01,
         -7.37403509e-01, -1.03351421e-01,  6.80973114e-01,
          7.52949092e-01,  1.57910417e-01, -9.50667788e-01,
         -6.72929231e-01, -1.33777240e+00,  7.52568100e-02]],

       [[-6.02388600e-01,  2.27882391e-01, -4.50902628e-01,
          3.66456569e-01,  8.26358865e-01, -3.01799026e-01,
         -1.07142066e+00,  1.17467327e+00, -1.54630056e+00,
         -1.00775152e+00,  2.41360476e-01,  4.42590619e-01,
          3.02296964e-01, -3.29771609e-01,  5.66701283e-01],
        [-2.00105122e+00,  4.02681430e-01,  1.92433241e+00,
          4.04919713e-03,  8.84068282e-01,  6.26970201e-01,
         -1.46163517e+00,  7.09474888e-01, -1.80077441e+00,
         -5.20329678e-02, -1.68081051e-01, -6.28219382e-01,
          1.41625541e+00,  9.85970438e-01, -1.76180830e-01],
        [ 6.66577555e-02, -1.88169922e+00,  1.73913337e-01,
          6.68117137e-01,  1.16921628e+00,  3.54675472e-01,
          1.16184134e+00,  2.12437590e-01, -1.07888795e+00,
         -1.67811945e+00, -9.72393443e-02, -8.29744429e-01,
         -1.23846970e+00,  7.37408237e-01,  7.32661423e-01],
        [-2.57919615e+00,  1.75666665e+00,  1.01622116e+00,
         -2.80467234e-01, -5.76186068e-01,  1.19895121e+00,
         -9.73565858e-01,  1.00030901e-01, -1.88703637e+00,
         -8.82243641e-01, -1.16200183e+00,  7.31043152e-01,
          1.43468388e+00, -7.92046292e-01,  4.89513028e-01],
        [ 6.89210952e-01,  4.58546685e-01,  1.35037849e+00,
          4.46183176e-01,  1.13143330e+00, -1.91631187e+00,
          7.71505109e-01,  1.03147128e-01,  7.17612097e-01,
         -9.49535509e-01, -4.71625516e-01,  1.73256964e+00,
         -3.79416908e-01,  8.08453539e-01,  1.54205260e+00],
        [ 1.03184636e+00, -4.91054546e-01, -1.05540419e+00,
         -5.33482455e-01,  6.66272777e-01,  2.99158661e-01,
          2.85996741e-01,  3.68779261e-01,  1.40782232e+00,
         -4.44217448e-01, -1.08396350e+00, -1.60580357e+00,
         -8.24511141e-01,  1.15714879e+00, -9.74851573e-01],
        [ 2.60648589e-01, -1.08883233e+00, -1.32349136e+00,
         -3.54923848e-01, -1.87397877e+00, -7.49751069e-01,
         -1.12034513e-01,  2.09537997e+00,  3.97430355e-01,
          4.91076409e-01,  4.53853104e-01,  6.80566792e-01,
         -2.44083024e+00,  2.20887277e-01,  6.51354497e-01],
        [ 6.05925537e-02,  1.67422179e-01,  6.39645534e-01,
          2.30313342e+00,  4.73099674e-01,  4.07298444e-02,
          1.26050895e+00,  9.33143809e-01, -5.83857223e-01,
         -7.70078949e-01,  1.75738140e-01, -9.42423290e-01,
          8.70452405e-01,  1.52324852e+00,  1.91693393e+00],
        [ 1.16662337e+00, -1.04177095e+00,  7.41124985e-02,
         -4.10168742e-01,  2.08000244e+00,  1.23850969e-01,
         -3.89880500e-01, -1.53389330e+00,  1.30952044e+00,
          1.04002504e-01, -1.95010549e+00,  4.17293404e-01,
         -1.15487826e+00,  2.61162094e-02,  1.66192028e+00],
        [ 9.88281660e-02,  2.29840675e+00, -1.62556942e+00,
         -1.99640419e+00, -2.45757624e-01, -7.65665141e-01,
          9.19297126e-02, -2.11557825e+00,  3.24051008e-01,
         -1.06343298e+00,  2.48677854e-01,  6.03883863e-01,
          1.21445932e+00, -3.25610528e-01, -7.41199884e-01]],

       [[-1.70780826e+00,  2.29224265e+00,  3.29511685e-01,
          1.54738302e-01, -3.73022629e-01,  5.65188898e-01,
         -1.56704617e+00, -2.04749206e-02,  1.08949986e+00,
          1.61911327e+00,  3.86351512e-01,  1.24849505e-01,
          1.69881938e+00, -1.70170152e+00, -1.77451482e-01],
        [-1.45149151e+00, -4.13903580e-01, -1.00238871e+00,
          8.28648180e-01,  1.77598404e+00, -1.79383619e-01,
          7.39156721e-01, -8.93210424e-01,  1.55340601e+00,
         -1.51889915e+00,  1.38695957e+00,  1.35424570e+00,
         -7.31461071e-01, -5.24100304e-01, -7.80725980e-01],
        [-1.81476779e+00,  1.29472976e+00, -8.94350121e-01,
         -8.57232295e-01, -7.59344976e-01, -9.71914572e-01,
          9.79892048e-01, -9.21179029e-01,  1.62003302e-01,
          1.09588189e+00, -6.66673583e-01, -2.90074080e+00,
          1.53368299e+00,  1.90588066e-01, -1.59719614e+00],
        [ 2.80859504e+00, -1.89576974e+00, -1.95568471e+00,
          4.22972453e-01, -5.39655546e-01,  6.80831127e-01,
         -7.19438106e-01,  1.46142118e-01,  6.69615840e-01,
         -6.49412665e-01, -1.07824631e+00,  3.71083726e-01,
         -8.69591093e-01, -1.21737652e+00,  1.29885042e+00],
        [ 1.67506030e+00,  3.95640560e-02, -7.46689142e-01,
         -1.90897062e+00, -1.34841943e+00, -1.13851580e+00,
         -1.91214624e-01, -8.07591924e-01, -9.68202710e-01,
         -1.03457897e+00, -5.36713794e-01,  1.81770199e+00,
          1.28829703e+00,  1.48110204e+00, -4.29569439e-01],
        [-6.32707916e-01,  4.49414394e-01, -3.01553361e-01,
          1.11473584e+00, -6.93839325e-01, -1.06388745e+00,
          1.74267258e+00,  8.69046575e-01, -1.43744705e+00,
          3.69409853e-02, -2.46976182e+00, -6.92549557e-01,
          1.26684822e+00,  1.72730455e+00, -1.36275955e-01],
        [ 7.98142392e-01,  2.27770256e-01,  8.61506374e-01,
          4.58067232e-01, -6.04871165e-01,  2.27098151e-01,
         -6.14793213e-01,  6.86720516e-02,  1.60315250e+00,
          9.32663334e-02,  3.15948302e+00,  9.13707715e-02,
          8.60500871e-02,  1.55666366e-01,  2.57563539e+00],
        [-1.13662508e-01, -4.89564407e-01, -6.79180881e-01,
         -1.76971321e-01,  1.34543473e-01, -1.37278857e+00,
         -1.78872722e-02, -2.44716545e-01,  1.25990408e-01,
         -7.97115233e-01, -1.78598702e+00,  8.94614485e-01,
         -7.62214545e-01,  4.84323654e-01, -6.33287584e-01],
        [-6.46038694e-01,  8.41682863e-01, -3.91635340e-01,
         -1.69577439e-01, -4.92878821e-01,  4.97230344e-01,
         -9.74637479e-01, -1.46195685e+00, -1.30780305e+00,
         -4.28798928e-01, -3.01434184e-01,  1.16294454e+00,
          1.51589603e+00, -2.65096632e+00,  8.95623359e-01],
        [-3.45827173e-02,  1.13437798e+00, -3.09631439e-01,
          1.32643677e-01,  2.72352793e-02, -1.18812286e+00,
          1.02486089e+00, -2.35633537e-01,  8.40443305e-01,
          3.87145850e-01, -6.71605587e-02,  3.78090514e-01,
          1.79367716e+00,  3.12422391e-01,  1.67360476e-01]],

       [[ 3.29058434e-01,  1.00398109e-01,  1.69395628e-01,
          6.27032277e-01,  3.07246950e-01, -1.17608452e+00,
         -6.99822084e-01, -3.71812277e-01, -1.80155816e-02,
          1.77435798e-01,  5.24654672e-01,  1.13810537e+00,
          1.82786852e+00,  8.07279913e-01, -1.28809049e+00],
        [-4.29410916e-01, -9.96260044e-01,  2.58282549e-01,
         -7.14650397e-01,  1.57270930e-01, -3.59188649e-01,
         -2.30739944e+00,  2.11272519e-01,  9.81050275e-01,
          1.47789945e+00,  2.83068655e-01,  8.79415970e-02,
          4.34256141e-01,  4.36803762e-02, -6.66207742e-01],
        [ 7.21258949e-01, -5.54266545e-01, -2.09874968e-01,
         -1.68586231e-01,  6.07170989e-01, -1.90943159e+00,
          8.43523492e-01, -5.36392360e-01, -1.37911845e+00,
          1.38411801e+00, -1.58061116e+00, -1.82627322e-01,
          1.21096644e+00, -4.18881891e-01,  5.36720388e-01],
        [ 4.40142009e-01, -1.85576897e-02, -2.70890853e+00,
          1.67926696e+00,  1.84129600e+00, -1.07565861e+00,
         -1.64992984e+00, -1.94587182e+00,  1.16010927e+00,
         -1.59161307e+00,  5.96470707e-01,  6.53181566e-01,
          7.85874143e-02,  4.39514828e-01,  2.22798174e+00],
        [-2.19258140e-01,  3.62771244e-01, -2.26252244e+00,
         -2.02359603e+00,  8.26113893e-01, -9.23220979e-01,
         -5.37834342e-01,  1.11930475e+00, -1.19221574e-01,
         -1.87658995e+00,  5.98993522e-01,  1.26233932e+00,
          5.34398093e-01, -7.49501070e-01,  3.01865423e-01],
        [ 1.10570791e+00,  2.63116964e-01,  8.01946375e-01,
         -1.24846291e+00,  3.11008530e-01,  2.00188937e+00,
         -6.12219870e-01,  4.66725401e-02,  6.87574897e-01,
          5.45318294e-01, -5.44792902e-01, -2.69092388e-01,
          3.85842514e-01, -2.30809234e-01,  1.38023794e+00],
        [ 7.67719685e-01,  5.70357901e-01,  9.50359994e-01,
         -2.05887835e+00,  1.58771553e-01, -4.11595646e-01,
         -3.94203519e-01, -5.91900706e-01, -5.43835900e-01,
         -5.22157424e-01,  1.38525366e+00,  1.83244343e+00,
          9.68317757e-02,  4.54497704e-01, -1.54086129e+00],
        [-4.51169963e-01, -1.28505493e+00, -6.82966143e-01,
          4.43154915e-02, -8.79115290e-01, -4.28307579e-01,
          6.57974348e-01, -9.13970397e-01, -1.11874445e+00,
         -1.98141315e+00, -7.75669005e-01, -1.89086512e-01,
          9.05932976e-01, -1.80538581e-01,  2.09328694e+00],
        [ 1.02164721e-01, -1.03689049e+00,  1.69408884e+00,
         -4.24705983e-01,  1.50502050e+00, -2.88406802e-01,
         -1.17679336e+00, -3.15456291e-01, -5.81577412e-01,
         -1.96688247e+00, -2.80445786e-01,  1.33407767e-01,
         -3.52560909e-01, -6.76218090e-01, -1.38351669e+00],
        [ 1.59107257e-01,  1.93502204e-01,  1.69076867e+00,
          1.02000190e-01, -1.06274685e-01, -2.15649779e+00,
          1.08187592e+00, -1.85818424e+00, -6.95227515e-01,
          2.17631450e+00,  6.07256021e-01,  4.29663466e-02,
         -3.37466790e-01, -1.70090122e+00, -9.85516029e-01]]])
data.sum(axis=0).shape

( 10 ,   15 ) \displaystyle \left( 10, \ 15\right) (10, 15)

 data.sum(axis=(0, 2)).shape

( 10 ) \displaystyle \left( 10\right) (10)

data.sum()

− 9.393368158093267 \displaystyle -9.393368158093267 9.393368158093267

A.sum()
11
A
array([ 0,  1,  2,  3,  4,  5, -1, -1, -1, -1])
s=0
for i in A:
    s=s+i
    
    
    
s
11
#axis概念的应用
dat = np.arange(0,5)
dat
array([0, 1, 2, 3, 4])
colu =dat[:,np.newaxis]
colu
array([[0],
       [1],
       [2],
       [3],
       [4]])
#由此可以实现行列的互变,在线性代数的范围内,行向量列向量互变有着重要的应用。

##矩阵(阵列)的基本运算规则

x = np.array([[1, 2], [3, 4]])
y = np.array([[5, 6], [7, 8]])
x+y
array([[ 6,  8],
       [10, 12]])
x*y
array([[ 5, 12],
       [21, 32]])
2**x
array([[ 2,  4],
       [ 8, 16]])
y/x
array([[5.        , 3.        ],
       [2.33333333, 2.        ]])
xx = np.array([1, 2, 3, 4]).reshape(2, 2)
 z = np.array([1, 2, 3, 4])

此时如果运算会产生错误,原因就是维度不同

xx/z
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

 in 
----> 1 xx/z


ValueError: operands could not be broadcast together with shapes (2,2) (4,) 
xx
array([[1, 2],
       [3, 4]])
 zz = np.array([[2, 4]])
xx/zz
array([[0.5, 0.5],
       [1.5, 1. ]])

关于python的运算符

x = x+y;

x += y ;

完全等效的两种表达

并且依次支持,$*,/ , // , ** $ 乘法,除法 ,整除,指数乘积,运算的简化

list array matrix 三者的辨析

1、list

list是Python基础的数据格式。

list通过[ ]申明,支持append和expend等方法,没有shape属性。

2、matrix与array

1、matrix是矩阵、array是数组。

2、matrix必须是二维。

3、array的就是对应元素相乘,如果行或列数不匹配会自动补全。

array想要实现矩阵相乘,使用np.dot(array1, array2)

4、matrix的就是矩阵相乘。

matrix想要实现对应元素相乘,使用np.multiply(mat1, mat2)

numpy 中的矩阵和数组

-- coding: utf-8 --

from numpy import *

a1 =[[1,2,3],[4,5,6]] #列表
print(‘a1 :’,a1)
#(‘a1 :’, [[1, 2, 3], [4, 5, 6]])

a2 = array(a1) #列表 -----> 数组
print(‘a2 :’,a2)
#(‘a2 :’, array([[1, 2, 3],[4, 5, 6]]))

a3 = mat(a1) #列表 ----> 矩阵
print(‘a3 :’,a3)
#(‘a3 :’, matrix([[1, 2, 3],[4, 5, 6]]))

a4 = a3.tolist() #矩阵 —> 列表
print(‘a4 :’,a4)
#(‘a4 :’, [[1, 2, 3], [4, 5, 6]])

print(a1 == a4)
#True

a5 = a2.tolist() #数组 —> 列表

print(‘a5 :’,a5)
#(‘a5 :’, [[1, 2, 3], [4, 5, 6]])
print(a5 == a1)
#True

a6 = mat(a2) #数组 —> 矩阵
print(‘a6 :’,a6)
#(‘a6 :’, matrix([[1, 2, 3],[4, 5, 6]]))

print(a6 == a3)
#[[ True True True][ True True True]]

a7 = array(a3) #矩阵 —> 数组
print(‘a7 :’,a7)
#(‘a7 :’, array([[1, 2, 3],[4, 5, 6]]))
print(a7 == a2)
#[[ True True True][ True True True]]

###################################################################
a1 =[1,2,3,4,5,6] #列表
print(‘a1 :’,a1)
#(‘a1 :’, [1, 2, 3, 4, 5, 6])

a2 = array(a1) #列表 -----> 数组
print(‘a2 :’,a2)
#(‘a2 :’, array([1, 2, 3, 4, 5, 6]))

a3 = mat(a1) #列表 ----> 矩阵
print(‘a3 :’,a3)
#(‘a3 :’, matrix([[1, 2, 3, 4, 5, 6]]))

a4 = a3.tolist() #矩阵 —> 列表
print(‘a4 :’,a4)
#(‘a4 :’, [[1, 2, 3, 4, 5, 6]]) #注意!!有不同
print(a1 == a4)
#False

a8 = a3.tolist()[0] #矩阵 —> 列表
print(‘a8 :’,a8)
#(‘a8 :’, [1, 2, 3, 4, 5, 6]) #注意!!有不同
print(a1 == a8)
#True

a5 = a2.tolist() #数组 —> 列表
print(‘a5 :’,a5)
#(‘a5 :’, [1, 2, 3, 4, 5, 6])
print(a5 == a1)
#True

a6 = mat(a2) #数组 —> 矩阵
print(‘a6 :’,a6)
#(‘a6 :’, matrix([[1, 2, 3, 4, 5, 6]]))

print(a6 == a3)
#[[ True True True True True True]]

a7 = array(a3) #矩阵 —> 数组
print(‘a7 :’,a7)
#(‘a7 :’, array([[1, 2, 3, 4, 5, 6]]))
print(a7 == a2)
#[[ True True True True True True]]

https://blog.csdn.net/qi_1221/article/details/82626835

总结list、array和matrix的区别

1、list是Python中的普通列表对象,支持append和attend操作,没有shape属性;array和matrix是numpy数据库中的对象,不支持append和attend操作,具有shape属性。

2、一个list中可以存放不同类型的数据,如int、float、str,或者布尔型;而array和matrix中只能存放相同类型的数据。

3、list不支持乘法操作;array和matrix支持乘法操作。

4、list对象不支持一次性读取一行或一列,只能通过指针进行元素的索引;array和matrix即支持一次性读取一行或一列,也支持通过指针来进行元素的索引。

https://www.jianshu.com/p/eda4a91644b4

a1 =[[1,2,3],[4,5,6]] #列表
a1

[ [ 1 ,   2 ,   3 ] ,   [ 4 ,   5 ,   6 ] ] \displaystyle \left[ \left[ 1, \ 2, \ 3\right], \ \left[ 4, \ 5, \ 6\right]\right] [[1, 2, 3], [4, 5, 6]]

for x in a1:
    print(x)
    
    
    
[1, 2, 3]
[4, 5, 6]
Aa1= np.array(a1)
Aa1
array([[1, 2, 3],
       [4, 5, 6]])
Ma1=np.matrix(a1)
Ma1
matrix([[1, 2, 3],
        [4, 5, 6]])
type(a1),type(Aa1),type(Ma1)  #查看数据类型的常用函数!
(list, numpy.ndarray, numpy.matrix)
Ma2 = np.mat(Aa1)
Ma2
matrix([[1, 2, 3],
        [4, 5, 6]])
Ma1 == Ma2
matrix([[ True,  True,  True],
        [ True,  True,  True]])
arr1=np.mat.Ma1#错误的语法
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

 in 
----> 1 arr1=np.mat.Ma1


AttributeError: 'function' object has no attribute 'Ma1'
arr1= Ma1.A
#矩阵转换为数组的方法被称为矩阵的A属性
arr1
array([[1, 2, 3],
       [4, 5, 6]])
type(arr1)
numpy.ndarray
arr2 = Ma1.getA()
arr2
array([[1, 2, 3],
       [4, 5, 6]])

数组和矩阵转换为列表

#数组到列表
list1 = arr1.tolist()
list1

[ [ 1 ,   2 ,   3 ] ,   [ 4 ,   5 ,   6 ] ] \displaystyle \left[ \left[ 1, \ 2, \ 3\right], \ \left[ 4, \ 5, \ 6\right]\right] [[1, 2, 3], [4, 5, 6]]

list2 = Ma1.tolist()
list2

[ [ 1 ,   2 ,   3 ] ,   [ 4 ,   5 ,   6 ] ] \displaystyle \left[ \left[ 1, \ 2, \ 3\right], \ \left[ 4, \ 5, \ 6\right]\right] [[1, 2, 3], [4, 5, 6]]

list3 = Ma1.tolist()[0]
list3

[ 1 ,   2 ,   3 ] \displaystyle \left[ 1, \ 2, \ 3\right] [1, 2, 3]

list3.reshape(1,1)
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

 in 
----> 1 list3.reshape(1,1)


AttributeError: 'list' object has no attribute 'reshape'
arr2.reshape(1,6)
array([[1, 2, 3, 4, 5, 6]])
#应该用数组完成你所需要的变换,然后再进行转换。
#但是同时list具有append和pop等属性也是非常好用的

list 属性

1.list.append() #在列表后面追加元素

2.list.extend() #在列表后面追加另一个列表的元素

3.list.pop(index) #弹出指定位置的元素

4.list.sort(reverse=False) #列表元素升序 #reverse=True为降序

5.list.count(data) #统计列表中出现data的次数

6.list.index(data) #列表中元素的索引

7.list.max() #列表中的最大值

8.list.min() #列表中的最小值

9.list.remove(obj) #删除列表中指定元素

10.list.reverse() #列表元素倒序输出

11.list.insert(index,obj) #向指定位置插入元素


常用函数

(1)初等函数 Elementwise Functions

x= np.linspace(-1,1,11)
y= np.sin(np.pi*x)
plt.plot(x,y)
[]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lHwQoCJz-1569549016579)(output_249_1.png)]

from IPython.display import Image
from IPython.core.display import HTML
Image(url= "./ElementwiseFunctions.png")
from IPython.display import Image
from IPython.core.display import HTML
Image(url= "./ElementwiseMathOperator.png")
def heaviside(x):
    return 1 if x>0 else 0
heaviside(-3)

0 \displaystyle 0 0

heaviside(4)

1 \displaystyle 1 1

#能否实现一系列的数据进行变换,而不是一个一个的数据
x
array([-1. , -0.8, -0.6, -0.4, -0.2,  0. ,  0.2,  0.4,  0.6,  0.8,  1. ])
heaviside(x)
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

 in 
----> 1 heaviside(x)


 in heaviside(x)
      1 def heaviside(x):
----> 2     return 1 if x>0 else 0


ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
heavisideV = np.vectorize(heaviside)
#以上过程就是矢量化
heavisideV(x)
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1])

Although the function returned by np.vectorize works with arrays, it will be
relatively slow since the original function must be called for each element in the array.
There are much better ways to implementing this particular function using arithmetic
with Boolean-valued arrays, as discussed later in this chapter。

尽管np.vectorize返回的函数与数组一起工作,但是

相对较慢,因为必须为数组中的每个元素调用原始函数。

有很多更好的方法可以使用算术来实现这个特定的函数

对于布尔值数组,如本章后面有所介绍。但是这种方法,仍然是一种非常方便和直观的实现标量数据到矢量数据处理过程中最常见的方法。

聚合函数Aggregate Functions

data = np.random.normal(size=(5, 10, 15))
data
array([[[-7.89295196e-01,  1.19996079e+00, -1.36992306e+00,
          1.33596122e+00,  5.98912854e-01,  6.59181102e-01,
         -6.72392777e-01, -2.41764930e+00,  5.50797756e-01,
          6.65132391e-01,  1.69983620e+00, -4.33976600e-01,
          1.72387199e-01,  1.08841269e+00,  1.29232675e+00],
        [ 3.29001101e-01,  1.24582360e+00, -5.82783321e-01,
         -4.64694733e-01, -1.14975702e+00,  1.31321383e+00,
          1.19156387e+00,  1.63811346e+00,  1.22850532e+00,
          7.43878301e-01,  1.07105512e+00, -7.41348095e-01,
         -1.02753398e-01, -1.17463697e+00, -2.19458719e+00],
        [-5.49406519e-01, -4.10288188e-01, -1.04803378e+00,
          1.92213417e+00,  1.11001193e+00, -8.92608890e-01,
          8.19844385e-01,  5.22166852e-02,  4.06842978e-02,
         -2.70716043e-01, -1.50647871e+00, -1.36767638e+00,
          1.85957349e+00,  1.20342774e+00,  5.79763811e-01],
        [-4.50273094e-02, -8.04174256e-01, -9.70170949e-01,
          3.63505984e-01, -1.39248508e+00, -3.93330232e-01,
         -3.22833460e-01,  1.75937768e+00, -7.75308684e-01,
         -1.76944599e+00, -1.51097260e+00, -1.20680774e+00,
         -1.73889132e+00, -1.42804122e+00, -5.48924036e-02],
        [ 6.46632419e-01, -1.35605233e+00, -3.36165078e-01,
          6.03955462e-01,  5.59107583e-01, -1.28273835e+00,
          8.13147389e-01,  2.95909469e-01,  4.42907721e-01,
         -6.58491964e-01, -1.42127696e+00,  1.41993229e+00,
         -1.13108787e+00, -2.53058958e-01,  6.71854157e-01],
        [-2.86724427e-01,  9.60059719e-02,  4.87083029e-01,
          9.76149848e-01,  1.94237244e+00, -1.37927292e+00,
         -1.92046303e+00,  2.95646552e-01, -4.36871469e-01,
         -4.43716298e-02, -8.80300110e-01, -3.78712314e-01,
         -4.15833348e-01,  3.57217686e-01,  1.86924190e-02],
        [ 2.15352805e-01, -3.03591011e-01, -4.39560083e-02,
         -3.30899300e-01,  2.70672335e+00, -1.40415781e+00,
         -4.62566408e-01, -2.40885866e-01,  1.03235047e+00,
         -5.44886602e-01,  1.00041819e+00,  4.43386998e-02,
          1.59228942e+00,  2.70360448e-02, -1.41114726e+00],
        [-1.13652935e+00, -1.84405986e+00, -8.00289978e-01,
         -6.90404156e-01, -7.20117609e-01, -2.73355384e-01,
          1.82366803e+00, -1.47656402e+00,  2.38202131e-01,
          3.60639014e-01,  4.51554302e-01, -7.01127087e-01,
          8.00748702e-01, -2.00308694e-01,  7.46447262e-01],
        [-4.16291540e-01,  1.40242582e+00,  3.46181565e-01,
         -5.71527974e-01,  3.03380234e+00,  8.13872356e-01,
         -1.40564923e+00, -1.63723876e+00,  4.80347440e-01,
          1.36059731e-01, -1.04704007e+00,  1.12157284e-02,
          1.90707582e-01,  4.90512517e-01,  1.99992402e+00],
        [ 6.38352254e-01,  4.55680657e-01, -8.17495564e-01,
         -1.40684073e-02, -8.42757721e-01,  3.88889432e-01,
         -1.91784981e-01,  6.85430317e-02,  2.23678950e-01,
         -1.20770091e+00, -5.27465683e-01,  6.86072733e-01,
         -5.96890055e-01,  2.00105370e+00, -8.88444560e-01]],

       [[ 9.48355263e-02,  5.88988081e-01, -7.79159996e-02,
          1.48259291e-01, -2.48315491e-01, -8.25351441e-01,
          8.10340689e-02, -2.95969566e+00,  1.05786862e-01,
          1.00981694e+00,  7.06901564e-01,  4.14735170e-03,
          4.14740174e-01, -1.11519030e+00, -6.05681283e-01],
        [-1.10534571e+00,  2.73237859e-01, -1.52165479e+00,
          1.18887730e+00, -2.39843145e-01,  1.24958998e+00,
          2.03160494e-01,  3.71222703e-01,  3.77154916e-01,
         -7.77246673e-02,  1.39252084e+00,  1.30920674e+00,
         -4.30680296e-01,  8.51058145e-01,  9.66670743e-01],
        [-1.06357024e-01, -1.08218748e+00,  6.27544576e-01,
          3.02165323e-03,  8.11713215e-01,  1.05793739e+00,
          1.32340882e-01,  7.19809175e-01, -6.03702928e-01,
         -1.51822915e+00, -7.46210861e-01, -4.66532629e-01,
         -6.00629031e-01, -8.37204130e-02,  1.34180211e+00],
        [ 2.49568058e-01, -1.33695677e+00, -1.68341424e+00,
         -1.42280990e-01,  4.71678380e-01, -2.32368276e+00,
          3.39593777e-01,  8.62472475e-01, -2.22868415e-01,
          1.67037435e+00,  5.12412676e-01,  6.23899079e-01,
          1.05033722e+00,  6.53655702e-02, -1.92769950e+00],
        [ 3.65893717e-01,  1.40531683e+00,  8.32678939e-01,
          2.21836822e-01, -5.46111378e-01, -9.84827297e-01,
          2.60854285e-02, -1.54562917e+00,  9.50658672e-01,
         -1.87204435e+00,  4.31770743e-01,  9.06707472e-01,
         -7.42018079e-01,  8.05221959e-01, -2.12693545e+00],
        [ 2.45065786e-01,  1.08378963e+00, -4.28345985e-01,
          1.86798637e+00,  1.35331825e+00,  4.16283844e-01,
         -2.38362636e-01, -9.18803590e-01,  1.41255541e+00,
          7.23131489e-01, -4.59986953e-01, -4.23199736e-01,
         -1.29164427e+00,  4.30439332e-01, -5.17529037e-01],
        [-3.45924419e-01,  1.66806886e-01,  2.43593242e-01,
         -3.94435379e-01, -2.85796429e-01, -1.37446952e-02,
         -3.87600337e-01, -1.17343490e-01,  3.36106012e-01,
          1.59648339e-01, -1.40297781e+00,  6.47972711e-01,
         -4.42804918e-01,  4.54159307e-01, -1.68588253e+00],
        [ 9.98003704e-01, -5.65393235e-01, -7.86425021e-01,
          8.16046698e-01, -1.03896344e-02,  2.19772965e+00,
          2.98319580e-01,  4.04113270e-01, -2.27470418e-01,
         -9.11621134e-01,  1.67762399e+00,  1.48012514e+00,
         -7.11622191e-01, -9.62934314e-01,  1.48262032e+00],
        [ 6.04957860e-01, -6.04093487e-02,  7.45195834e-01,
          1.29679638e+00,  1.51397226e+00,  5.76606298e-01,
          7.82304578e-01,  1.53072466e+00, -1.03381727e+00,
         -4.92840430e-01,  1.30165064e-01,  1.32597622e-02,
         -1.22241044e-02, -1.36120420e-01,  2.27214230e-01],
        [-2.76490404e-01, -1.53391690e+00, -1.02473278e+00,
          2.82841488e-01,  2.32677983e+00, -9.61875746e-01,
         -2.30946584e-01,  7.50876646e-02, -9.84979831e-01,
         -7.61969070e-03,  1.87417138e-03,  8.72598506e-01,
          4.33807649e-01, -9.94222127e-01, -4.07187566e-01]],

       [[-2.33697218e-01,  1.97690233e+00,  6.16922376e-01,
          1.92451331e+00, -4.12326595e-01,  4.14147434e-01,
          7.46067908e-02, -3.40125491e-01,  8.12419201e-03,
         -3.86841870e-01,  2.97448262e-01,  1.02366511e+00,
         -5.75236176e-01,  2.06439117e+00, -4.06988862e-01],
        [ 2.31596999e-01, -1.04617331e+00,  1.90745821e-01,
         -2.67412442e+00, -7.73154532e-01,  7.17018262e-01,
          1.41406830e+00,  6.77691877e-02,  3.33045371e-02,
         -4.71177590e-01, -7.44786432e-01, -6.43868887e-01,
          9.50398005e-01, -5.20943728e-01,  1.61810413e+00],
        [-4.58061559e-01, -1.12728498e+00,  9.36963537e-01,
          3.76377635e-01,  5.86738772e-01, -1.97605673e+00,
          3.85751638e-01, -9.65650555e-01,  3.13528310e-01,
          6.20839042e-01,  1.05079330e+00, -7.84749850e-01,
          1.12086841e+00, -1.15735168e+00,  1.34324499e+00],
        [-6.48742794e-01, -4.35018284e-01, -1.18643143e+00,
          1.00715542e+00,  8.02773696e-01,  1.35652286e+00,
          1.60229350e+00,  6.13515987e-02, -1.24127935e-01,
          8.85078577e-01, -1.23538952e+00, -5.29518480e-01,
         -2.40757514e-01,  1.60038881e+00,  1.95196682e+00],
        [-5.29387505e-01, -1.17035199e+00, -1.59927845e-02,
         -1.44219970e-01, -3.45584339e-01, -7.94261164e-02,
          5.03379012e-01,  1.30629151e+00, -1.22598812e+00,
         -3.14806794e-01, -1.57563645e-01, -6.38165845e-01,
         -7.64021329e-01,  6.58099875e-01, -7.09957099e-01],
        [-1.29337204e+00,  4.00113871e-01,  1.51216838e-01,
          2.57928635e-01, -5.35101721e-01,  6.25163580e-01,
          1.68360078e-01,  1.26254777e-01, -3.80786616e-01,
          5.58094503e-02,  3.84443168e-01,  4.18665709e-01,
         -4.94103931e-01,  6.47300371e-01,  1.26496472e+00],
        [ 2.68779867e+00,  1.07143252e+00, -1.22861750e+00,
          1.33040680e-01,  3.52540694e-01,  7.83962281e-01,
         -4.42187974e-01,  7.32674848e-04,  6.65396429e-01,
          1.17813836e+00,  6.97254499e-02,  2.57492247e-03,
         -2.54447205e-01, -2.55960332e+00,  6.21629031e-01],
        [-4.37102102e-02,  5.21658832e-01,  4.53182541e-01,
         -1.37658413e+00, -4.17414374e-01,  1.04649355e+00,
         -8.56614060e-01, -2.51181623e-01, -3.93511361e-01,
         -1.65143764e-01,  1.26729402e+00,  1.37647596e+00,
         -1.02748466e+00,  6.77161410e-01, -7.40001275e-02],
        [ 3.37803652e-01, -1.49188529e+00, -1.51127838e+00,
          2.04308577e+00, -1.09488650e+00,  8.89274420e-01,
         -9.35774387e-01, -5.56294459e-01, -1.39972699e+00,
         -1.80683755e+00, -7.21009448e-01, -6.80275177e-01,
          6.10742525e-01, -4.91323991e-02, -5.57274852e-01],
        [ 1.92377474e-01, -4.56512361e-01,  7.13991822e-01,
         -3.98089571e-01, -8.12991718e-01,  1.78087108e+00,
         -8.52657486e-01,  9.13162753e-01, -5.87225528e-02,
         -5.45259768e-01, -1.48288194e-01, -1.42599497e-01,
         -2.06909411e+00,  2.06168856e-01, -6.58705439e-01]],

       [[ 1.28997352e+00,  2.65614541e-01,  5.47994495e-01,
          1.99929924e+00, -8.92210368e-01,  1.88938548e-01,
         -1.06886255e+00,  9.81890855e-02,  8.02247325e-01,
         -8.85450810e-01, -2.95389290e-01,  3.13068310e-01,
         -7.50409081e-01,  3.66340242e-01, -1.80647746e-01],
        [-1.53829751e-01, -1.73668727e+00,  1.22013304e+00,
          2.27179375e+00,  9.51639528e-01, -5.44372042e-01,
         -6.74864948e-01,  1.83728984e-01, -4.78181931e-01,
         -1.02037711e+00, -1.58095561e-01,  3.44117225e-01,
         -1.02708055e+00, -1.84102521e-01, -1.25228271e+00],
        [-1.02387733e-01, -4.53281831e-01, -7.75455110e-01,
         -1.95139497e+00,  7.27374904e-02,  5.32739041e-01,
          5.26455617e-01, -9.26361286e-01, -1.38315809e+00,
         -3.89860282e-01, -2.13967168e-01,  3.14865723e-01,
         -5.27934290e-01, -1.44261467e+00, -1.16825236e+00],
        [ 1.41212536e+00, -4.22576436e-01, -1.16042628e+00,
          8.64600374e-01,  6.67779269e-01, -1.22099302e-01,
         -1.16178483e+00, -2.32127412e-01,  1.12910075e+00,
          1.38194428e+00, -5.82450882e-01, -4.21236426e-01,
          1.19360930e-02,  2.47810408e+00, -1.07264735e-01],
        [ 2.94492770e-01,  3.44248037e-01,  6.74715500e-01,
          1.48162842e+00, -5.31265689e-01, -1.88307258e+00,
          1.10682851e+00,  4.28136450e-01, -1.41727881e-01,
          5.00349281e-01, -2.48507165e-01,  9.45209412e-02,
         -2.27234010e-01,  1.07060225e+00,  8.87330898e-02],
        [-2.05317969e-01, -1.36957999e+00, -1.23017735e-01,
         -5.74921071e-01, -2.77303013e-01,  1.06298500e+00,
         -1.24541048e+00,  1.08371909e+00, -1.02493929e+00,
         -1.78440425e+00, -1.14462079e+00,  4.54122452e-01,
         -9.61452192e-01, -1.18590316e+00,  7.86228484e-01],
        [ 6.35260473e-02,  1.17406729e+00, -1.97430151e-01,
          7.61789554e-01, -1.80466435e-02,  1.59144725e-01,
         -3.07606042e-01, -2.13630191e+00,  9.18158362e-01,
          1.03823953e-03, -1.62898889e+00,  1.82462715e-01,
         -1.06586226e+00, -2.26529827e-01,  1.23485628e+00],
        [-7.84352482e-02,  1.13139471e-02,  1.30516249e+00,
          7.61754826e-01, -7.38331798e-01, -7.29851310e-01,
          4.76941232e-02,  7.40084580e-01, -1.23196080e+00,
          1.56763757e+00,  1.97004588e-01,  1.49721374e+00,
          4.91749019e-01, -6.94690669e-01,  6.31809188e-01],
        [ 5.50819238e-01, -1.39255213e+00, -5.14665226e-01,
         -7.60869809e-01, -1.89347550e-01, -2.48994205e+00,
         -2.02614322e+00,  1.21537012e+00, -3.30797956e-02,
         -1.92058777e-01,  1.11697915e+00,  5.17124279e-01,
          3.66558369e-01, -3.68137478e-01, -1.55584113e+00],
        [ 1.58165360e+00,  3.14927389e-01,  1.30186530e+00,
         -1.75469206e+00,  3.83421320e-01,  1.06364180e+00,
         -8.40067843e-01,  1.71747247e-02, -9.16346852e-01,
         -2.24108781e-01,  8.20880281e-01, -5.78435737e-02,
         -6.25960275e-01,  1.75238060e+00,  5.33945738e-01]],

       [[-9.20284798e-01,  1.09284417e+00,  1.87448261e-01,
         -2.28859447e+00, -1.78240453e+00,  1.41257659e+00,
          2.38029513e+00, -3.71963979e-01,  3.20013027e-01,
          1.36369676e+00,  1.44972430e+00, -1.62003864e+00,
         -6.48428058e-01, -2.59707812e+00, -1.86491823e+00],
        [-2.84354899e-01, -1.62656588e+00, -1.12330160e+00,
          2.90818439e-01,  1.19233999e+00, -1.35588102e+00,
          4.54850127e-02, -8.12460429e-01, -7.47119526e-02,
         -3.56382113e-01,  9.76175740e-01,  7.88067478e-01,
          1.96994503e+00, -8.32847329e-01, -2.94770925e-01],
        [-6.97079155e-01, -8.67425803e-01, -9.12395073e-01,
          6.11260499e-01,  6.24693091e-01,  1.12894959e+00,
          6.66480557e-01, -4.12409353e-01, -3.14364830e-01,
         -1.43302777e+00, -5.40121168e-01, -6.34161709e-01,
          3.48996724e-01,  1.37852482e+00, -7.82809518e-01],
        [ 5.94345717e-01, -4.48683718e-01,  8.79009825e-01,
          2.22085197e-01, -3.14925181e-01, -2.67599908e-01,
         -1.30929144e-01,  2.49168239e-01,  1.26368646e+00,
          9.75919979e-01,  2.29114547e+00, -1.13859910e+00,
         -3.21745583e-01,  1.42370081e+00,  1.52351829e-01],
        [ 4.76943630e-01,  6.74953003e-01,  1.91820539e-01,
          1.91591971e+00, -1.52394686e+00,  1.76252795e-01,
          6.54101956e-01,  2.46981736e-01,  6.44425458e-01,
         -9.78830727e-01, -2.13581966e+00, -1.15147778e+00,
         -3.16278139e-01,  2.32453462e+00, -3.99712983e-01],
        [ 2.76309252e-01,  1.57981994e+00,  7.68703596e-01,
          4.83290509e-02,  6.25375327e-01,  1.67739795e+00,
         -5.57190177e-01,  2.03198999e+00,  9.10275238e-01,
          1.49827115e+00, -2.87150088e-01,  1.08312095e+00,
          4.39048588e-01,  3.14272823e-01,  1.55972792e-02],
        [-1.38486088e+00, -2.87131375e-01, -9.17878602e-01,
          2.04303915e-01, -6.56706338e-01, -6.05093021e-01,
         -4.93862743e-01, -1.43410528e+00, -1.03803836e+00,
         -2.35450768e-01, -3.33811201e-01, -2.96044783e-01,
         -1.88517601e+00, -8.00676861e-01, -9.61586318e-01],
        [ 2.46570954e-01,  4.64957508e-01, -7.77403396e-01,
         -1.44860979e+00, -1.27637166e+00,  3.42290924e-01,
          6.33531938e-01,  1.76078809e+00, -3.60788973e-01,
          4.46364010e-01, -1.53847932e+00,  1.06587933e+00,
         -6.07103956e-01, -2.42052844e+00, -1.79180372e-01],
        [ 2.81792033e-01,  1.88113038e+00, -1.29137405e+00,
         -7.15264046e-01,  1.31787867e+00,  4.89483605e-01,
         -1.24460359e+00,  6.69344061e-01, -3.47853838e-01,
          3.65859811e-01,  6.38238041e-01, -3.87115646e-01,
         -1.29573742e+00, -5.07774945e-02,  7.06404794e-01],
        [-5.15127311e-01, -6.19543633e-01,  7.18349639e-01,
         -1.01095240e+00, -5.38631506e-01,  1.16225994e+00,
          1.33324950e+00, -1.23990024e+00, -1.13970211e+00,
         -8.70414599e-01, -1.66277051e+00,  2.25408986e+00,
         -6.06171312e-01, -7.53096174e-01,  8.37460477e-01]]])
data.sum(axis=0).shape

( 10 ,   15 ) \displaystyle \left( 10, \ 15\right) (10, 15)

 data.sum(axis=(0, 2)).shape

( 10 ) \displaystyle \left( 10\right) (10)

data.sum()

− 9.639063538382969 \displaystyle -9.639063538382969 9.639063538382969

data = np.arange(1,10).reshape(3,3)
data
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

下面我们来看axis不同时结果的变化

data.sum()#所有的矩阵元相加
45
data.sum(axis=0) #每一列各自相加,得到的和。
array([12, 15, 18])
data.sum(axis=1)#每一行相加后得到的和。
array([ 6, 15, 24])

如果所示不同的和的结果。

from IPython.display import Image
from IPython.core.display import HTML
Image(url= "./SumAlongAxile.png")

你可能感兴趣的:(python,numpy,scipy,python科学计算)