Numpy 库的学习记录

目录

1、array

what is array

narray

2、create a basic array

水平和垂直操作

3、Adding, removing, and sorting elements

排序

 concatenate 连接

4、shape of array

get the shape of array

reshape

 5、convert array(比如 一维 到 二维)

6、index of array

index

对序列里的元素进行限定

返回 boolean values 

7、Basic array operations

基本运算

求和 求最值 

reverse an array 取反

 多维序列 扁平化 

8、matrices 矩阵

创建矩阵

矩阵相加

broadcast  - 扩大

随机数矩阵

Transposing(转置) and reshaping a matrix

 9、unique items and count

一维

 多维

 10、Working with mathematical formulas

11、 和pandas 和 Matplotlib 

12、随机数 random 相关

np 的随机数 seed

random.normal ,高斯分布的随机数

 random.uniform  均匀分布


1、array

what is array

An array is a central data structure of the NumPy library.

An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element.(比如学pytorch)

import numpy as np
import torch



a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(a)
print(a.dtype)
print(a.shape)

''''
In NumPy, dimensions are called axes.
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]
 
这个矩阵可以说,3行四列
也可以说
The first axis has a length of 3 and the second axis has a length of 4.
'''
print(a[0])

narray

N-dimensional array

The NumPy ndarray class is used to represent both matrices(矩阵) and vectors(向量).

vector: an array with a single dimension

matrix: an array with two dimensions

tensor(张量): 3-D or higher dimensional array

2、create a basic array


import numpy as np
import torch


a = np.zeros(2)
print(a)

a = np.ones(2)
print(a)


a = np.arange(4) #an array with a range of elements
print(a)
b = np.arange(1,8,3) #start end step
print(b)


a = np.linspace(0, 10, num=5) #分5分 线性增加
print(a)

a = np.ones(2, dtype=np.int64) #specify the type
print(a)

水平和垂直操作

水平和垂直合并

import numpy as np

a1 = np.array([[1, 1],
            [2, 2]])

a2= np.array([[3, 3],
            [4, 4]])

a_h = np.hstack((a1,a2)) #水平
print(a_h)
a_v = np.vstack((a1,a2)) #vertical 垂直
print(a_v)

水平和垂直分裂

import numpy as np

a = np.arange(1,25).reshape(2,12)
print(a)
print(a.shape)
'''
返回一个列表
返回3个array 
每个array 的shape (2*4) 4 = 12/3, 原先12列
'''
a_hsp = np.hsplit(a, 3)
print(a_hsp)
print(a_hsp[0].shape)

3、Adding, removing, and sorting elements

排序

import numpy as np
import torch


arr = np.array([2, 1, 5, 3, 7, 4, 6, 8])
print(arr)

arr_sort = np.sort(arr)
print(arr_sort)
print(arr)

可以看出,排序不会改变原来序列的元素

比如

import numpy as np
import torch


'''
返回排好序的序列的元素的索引
'''

x = np.array([3, 1, 2])
index = np.argsort(x)
print(index)  #3-0,1-1,2-2,   返回的索引1,2,0

 concatenate 连接

import numpy as np
import torch


a = np.array([1, 2, 3, 4])
b = np.array([5, 6, 7, 8])

c = np.concatenate((a, b))
print(c)

x = np.array([[1, 2], [3, 4]])
print(x) # 2*2
y = np.array([[5, 6]])
z = np.concatenate((x, y), axis=0) #列数相同 连接
print(z)

x = np.array([[1, 2, 3]])
print(x) # 2*2
y = np.array([[5, 6]])
z = np.concatenate((x, y), axis=1) #行数相同 连接
print(z)

4、shape of array

get the shape of array

'''
三维的 array  3x2x4
'''
array_example = np.array([[[0, 1, 2, 3],
                           [4, 5, 6, 7]],

                          [[0, 1, 2, 3],
                            [4, 5, 6, 7]],

                         [[0 ,1 ,2, 3],
                          [4, 5, 6, 7]]])



print(array_example)
print(array_example.ndim) #打印维度
print(array_example.shape) 
print(array_example.size) #打印元素的总个数

reshape

import numpy as np


'''
三维的 array  3x2x4
'''
array_example = np.array([[[0, 1, 2, 3],
                           [4, 5, 6, 7]],

                          [[0, 1, 2, 3],
                            [4, 5, 6, 7]],

                         [[0 ,1 ,2, 3],
                          [4, 5, 6, 7]]])


print(array_example)
array_example1 = np.reshape(array_example,newshape=(3,4,2),order='C')
print(array_example1.shape)
print(array_example1)

 5、convert array(比如 一维 到 二维)

import numpy as np


a = np.array([1,2,3,4,5,6])
print(a)
print(a.shape)
a2 = a[np.newaxis, :] #np.newaxis will increase the dimensions of your array
print(a2)
print(a2.shape)

a3 = np.expand_dims(a, axis=1) # expand dim 扩展维度
print(a3)
print(a3.shape)

6、index of array

index

import numpy as np


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

print(data[1])
print(data[1:])
print(data[-2:])

对序列里的元素进行限定

import numpy as np

a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
#建立一个条件判别式
five_blow = (a <= 5)
print(a[five_blow])

divisible_by_2 = (a % 2 == 0)
print(a[divisible_by_2])

c = a[(a > 2) & (a < 11)]
print(c)

返回 boolean values 

import numpy as np

a = np.array([[1 , 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
five_up = (a > 5) | (a == 5)
print(five_up)

7、Basic array operations

基本运算

addition, subtraction, multiplication, division, and more

import numpy as np

a = np.array([1,2])
b = np.ones(2,dtype=int)

print(a)
print(b)

c = a + b
print(c)
c1 = a - b
print(c1)
c2 = a * b
print(c2)
c3 = a // a
print(c3)

求和 求最值 

import numpy as np

a = np.array([1,2])
print(a.max())
print(a.min())
print(a.sum())

reverse an array 取反

一维

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
reversed_arr = np.flip(arr)
print(reversed_arr)

多维

import numpy as np

arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
arr_2d_re = np.flip(arr_2d,axis=0) # 按 行 取反
print(arr_2d_re)

arr_2d_re1 = np.flip(arr_2d,axis=1) # 按 行 取反
print(arr_2d_re1)

arr_2d_re2 = np.flip(arr_2d) 
print(arr_2d_re2)

 

 

 

指定行列取反

import numpy as np

arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
arr_2d[1] = np.flip(arr_2d[1]) # 第二行 取反
print(arr_2d)

arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
arr_2d[:,1] = np.flip(arr_2d[:,1]) # 第二列 取反
print(arr_2d)

 

 

 多维序列 扁平化 

Reshaping and flattening multidimensional arrays

import numpy as np

arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a_flat = arr_2d.flatten()
print(a_flat)

import numpy as np

arr_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
a_flat = arr_2d.flatten()
print(a_flat)
'''
对原先的array 不产生影响
'''
a_flat[0] = 99
print(arr_2d)
print(a_flat)

'''
 use ravel, the changes you make to the new array
 will affect the parent array.
 
 扁平化了 且与原始的array 有直接影响
'''
a_flat2 = arr_2d.ravel()
print(a_flat2)
a_flat2[0] = 99
print(a_flat2)
print(arr_2d)

8、matrices 矩阵

创建矩阵

import numpy as np

matrix = np.array([[1, 2], [3, 4], [5, 6]])
print(matrix)


print(matrix[0,1])
print(matrix[1:2])
print(matrix[1:2,1])

 

import numpy as np

matrix = np.array([[1, 2], [3, 4], [5, 6]])
print(matrix)

print(matrix.max())
print(matrix.min())
print(matrix.sum())

print(matrix.max(axis=0)) #每列的最大值
print(matrix.max(axis=1)) #每行的最大值

 

矩阵相加

import numpy as np

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

sumArray = data + ones
print(sumArray)

broadcast  - 扩大

use its broadcast rules for the operation.

 会自动补充

import numpy as np

data = np.array([[1, 2], [3, 4], [5, 6]])
ones_row = np.array([[1, 1]])

sumArray = data + ones_row
print(sumArray)

 

随机数矩阵

import numpy as np

rng = np.random.default_rng(0)
n = rng.random((2,3))
print(n)

创建一个大于1,小于5 的 2*4 的矩阵

import numpy as np

rng = np.random.default_rng(1)
n = rng.integers(5,size=(2,4))
print(n)

Transposing(转置) and reshaping a matrix

import numpy as np

arr = np.array([[1,2,3],[4,5,6]])
print(arr)

arr1 = arr.reshape(3,2)
print(arr1)

#矩阵的转置,两种方法
arr2 = arr.transpose()
print(arr2)
arr3 = arr.T
print(arr3)

 9、unique items and count

一维

返回不重复的元素 及其 index

import numpy as np

a = np.array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18, 19, 20])
unique_values, indices_list = np.unique(a, return_index=True)
print(unique_values)
print(indices_list)

返回不重复的元素 及其 元素重复的个数

import numpy as np

a = np.array([11, 11, 12, 13, 14, 15, 16, 17, 12, 13, 11, 14, 18, 19, 20])
unique_values, occurrence_count = np.unique(a, return_counts=True)
print(unique_values)
print(occurrence_count)

 

 多维

import numpy as np

a_2d = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [1, 2, 3, 4]])
print(a_2d.shape)

unique_rows, indices, occurrence_count \
    = np.unique( a_2d, axis=0, return_counts=True, return_index=True)
print(unique_rows) # 返回独特的行向量
print(indices) # 行 对应的 坐标
print(occurrence_count) #行 出现的 次数

 10、Working with mathematical formulas

 

 

11、 和pandas 和 Matplotlib 

12、随机数 random 相关

np 的随机数 seed

seed()中的参数被设置了之后,np.random.seed()可以按顺序产生一组固定的数组,如果使用相同的seed()值,则每次生成的随机数都相同。如果不设置这个值,那么每次生成的随机数不同。但是,只在调用的时候seed()一下并不能使生成的随机数相同,需要每次调用都seed()一下,表示种子相同,从而生成的随机数相同。

eg1、两次 都是 seed(1)

import random
import matplotlib.pyplot as plt
import numpy as np



np.random.seed(1)
L1 = np.random.randn(3, 3)
np.random.seed(1)
L2 = np.random.randn(3, 3)
print(L1)
print(L2)

eg2 一次seed(1),一次seed(2)

import random
import matplotlib.pyplot as plt
import numpy as np



np.random.seed(1)
L1 = np.random.randn(3, 3)
np.random.seed(2)
L2 = np.random.randn(3, 3)
print(L1)
print(L2)

 

结论,seed(参数),系统给你分配某堆种子,我们就从这一堆种子找到的随机数

random.normal ,高斯分布的随机数

Draw random samples from a normal (Gaussian) distribution

import random
import matplotlib.pyplot as plt
import numpy as np

# 数据准备
np.random.seed(10)

mu, sigma = 1, 2 # mean and standard deviation(标准差)
s = np.random.normal(mu, sigma, 1000)


# 计算上面这组随机数 的 均值 和 方差
s_mean = abs(mu - np.mean(s))
print('s的均值 与 设定值 之差:  ',s_mean)
s_variance = abs(sigma - np.std(s, ddof=1))
print('s的方差 与 设定值 之差:  ',s_variance)
#差距很小,近似了

 random.uniform  均匀分布

import matplotlib.pyplot as plt
import numpy as np



# make the data
np.random.seed(3)

s = np.random.uniform(-1,0,1000)


count, bins, ignored = plt.hist(s, 16, density=True)
plt.plot(bins, np.ones_like(bins), linewidth=2, color='r')

plt.show()

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值