import numpy as np
import math
# 创建数组对象 ndarray
a = np.arange(10)print(a)print(type(a))# 对ndarray对象类型进行向量处理 sqrt 开平方print(np.sqrt(a))
b =list(range(10))
c =[]for i in b:
c.append(math.sqrt(i))print(c)
import numpy as np
# 使用array函数创建一维数组1
a = np.array([1,2,3,4])print(a)print(type(a))# 使用array函数创建一维数组2
a1 = np.array([1,2,3,4], dtype=float, ndmin=1)print(a1)print(type(a1))# 使用array函数创建二维数组
b = np.array([[1,2,3],[4,5,6],[7,8,9]])print(b)print(type(b))# 使用array函数创建三维数组
c = np.array([[[1,2,3],[4,5,6],[7,8,9]]])print(c)print(type(c))
import numpy as np
# 使用arange创建数组
a = np.arange(1,11)print(a)
b = np.arange(1,11,2)print(b)print(type(a))
import numpy as np
# 使用random创建一维数组[0.0, 1.0)
a = np.random.random(size=5)print(a)print(type(a))# 使用random创建二维数组[0.0, 1.0)
b = np.random.random(size=(3,4))print(b)print(type(b))# 使用random创建三维数组[0.0, 1.0)
c = np.random.random(size=(2,3,4))# 两个3行4列print(c)# 随机整数生成一维数组
d = np.random.randint(5,10, size=10)print(d)# 二维
e = np.random.randint(3,9, size=(4,3))print(e)# 三维
f = np.random.randint(9, size=(2,4,3))print(d.dtype)print(f)
import numpy as np
# 创建标准的正态分布 期望为0, 方差为1# 一维
a = np.random.randn(4)print(a)print(type(a))# 二维
b = np.random.randn(2,3)print(b)# 三维
c = np.random.randn(2,3,4)print(c)# 创建指定期望值和方差的正态分布
d = np.random.normal(loc=4, scale=3, size=(2,3,4))print(d)
import numpy as np
a = np.array([1,2,3,4,5])
b = np.arange(6,11)
c = np.random.random(5)
d = np.random.randint(100,200, size=5)
e = np.random.randn(10)
f = np.random.normal(loc=3, scale=4, size=5)# print(a)# print(b)# print(c)# print(d)# print(e)# print(f)print("ndim:", a.ndim, b.ndim, c.ndim, d.ndim)# ndim 空间维数print("shape:", a.shape, b.shape)# shape 形状print("dtype", a.dtype, b.dtype, c.dtype)print("size", a.size, b.size, c.size, d.size, e.size, f.size)print("itemsize:", a.itemsize, b.itemsize, c.itemsize)# itemsize 每个元素所占的字节
import numpy as np
# 用0填充
a = np.zeros(5)
b = np.zeros((5,5))# 用1填充
c = np.ones(5)# 创建空
d = np.empty((3,3), dtype=int)# np.linspace(start, stop, num=50, (endpoint=True 包括的), retstep=False, dtype=None)# 该函数用于创建一个一维数组,数组是由一个等差数列构成的
e = np.linspace(1,5,10, endpoint=False)# logspace (start, stop, num=50, endpoint, base, dtype) 创建等比数列
f = np.logspace(0,9,10, base=2)print(a)print(b)print(c)print(e)print(d)print(f)
一维数组切片
import numpy as np
# 一维数组的切片
a = np.arange(10)print(a)
b = a[1::2]print(b)print(a[2])print(a[-2])
二维数组切片
import numpy as np
a = np.arange(1,13)print(a)# 一维数组转换成二维数组
x = a.reshape(4,3)print(x)# 获取二维数组的第二行, 索引是从0开始的print(x[1])# 获取二维数组的第三行第二列print(x[2][1])# 切片的使用[行进行切片,列进行切片] [start:stop:step, start:stop:step]# 获取所有行部分列print(x[:,1])# 获取奇数行所有列print(x[::2,:])# 获取奇数行偶数列print(x[::2,1::2])# 使用坐标获取数组[x, y]元素print(x[2,1])# 同时获取第三行第二列,第四行第一列print(x[2,1], x[3,0])# 获取到的元素组成新的数组print(np.array((x[2,1], x[3,0])))# 负索引的使用print("最后一行")print(x[-1])print("行倒序")print(x[::-1])print("行列倒序")print(x[::-1,::-1])
数组的复制, 深浅拷贝
import numpy as np
a = np.arange(1,37).reshape(6,6)print(a)
b = a[0:2,0:2]print(b)# 切片操作,对b的值进行修改,a的值也改变 是浅拷贝
b[0][0]=100print(b)print(a)# 深拷贝
c = np.copy(a[:2,:2])print(c)
c[0][0]=300print(c)print(b)
维度转换
import numpy as np
# 通过a.reshape变多维
a = np.arange(1,37)
b = a.reshape(6,6)
c = a.reshape(2,3,6)# 通过np.reshape修改为多维
d = np.reshape(a,(2,18))print(a)print(b)print(c)print(d)# 1. 多变一
a1 = c.reshape(36)# 2. 或者
a2 = c.reshape(-1)print("3. 通过函数ravel")
a3 = c.ravel()print("4. 通过flatten")
a4 = c.flatten()print(a1)print(a2)print(a3)print(a4)
数组的拼接
import numpy as np
a = np.arange(1,13)print(a)
a2 = a.reshape(4,3)print(a2)
b = np.arange(113,125)print(b)
b2 = b.reshape(4,3)print(b2)print("水平拼接,用hstack, 注意一定要加[a2, b2], (a2, b2)也行")
c = np.hstack((a2, b2))print(c)print("垂直拼接vstract")
d = np.vstack([a2, b2])print(d)print("使用concatenate拼接,默认exis=0效果和vstract相同")
e = np.concatenate((a2, b2), axis=0)print(e)print("exis=1, 相当于hstract水平拼接")
f = np.concatenate((a2, b2), axis=1)print(f)print("三维数组有三个轴 axis= 0 1 2")
a3 = np.arange(1,13).reshape(2,2,3)
b3 = np.arange(101,113).reshape(2,2,3)print(a3)print(b3.shape)
c3 = np.concatenate((a3, b3), axis=0)print(c3)print(c3.shape)
d3 = np.concatenate((a3, b3), axis=1)print(d3)print(d3.shape)
e3 = np.concatenate((a3, b3), axis=2)print(e3)print(e3.shape)
数组的分割split
import numpy as np
print("一维数组的分割")
a = np.arange(1,17)print(a)print(type(a))
b = np.split(a,4)print(b)print(type(b))print("像下面如果传递数组,就是分为3份")
c = np.split(a,[3,5])print(c)print(type(c))
a2 = a.reshape(4,4)print(a2)
m = np.split(a2,2)print(m,type(m))
n = np.split(a2,2, axis=1)print(n)print(type(n))print("还有hsplit和vsplit")
数组转置
import numpy as np
print("数组的转置")
a = np.arange(1,25).reshape(8,3)print(a)print("transpose函数")
b = a.transpose()print(b)print("对于二维数组,也可以使用a.T转置")
c = a.T
print(c)print("使用np.transpose()转置")
d = np.transpose(a)print(d)print("多维数组进行转置a[i][j][k],默认的将i和K交换 (下标2, 1, 0)")
a3 = np.arange(1,25).reshape(2,3,4)
b3 = np.transpose(a3)print(b3)print(b3.shape)print("多维数组转换")
c3 = np.transpose(a3,(1,2,0))print(c3)print(c3.shape)
数组的+,-,x,/, 三角函数
import numpy as np
print("数组的+, -, x, /")print("加法")
a = np.arange(9).reshape(3,3)
b = np.array([10,10,10])print(a + b)print(np.add(a, b))print("减法")print(b - a)print(np.subtract(a, b))print("乘法, out参数的使用")
y = np.empty((3,3))print(y)
np.multiply(a,10, out=y)print(y)print("除法,同乘法")
np.divide(a,10, out=y)print(y)print("三角函数")
a = np.array([0,30,60,90])print(np.sin(a * np.pi/180))
import numpy as np
a = np.array([0,30,60,90])print(np.sin(a * np.pi/180))print("四舍五入函数, decimals: 舍去的小数位数,默认为0, 如果为负将四舍五入到小数点左侧的位置")
a1 = np.array([1.0,4.45,123,0.657,25.532])
aa = np.around(a1, decimals=1)print(aa)print("numpy.floor(), 返回数字的下舍整数")
a2 = np.floor(a1)print(a2)print("numpy.ceil(), 返回数字的上入整数")
a3 = np.ceil(a1)print(a3)print("统计函数:")print("power", np.power(a,2))
x = np.arange(5)
y = np.zeros(10)
np.power(2, x, out=y[:5])print(y)print("中位数:排完序后 偶数中间两个数的平均值,奇数是中间的数:median()", np.median(x))print("二维数组 要通过axies指定轴, 0表示轴垂直, 1为水平方向, ", np.median(a, axis=0))print("求平均值,mean", np.mean(a))print("sum max min", np.max(a), np.sum(a), np.min(a))print("argmin:最小值的下标,argmax: 最大值的下标", np.argmax(a), np.argmin(a))