Skip to content

Numpy 学习笔记

python
import numpy as np
import random
import time
python
data = np.array([[1,5,3,4,8,5],[4,2,1,8,6,21],[6,4,23,4,87,5],[45,3,12,4,58,5]])

效率对比

python
a =[]
for i in range(100000000):
    a.append(random.random())
b = np.array(a)
python
t1 = time.time()
sum1 = sum(a)
t2 = time.time()

t3 = time.time()
sum2 = np.sum(b)
t4 = time.time()

print(t2 - t1, t4 - t3)

0.4817538261413574 0.15558481216430664

ndarray属性

python
data

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [45, 3, 12, 4, 58, 5]])

python
data.shape

(4, 6)

python
data.ndim

2

python
data.size

24

python
data.dtype

dtype('int32')

python
data.itemsize

4

生成数组

python
# 生成0和1的数据
np.zeros(shape=(3,4),dtype='int32')

array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]])

python
np.ones(shape=(3,4))

array([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]])

从现有数组生成

python
data

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [45, 3, 12, 4, 58, 5]])

python
data2 = np.asarray(data) # 浅拷贝
data2

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [45, 3, 12, 4, 58, 5]])

python
data3 = np.copy(data) # 深拷贝
data3

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [45, 3, 12, 4, 58, 5]])

python
data[3][2] = 111111
python
data2

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [ 45, 3, 111111, 4, 58, 5]])

python
data3

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [45, 3, 12, 4, 58, 5]])

生成固定范围的数组

python
np.linspace(0,10,100)

array([ 0. , 0.1010101 , 0.2020202 , 0.3030303 , 0.4040404 , 0.50505051, 0.60606061, 0.70707071, 0.80808081, 0.90909091, 1.01010101, 1.11111111, 1.21212121, 1.31313131, 1.41414141, 1.51515152, 1.61616162, 1.71717172, 1.81818182, 1.91919192, 2.02020202, 2.12121212, 2.22222222, 2.32323232, 2.42424242, 2.52525253, 2.62626263, 2.72727273, 2.82828283, 2.92929293, 3.03030303, 3.13131313, 3.23232323, 3.33333333, 3.43434343, 3.53535354, 3.63636364, 3.73737374, 3.83838384, 3.93939394, 4.04040404, 4.14141414, 4.24242424, 4.34343434, 4.44444444, 4.54545455, 4.64646465, 4.74747475, 4.84848485, 4.94949495, 5.05050505, 5.15151515, 5.25252525, 5.35353535, 5.45454545, 5.55555556, 5.65656566, 5.75757576, 5.85858586, 5.95959596, 6.06060606, 6.16161616, 6.26262626, 6.36363636, 6.46464646, 6.56565657, 6.66666667, 6.76767677, 6.86868687, 6.96969697, 7.07070707, 7.17171717, 7.27272727, 7.37373737, 7.47474747, 7.57575758, 7.67676768, 7.77777778, 7.87878788, 7.97979798, 8.08080808, 8.18181818, 8.28282828, 8.38383838, 8.48484848, 8.58585859, 8.68686869, 8.78787879, 8.88888889, 8.98989899, 9.09090909, 9.19191919, 9.29292929, 9.39393939, 9.49494949, 9.5959596 , 9.6969697 , 9.7979798 , 9.8989899 , 10. ])

python
np.arange(1,30,3)

array([ 1, 4, 7, 10, 13, 16, 19, 22, 25, 28])

生成随机数组

python
dataR = np.random.uniform(low=-1,high=1,size=10000)
dataR

array([-0.57371971, 0.92439957, -0.75191664, ..., 0.36443061, 0.13864544, -0.29427612])

python
import matplotlib.pyplot as plt
plt.figure(figsize=(20,8),dpi=80)
plt.hist(dataR,1000)
plt.show()

python
# 正态分布
dataZT = np.random.normal(loc=2, scale=0.1, size=1000000)
python
plt.figure(figsize=(20,8),dpi=80)
plt.hist(dataZT,1000)
plt.show()

行列反转

python
data

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [ 45, 3, 111111, 4, 58, 5]])

python
data.T

array([[ 1, 4, 6, 45], [ 5, 2, 4, 3], [ 3, 1, 23, 111111], [ 4, 8, 4, 4], [ 8, 6, 87, 58], [ 5, 21, 5, 5]])

数组去重

python
data

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [ 45, 3, 111111, 4, 58, 5]])

python
np.unique(data)

array([ 1, 2, 3, 4, 5, 6, 8, 21, 23, 45, 58, 87, 111111])

ndarray 运算

逻辑运算

python
data > 2

array([[False, True, True, True, True, True], [ True, False, False, True, True, True], [ True, True, True, True, True, True], [ True, True, True, True, True, True]])

python
np.all(data > 2) # 传bool值,全为true才返回true

False

python
np.any(data > 2) # 有一个为true就返回true

True

python
np.where(data > 2, 100, 200) # bool值,true的位置的值,false的位置的值

array([[200, 100, 100, 100, 100, 100], [100, 200, 200, 100, 100, 100], [100, 100, 100, 100, 100, 100], [100, 100, 100, 100, 100, 100]])

python
 # 复杂逻辑
np.where(np.logical_and(data > 2, data < 50), 100, 200)

array([[200, 100, 100, 100, 100, 100], [100, 200, 200, 100, 100, 100], [100, 100, 100, 100, 200, 100], [100, 100, 200, 100, 200, 100]])

python
np.where(np.logical_or(data < 2, data > 50), 100, 200)

array([[100, 200, 200, 200, 200, 200], [200, 200, 100, 200, 200, 200], [200, 200, 200, 200, 100, 200], [200, 200, 100, 200, 100, 200]])

统计运算

python
data.max()
# 同
np.max(data)

111111

python
data.max(axis = 0) # axis = 0 按列

array([ 45, 5, 111111, 8, 87, 21])

python
data.argmax(axis = 1) # axis = 1 按行

array([4, 5, 4, 2], dtype=int64)

数组间运算

数组与数

python
data

array([[ 1, 5, 3, 4, 8, 5], [ 4, 2, 1, 8, 6, 21], [ 6, 4, 23, 4, 87, 5], [ 45, 3, 111111, 4, 58, 5]])

python
data + 10

array([[ 11, 15, 13, 14, 18, 15], [ 14, 12, 11, 18, 16, 31], [ 16, 14, 33, 14, 97, 15], [ 55, 13, 111121, 14, 68, 15]])

数组与数组

python
data1 = np.zeros(shape=(2,5),dtype='int32')
python
data2 = np.ones(shape=(2,5),dtype='int32')
python
data1 - data2

array([[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1]])

python
# 矩阵乘法
data1 = np.array([[3,2],[3,2],[3,2],[3,2],[3,2],[3,2],[3,2],[3,2]])
data2 = np.array([[1.5],[5.2]])
np.matmul(data1,data2)
# 同 np.dot(data1,data2)
# 同 data1 @ data2

array([[14.9], [14.9], [14.9], [14.9], [14.9], [14.9], [14.9], [14.9]])

合并、分割

合并

axis=0表示跨行,axis=1表示跨列,作为方法动作的副词

python
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.concatenate((a,b),axis = 0)

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

python
a = np.array((1,2,3))
b = np.array((2,3,4))
np.concatenate((a,b),axis = 0)

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

水平合并

python
a = np.array((1,2,3))
b = np.array((2,3,4))
np.hstack((a,b))

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

python
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.hstack((a,b))

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

垂直合并

python
a = np.array((1,2,3))
b = np.array((2,3,4))
np.vstack((a,b))

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

python
a = np.array([[1],[2],[3]])
b = np.array([[2],[3],[4]])
np.vstack((a,b))

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

分割

python
x = np.arange(9.0)
python
np.split(x,3,axis=0)

[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7., 8.])]

python
x = np.arange(8.0)
python
np.split(x,[3,5,6,10])

[array([0., 1., 2.]), array([3., 4.]), array([5.]), array([6., 7.]), array([], dtype=float64)]

Numpy读取

python
data = np.genfromtxt("test.csv",delimiter=",")
python
data

array([[ nan, nan, nan, nan], [ 1. , 123. , 1.4, 23. ], [ 2. , 110. , nan, 18. ], [ 3. , nan, 2.1, 19. ]])

因为读取后的数据处理较为繁琐,故一般只用Numpy进行计算