numpy 的基本使用1
最近开始认真学习机器学习啦,之前只是大概了解了一下,记录一下自己的学习过程,后面也方便随时复习一下。
NumPy是一个由多维数组对象和用于处理数组的例程集合组成的库。可以执行以下操作:数组的算数和逻辑运算;傅立叶变换和用于图形操作的例程;与线性代数有关的操作,NumPy拥有线性代数和随机数生成的内置函数。NumPy 通常与 SciPy(Scientific Python)和 Matplotlib(绘图库)一起使用。
官网:http://www.numpy.org
安装Numpy:
pip install numpy 或 python -m pip install numpy
#学会文档查询
np.random.normal?
help(np.random.normal) #两种方式均可
#引入numpy库:
import numpy
#查看numpy版本:
numpy.__version__
'1.19.5'
#重命名库,方便使用:
import numpy as np
#通过list创建数组(数组元素类型一致):
nparray = np.array([i for i in range(10)])
nparray
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
nparray.dtype
dtype('int32')
#创建array方法:
nparray = np.array([i for i in range(10)])
nparray
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array = np.zeros(10)
array
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
#类型查看:
array.dtype
dtype('float64')
#指定类型:
np.zeros(10,dtype = int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
ar = np.zeros(3)
ar
array([0., 0., 0.])
ar.dtype
dtype('float64')
#创建二维数组:
np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
#全1数组或二维数组:
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.ones((2,3))
array([[1., 1., 1.],
[1., 1., 1.]])
#指定值:
np.full(shape=(2,3),fill_value=6)
array([[6,6,6],
[6,6,6]])
#arange
#python中创建range(起始值默认0,截止,步长默认1) 步长不能为浮点数:
[i for i in range(0,20,2)]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
#Numpy创建 步长可以为浮点数:
np.arange(0,20,2.2)
array([ 0. , 2.2, 4.4, 6.6, 8.8, 11. , 13.2, 15.4, 17.6, 19.8])
#linspace
#包括起始点 等间距截取个数:
np.linspace(0,20,10)
array([ 0. , 2.22222222, 4.44444444, 6.66666667, 8.88888889,
11.11111111, 13.33333333, 15.55555556, 17.77777778, 20. ])
#random
#生成0到10(不包括10)之间的随机数
np.random.randint(0,10)
9
#指定随机数个数,建议个数前加上名称size=以便区分
np.random.randint(0,10,10)
array([0, 3, 6, 1, 0, 9, 0, 7, 1, 4])
np.random.randint(0,10,size=(2,3))
array([[3, 0, 5],
[6, 5, 3]])
#指定随机种子:
np.random.seed(666)
np.random.randint(0,10,10)
array([2, 6, 9, 4, 3, 1, 0, 8, 7, 5])
#创建浮点随机数:
np.random.random()
0.19289200304058374
#指定个数,size=可不写,建议加上以便区分:
np.random.random(size = 10)
array([0.70084475, 0.29322811, 0.77447945, 0.00510884, 0.11285765,
0.11095367, 0.24766823, 0.0232363 , 0.72732115, 0.34003494])
np.random.random((2,3))
array([[0.19750316, 0.90917959, 0.97834699],
[0.53280254, 0.25913185, 0.58381262]])
#创建符合正态分布随机浮点数,均值为0 方差为1
np.random.normal()
0.7294645190059565
指定均值为10和方差100 第三个参数为大小
np.random.normal(10,100,(2,3))
array([[ 124.13367716, 55.24366191, -213.49687669],
[ 137.04776598, -62.92882099, 145.9252547 ]])
#属性相关:
x = np.arange(10)
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.arange(6).reshape((3,2))
y
array([[0, 1],
[2, 3],
[4, 5]])
#查看维度:
x.ndim
1
y.ndim
2
#一行10个元素:
x.shape
(10,)
#表示3行2列:
y.shape
(3, 2)
#元素个数的统计:
x.size
10
y.size
6
#array数据访问:
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[0]
0
x[-1]
9
y
array([[0, 1],
[2, 3],
[4, 5]])
y[0][0]
0
y[1,1]
3
#切片:
x[0:5]
array([0, 1, 2, 3, 4])
x[5:]
array([5, 6, 7, 8, 9])
#取列:
y[:,0]
array([0, 2, 4])
y[:,0].ndim
1
#注意:修改子矩阵中元素会改变原矩阵(区别于python)
y
array([[0, 1],
[2, 3],
[4, 5]])
subY = y[:1,:1]
subY
array([[0]])
subY = y[:1,:2]
subY
array([[0, 1]])
subY[0,0]=1
subY
array([[1, 1]])
y
array([[1, 1],
[2, 3],
[4, 5]])
#创建与原矩阵不相关的矩阵:
subY = y[:1,:2].copy()
subY
array([[1, 1]])
subY[0,0]=2
y
array([[1, 1],
[2, 3],
[4, 5]])
#reshape
x
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
A = x.reshape(2,5)
A
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
B = x.reshape(1,10)
B
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
#注意:B为二维数组 维度为2:
B.ndim
2
A.ndim
2
B.shape
(1, 10)
#改变为10行,每行有多少让其自动生成(传入-1)
x.reshape(10,-1)
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
x.reshape(2,-1)
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
#错误:10不能被3整除
x.reshape(3,-1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: cannot reshape array of size 10 into shape (3,newaxis)
跟着大佬梳理的流程走下来的,在这里注明一下出处:
https://github.com/Exrick/Machine-Learning
注:大佬的更直观详细
这篇博客介绍了NumPy库的基础使用,包括数组创建、算术操作、随机数生成、线性代数操作等。通过实例展示了如何使用`arange`、`linspace`、`random`等函数,并讲解了数组访问和重塑的方法。适合机器学习初学者进行学习和复习。
188





