python-learning-第二季-数据处理numpy

python

https://www.bjsxt.com/down/8468.html

numpy-科学计算基础库

例子:

import numpy as np

#创建数组

a = np.arange(10)

print(a)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[0 1 2 3 4 5 6 7 8 9]

<class 'numpy.ndarray'>

Process finished with exit code 0

对列表中的元素开平方

之前的方法为:

import math

b = [3,4,9]

#定义存储开平方结果的列表

result = []

for i in b:

result.append(math.sqrt(i))

print(result)

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[1.7320508075688772, 2.0, 3.0]

Process finished with exit code 0

现在使用numpy速度更快,更方便。对ndarray对象类型进行向量处理:

import numpy as np

b = np.array([3,4,9])

print(np.sqrt(b))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[1.73205081 2. 3. ]

Process finished with exit code 0

array进行创建数组

一维数组:

import numpy as np

a = np.array([3,4,9])

print(a)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[3 4 9]

<class 'numpy.ndarray'>

Process finished with exit code 0

a.shape 为(3,)

二维数组:

import numpy as np

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

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[1 2 3]

[2 3 4]

[3 4 5]]

(3, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

三维数组:

import numpy as np

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

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[[1 2 3]

[2 3 4]

[3 4 5]]]

(1, 3, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

array函数中dtype参数的使用,设置数组元素类型:

import numpy as np

a = np.array([3,4,9], dtype=float)

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[3. 4. 9.]

(3,)

<class 'numpy.ndarray'>

Process finished with exit code 0

array函数中ndmin参数的使用,说明最小维度为几,传入的值如果维度不够,就会在前面加维度1:

import numpy as np

a = np.array([3,4,9], dtype=float, ndmin=3)

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[[3. 4. 9.]]]

(1, 1, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

arange函数:

import numpy as np

a = np.arange(0, 6, dtype=float)

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[0. 1. 2. 3. 4. 5.]

(6,)

<class 'numpy.ndarray'>

Process finished with exit code 0

随机创建数组 

import numpy as np

a = np.random.random(10) #创建size=10的10个随机数[0.0, 1.0)

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[0.70224679 0.12333366 0.7615228 0.48488729 0.55049969 0.88189077

0.88448342 0.6340702 0.55846358 0.03856909]

(10,)

<class 'numpy.ndarray'>

Process finished with exit code 0

创建二维的:

import numpy as np

a = np.random.random(size=(3,4)) #3行4列

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[0.75452762 0.06511761 0.28876795 0.33917503]

[0.70055853 0.05899591 0.6951374 0.48631801]

[0.79725514 0.52645849 0.60955185 0.94158767]]

(3, 4)

<class 'numpy.ndarray'>

Process finished with exit code 0

三维的:

import numpy as np

a = np.random.random(size=(3,4,2)) #3行4列

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[[0.09459011 0.06400518]

[0.63932067 0.90659996]

[0.25010503 0.00512396]

[0.93533579 0.15083294]]

[[0.68609045 0.53156758]

[0.71763029 0.43475711]

[0.38447034 0.23069394]

[0.48814115 0.65881832]]

[[0.91488505 0.58573524]

[0.73130286 0.89564597]

[0.31657241 0.63555136]

[0.60898115 0.71098613]]]

(3, 4, 2)

<class 'numpy.ndarray'>

Process finished with exit code 0

随机整数: 

dtype参数默认为np.int, 也可以设置为np.int64

import numpy as np

a = np.random.randint(1, 10, 5)

print(a)

print(a.shape)

print(a.dtype)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[4 1 3 3 8]

(5,)

int64

<class 'numpy.ndarray'>

Process finished with exit code 0

发现实际默认的跟讲的相反

import numpy as np

a = np.random.randint(1, 10, (3,3))

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[4 5 3]

[1 6 8]

[2 7 6]]

(3, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

import numpy as np

a = np.random.randint(1, 10, (4,4))

print(a)

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[5 5 3 1]

[3 8 1 6]

[7 7 2 2]

[6 4 6 9]]

(4, 4)

<class 'numpy.ndarray'>

Process finished with exit code 0

标准正态分布 

一维:

import numpy as np

a = np.random.randn(4)

print(a)

print(a.shape)

print(a.dtype)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[-0.07124224 -0.23748904 -0.66759342 0.78374469]

(4,)

float64

<class 'numpy.ndarray'>

Process finished with exit code 0

二维:

import numpy as np

a = np.random.randn(2,3)

print(a)

print(a.shape)

print(a.dtype)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[-1.01226872 -1.32755441 -2.26288293]

[ 0.94123471 1.04692986 0.85342488]]

(2, 3)

float64

<class 'numpy.ndarray'>

Process finished with exit code 0

三维:

import numpy as np

a = np.random.randn(2,3,2)

print(a)

print(a.shape)

print(a.dtype)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[[-0.10896308 -0.5064629 ]

[-0.39916753 0.35598577]

[-0.41677605 -0.41341541]]

[[-1.12973198 0.26209766]

[ 0.24671435 -0.2798904 ]

[ 0.82366767 0.76207401]]]

(2, 3, 2)

float64

<class 'numpy.ndarray'>

Process finished with exit code 0

指定期望和方差的正太分布 

默认期望为0.0,方差为1.0

import numpy as np

a = np.random.normal(loc=3, scale=4, size=(2,3))

print(a)

print(a.shape)

print(a.dtype)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[-1.67615131 1.55790654 1.159349 ]

[-0.84205285 3.53045653 1.2121123 ]]

(2, 3)

float64

<class 'numpy.ndarray'>

Process finished with exit code 0

ndarray对象的属性

import numpy as np

a = np.random.normal(loc=3, scale=4, size=(2,3))

print(a)

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 5.34933995 -1.68167826 4.93713342]

[ 4.68725164 5.71788803 5.41723111]]

2

6

float64

8

(2, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

其他方式创建数组 

import numpy as np

a = np.zeros((5,))

#等价于a = np.zeros(5)

print(a)

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[0. 0. 0. 0. 0.]

1

5

float64

8

(5,)

<class 'numpy.ndarray'>

Process finished with exit code 0

import numpy as np

a = np.ones((2,3))

print(a)

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[1. 1. 1.]

[1. 1. 1.]]

2

6

float64

8

(2, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

  

import numpy as np

a = np.empty((2,3))

print(a)

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[-3.10503618e+231 -2.32036278e+077 1.48219694e-323]

[ 0.00000000e+000 0.00000000e+000 4.17201348e-309]]

2

6

float64

8

(2, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

import numpy as np

a = np.linspace(1, 10)

print(a)

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[ 1. 1.18367347 1.36734694 1.55102041 1.73469388 1.91836735

2.10204082 2.28571429 2.46938776 2.65306122 2.83673469 3.02040816

3.20408163 3.3877551 3.57142857 3.75510204 3.93877551 4.12244898

4.30612245 4.48979592 4.67346939 4.85714286 5.04081633 5.2244898

5.40816327 5.59183673 5.7755102 5.95918367 6.14285714 6.32653061

6.51020408 6.69387755 6.87755102 7.06122449 7.24489796 7.42857143

7.6122449 7.79591837 7.97959184 8.16326531 8.34693878 8.53061224

8.71428571 8.89795918 9.08163265 9.26530612 9.44897959 9.63265306

9.81632653 10. ]

1

50

float64

8

(50,)

<class 'numpy.ndarray'>

Process finished with exit code 0

  

上面注释写错了,是底数为10,但是倍数就不一定了,比如下面的例子的意思就是在值范围[10,10^10]中间取20个数,使他们之间的倍数是相同的:

import numpy as np

a = np.logspace(1, 10, 20, dtype=int)

print(a)

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[ 10 29 88 263 784 2335

6951 20691 61584 183298 545559 1623776

4832930 14384498 42813323 127427498 379269019 1128837891

3359818286 10000000000]

1

20

int64

8

(20,)

<class 'numpy.ndarray'>

Process finished with exit code 0

一维数组的切片索引: 

import numpy as np

a = np.arange(10)

print(a)

print(a[0])

print(a[-1])

print(a[:])

print(a[2:6:2])

print(a[::-1])

print(a[-3:-1:1])

print(a[-1:-3:-1])

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[0 1 2 3 4 5 6 7 8 9]

0

9

[0 1 2 3 4 5 6 7 8 9]

[2 4]

[9 8 7 6 5 4 3 2 1 0]

[7 8]

[9 8]

1

10

int64

8

(10,)

<class 'numpy.ndarray'>

Process finished with exit code 0

二维的切片和索引

[行的切片,列的切片 ] = [start:stop:step,start:stop:step]

import numpy as np

a = np.arange(1, 13)

a = a.reshape(4,3)

print(a)

#等价于

print(a[:,:])

print()

print(a[0])

print(a[1][2])

print(a[:][2])

#得到第二行,等价于

print(a[2])

#也等价于下面的写法

print(a[2][:])

print()

#想要得到第二列为:

print(a[:,2])

#得到二三行的一二列

print(a[2:4,1:3])

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

[1 2 3]

6

[7 8 9]

[7 8 9]

[7 8 9]

[ 3 6 9 12]

[[ 8 9]

[11 12]]

2

12

int64

8

(4, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

使用坐标获取:

import numpy as np

a = np.arange(1, 13)

a = a.reshape(4,3)

print(a)

#第三行第二列

print(a[2,1])

#等价于

print(a[2][1])

print()

#同时获得第三行第二列,第四行第一列

print(np.array((a[2,1],a[3,0])))

#等价于

print(a[(2,3),(1,0)])

print()

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

8

8

[ 8 10]

[ 8 10]

2

12

int64

8

(4, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

索引为负数:

import numpy as np

a = np.arange(1, 13)

a = a.reshape(4,3)

print(a)

#获取最后一行

print(a[-1])

#行进行倒序

print(a[::-1, :])

#行列都倒序

print(a[::-1, ::-1])

print()

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

[10 11 12]

[[10 11 12]

[ 7 8 9]

[ 4 5 6]

[ 1 2 3]]

[[12 11 10]

[ 9 8 7]

[ 6 5 4]

[ 3 2 1]]

2

12

int64

8

(4, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

数组的复制

浅拷贝:

import numpy as np

a = np.arange(1, 13).reshape(4,3)

print(a)

print(id(a))

#获取一二行一二列

sub_a = a[:2,:2]

print(sub_a)

print(id(sub_a))

#修改切片的值

sub_a[0][0] = 100

print(a)

print(sub_a)#结果可见会影响原来数组,浅拷贝

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

4495540752

[[1 2]

[4 5]]

4496167680

[[100 2 3]

[ 4 5 6]

[ 7 8 9]

[ 10 11 12]]

[[100 2]

[ 4 5]]

2

12

int64

8

(4, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

深拷贝——copy方法

import numpy as np

a = np.arange(1, 13).reshape(4,3)

print(a)

print(id(a))

#获取一二行一二列

sub_a = np.copy(a[:2,:2])

print(sub_a)

print(id(sub_a))

#修改切片的值

sub_a[0][0] = 200

print(a)

print(sub_a)#结果可见不会影响原来数组,深拷贝

print(a.ndim)

print(a.size)

print(a.dtype) #float64 = 8个字节

print(a.itemsize) #以字节为单位

print(a.shape)

print(type(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

4347974160

[[1 2]

[4 5]]

4426006000

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

[[200 2]

[ 4 5]]

2

12

int64

8

(4, 3)

<class 'numpy.ndarray'>

Process finished with exit code 0

修改数组的维度 

import numpy as np

#一维成二维

a = np.arange(1, 13).reshape(4,3)

print(a)

#一维变三维

c = np.reshape(a, (2,2,3))

print(c)

#多维成一维:

d = a.reshape(12)

print(d)

e = a.reshape(-1)

print(e)

print()

f = c.ravel()

print(f)

g = c.flatten()

print(g)

print()

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

[[[ 1 2 3]

[ 4 5 6]]

[[ 7 8 9]

[10 11 12]]]

[ 1 2 3 4 5 6 7 8 9 10 11 12]

[ 1 2 3 4 5 6 7 8 9 10 11 12]

[ 1 2 3 4 5 6 7 8 9 10 11 12]

[ 1 2 3 4 5 6 7 8 9 10 11 12]

数组的拼接 

 垂直的

import numpy as np

#一维成二维

a = np.arange(1, 7).reshape(2,3)

b = np.arange(7, 13).reshape(2,3)

print(a)

print(b)

#水平拼接

c = np.hstack((a,b))

print(c)

#垂直拼接

d = np.vstack((a,b))

print(d)

print()

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[1 2 3]

[4 5 6]]

[[ 7 8 9]

[10 11 12]]

[[ 1 2 3 7 8 9]

[ 4 5 6 10 11 12]]

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

import numpy as np

#一维成二维

a = np.arange(1, 7).reshape(2,3)

b = np.arange(7, 13).reshape(2,3)

print(a)

print(b)

#垂直方向

e = np.concatenate((a,b))

print(e)

#水平方向

f = np.concatenate((a,b), axis=1)

print(f)

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[1 2 3]

[4 5 6]]

[[ 7 8 9]

[10 11 12]]

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

[[ 1 2 3 7 8 9]

[ 4 5 6 10 11 12]]

三维数组有三个轴=0,1,2

import numpy as np

#一维成二维

a = np.arange(1, 7).reshape(1,2,3)

b = np.arange(7, 13).reshape(1,2,3)

print(a)

print(b)

print()

#垂直方向

e = np.concatenate((a,b))

print(e)

print(e.shape)

#水平方向

f = np.concatenate((a,b), axis=1)

print(f)

print(f.shape)

g = np.concatenate((a,b), axis=2)

print(g)

print(g.shape)

print()

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[[1 2 3]

[4 5 6]]]

[[[ 7 8 9]

[10 11 12]]]

[[[ 1 2 3]

[ 4 5 6]]

[[ 7 8 9]

[10 11 12]]]

(2, 2, 3)

[[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]]

(1, 4, 3)

[[[ 1 2 3 7 8 9]

[ 4 5 6 10 11 12]]]

(1, 2, 6)

数组的分隔 

import numpy as np

#一维成二维

x = np.arange(1, 7)

a = np.split(x,3) #平均分割成3份,值个数够分隔成这么多,否则报错,返回一个列表对象

print(a)

print(a[0])

print(type(a))

b = np.split(x,[3,5]) #以索引位置值3和值5作为分割线,按位置分割

print(b)

print(type(b))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[array([1, 2]), array([3, 4]), array([5, 6])]

[1 2]

<class 'list'>

[array([1, 2, 3]), array([4, 5]), array([6])]

<class 'list'>

Process finished with exit code 0

 二维数组:

import numpy as np

#一维成二维

x = np.arange(1, 17).reshape((4,4))

print(x)

print()

#垂直分隔,行分隔,平均分隔,

a = np.split(x, 2, axis=0) #平均分割成2份,值个数够分隔成这么多,否则报错,返回一个列表对象

print(a)

print(a[0])

print(type(a))

print()

#垂直分隔,行分隔,行索引位置分隔,

b = np.split(x,[1,2], axis=0) #以值3和值5作为分割线

print(b)

print(type(b))

print()

#水平方向,列分隔,平均分隔

c = np.split(x, 2, axis=1) #平均分割成2份,值个数够分隔成这么多,否则报错,返回一个列表对象

print(c)

print(type(c))

print()

#水平方向,列分隔,位置分隔

d = np.split(x,[2,3], axis=1) #以列索引值3和值5作为分割线

print(d)

print(type(d))

print()

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]

[13 14 15 16]]

[array([[1, 2, 3, 4],

[5, 6, 7, 8]]), array([[ 9, 10, 11, 12],

[13, 14, 15, 16]])]

[[1 2 3 4]

[5 6 7 8]]

<class 'list'>

[array([[1, 2, 3, 4]]), array([[5, 6, 7, 8]]), array([[ 9, 10, 11, 12],

[13, 14, 15, 16]])]

<class 'list'>

[array([[ 1, 2],

[ 5, 6],

[ 9, 10],

[13, 14]]), array([[ 3, 4],

[ 7, 8],

[11, 12],

[15, 16]])]

<class 'list'>

[array([[ 1, 2],

[ 5, 6],

[ 9, 10],

[13, 14]]), array([[ 3],

[ 7],

[11],

[15]]), array([[ 4],

[ 8],

[12],

[16]])]

<class 'list'>

Process finished with exit code 0

hsplit()方法

 也可以按位置分割,就是省略了axis参数:

 vsplit()方法

上面结果有错,应为:

1 2 3

4 5 6

7 8 9

10 11 12

上面的例子等价于:

import numpy as np

#一维成二维

x = np.arange(1, 17).reshape((4,4))

print(x)

print()

#垂直分隔,行分隔,平均分隔,

a = np.vsplit(x, 2) #平均分割成2份,值个数够分隔成这么多,否则报错,返回一个列表对象

print(a)

print(a[0])

print(type(a))

print()

#垂直分隔,行分隔,行索引位置分隔,

b = np.vsplit(x,[1,2]) #以值3和值5作为分割线

print(b)

print(type(b))

print()

#水平方向,列分隔,平均分隔

c = np.hsplit(x, 2) #平均分割成2份,值个数够分隔成这么多,否则报错,返回一个列表对象

print(c)

print(type(c))

print()

#水平方向,列分隔,位置分隔

d = np.hsplit(x,[2,3]) #以列索引值3和值5作为分割线

print(d)

print(type(d))

print()

数组的转置——transpose

import numpy as np

a = np.arange(1,25).reshape((4,6))

print(a, a.shape)

print()

print('转置后a[i][j] -> a[j][i]')

b = a.transpose()

print(b, b.shape)

print()

#对二维来说,还可以使用.T

print(a.T)

print()

#numpy中的transpose方法

print(np.transpose(a))

print()

#多维数组进行转置

c = a.reshape((2,3,4))

print(c, c.shape)

print()

print('a[i][j][k] -> a[k][j][i]')

d = np.transpose(c)

print(d, d.shape)

print()

#指定维度位置的变换

e = np.transpose(c, (1,0,2)) #即a[i][j][k] -> a[j][i][k]

print(e, e.shape)

print()

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[ 1 2 3 4 5 6]

[ 7 8 9 10 11 12]

[13 14 15 16 17 18]

[19 20 21 22 23 24]] (4, 6)

转置后a[i][j] -> a[j][i]

[[ 1 7 13 19]

[ 2 8 14 20]

[ 3 9 15 21]

[ 4 10 16 22]

[ 5 11 17 23]

[ 6 12 18 24]] (6, 4)

[[ 1 7 13 19]

[ 2 8 14 20]

[ 3 9 15 21]

[ 4 10 16 22]

[ 5 11 17 23]

[ 6 12 18 24]]

[[ 1 7 13 19]

[ 2 8 14 20]

[ 3 9 15 21]

[ 4 10 16 22]

[ 5 11 17 23]

[ 6 12 18 24]]

[[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]]

[[13 14 15 16]

[17 18 19 20]

[21 22 23 24]]] (2, 3, 4)

a[i][j][k] -> a[k][j][i]

[[[ 1 13]

[ 5 17]

[ 9 21]]

[[ 2 14]

[ 6 18]

[10 22]]

[[ 3 15]

[ 7 19]

[11 23]]

[[ 4 16]

[ 8 20]

[12 24]]] (4, 3, 2)

[[[ 1 2 3 4]

[13 14 15 16]]

[[ 5 6 7 8]

[17 18 19 20]]

[[ 9 10 11 12]

[21 22 23 24]]] (3, 2, 4)

Process finished with exit code 0

函数1

算术函数-广播机制

import numpy as np

a = np.arange(9, dtype=float).reshape(3,3)

b = np.array([10,10,10])

print('加法')

print(np.add(a,b))

print(a+b)

print()

print('减法')

print(np.subtract(b,a))

print(b-a)

print()

print('乘法')

print(np.multiply(a,b))

print(a*b)

print()

print('除法')

print(np.divide(a,b))

print(a/b)

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

加法

[[10. 11. 12.]

[13. 14. 15.]

[16. 17. 18.]]

[[10. 11. 12.]

[13. 14. 15.]

[16. 17. 18.]]

减法

[[10. 9. 8.]

[ 7. 6. 5.]

[ 4. 3. 2.]]

[[10. 9. 8.]

[ 7. 6. 5.]

[ 4. 3. 2.]]

乘法

[[ 0. 10. 20.]

[30. 40. 50.]

[60. 70. 80.]]

[[ 0. 10. 20.]

[30. 40. 50.]

[60. 70. 80.]]

除法

[[0. 0.1 0.2]

[0.3 0.4 0.5]

[0.6 0.7 0.8]]

[[0. 0.1 0.2]

[0.3 0.4 0.5]

[0.6 0.7 0.8]]

Process finished with exit code 0

使用函数的好处是可以指定输出结果

import numpy as np

a = np.arange(9, dtype=float).reshape(3,3)

print(a)

y = np.empty((3,3))

print(y) #刚好保存的是之前的值

np.multiply(a,10, out=y)

print(y)

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[[0. 1. 2.]

[3. 4. 5.]

[6. 7. 8.]]

[[0. 1. 2.]

[3. 4. 5.]

[6. 7. 8.]]

[[ 0. 10. 20.]

[30. 40. 50.]

[60. 70. 80.]]

Process finished with exit code 0

数学函数

import numpy as np

a = np.array([0,30,45,60,90])

print(np.sin(a*np.pi/180))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[0. 0.5 0.70710678 0.8660254 1. ]

Process finished with exit code 0

四舍五入:

import numpy as np

a = np.array([1.0, 4.55, 123, 0.567, 25.532])

print(np.around(a))

print(np.around(a, decimals=1))

print(np.around(a, decimals=-1))

print(np.floor(a))

print(np.ceil(a))

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

[ 1. 5. 123. 1. 26.]

[ 1. 4.6 123. 0.6 25.5]

[ 0. 0. 120. 0. 30.]

[ 1. 4. 123. 0. 25.]

[ 1. 5. 123. 1. 26.]

Process finished with exit code 0

统计函数

import numpy as np

a = np.array([2,3,5,4])

b = np.array([2,2,3,3])

print(np.sum(a))

print(np.prod(a))

print(np.mean(a))

print(np.std(a))

print(np.var(a))

print()

#多维的都可以指定轴

print(np.median(a)) #如果顺序是乱的,那么会自己排序

d = np.arange(1,13).reshape(3,4)

print(d)

print(np.median(d, axis=0)) #垂直轴

print(np.median(d, axis=1)) #水平轴

print()

print(np.power(a,b))

print(np.power(a,2))

print(np.min(a))

print(np.max(a))

print(np.argmin(a))

print(np.argmax(a))

print(np.exp(a)) #e^a

c = np.array([10,10,np.e])

print(np.log(c)) #以e为底数的对数

print()

x = np.arange(5)

print(x)

y = np.zeros(10)

print(y)

np.power(x,2, out=y[1:6]) #指明存放位置

print(y)

返回:

/Users/user/PycharmProjects/python3/venv/bin/python /Users/user/PycharmProjects/python3/test.py

14

120

3.5

1.118033988749895

1.25

3.5

[[ 1 2 3 4]

[ 5 6 7 8]

[ 9 10 11 12]]

[5. 6. 7. 8.]

[ 2.5 6.5 10.5]

[ 4 9 125 64]

[ 4 9 25 16]

2

5

0

2

[ 7.3890561 20.08553692 148.4131591 54.59815003]

[2.30258509 2.30258509 1. ]

[0 1 2 3 4]

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

[ 0. 0. 1. 4. 9. 16. 0. 0. 0. 0.]

Process finished with exit code 0

以上是 python-learning-第二季-数据处理numpy 的全部内容, 来源链接: utcz.com/z/389357.html

回到顶部