# encoding:utf-8
import numpy as np
#定义一个二维矩阵
array = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
print(array)
print(array.ndim)#矩阵维度
print(array.shape)#矩阵形状
print(array.size)#矩阵元素个数
print(array.dtype)#矩阵元素数据类型
# encoding:utf-8
import numpy as np
# 创建array
a = np.array([1,2,3],dtype=np.float)
print(a.dtype)
print("##########################")
# 创建全0矩阵
zero = np.zeros((4,3))
print(zero)
print("##########################")
# 创建全1矩阵
one = np.ones((3,4))
print(one)
print("##########################")
# 创建随机矩阵
empty = np.empty((3,2))
print(empty)
print("##########################")
# 创建指定范围的矩阵(向量)
e = np.arange(12)#0到11
f = np.arange(4,9)#4到8
g = np.arange(1,24,3)#1到23,间隔为3
print(e)
print(f)
print(g)
print("##########################")
# reshape(),改为指定形式的矩阵
h = np.arange(12).reshape((4,3))
print(h)
# encoding:utf-8
import numpy as np
# 创建array
a = np.array([[1,2,3],
[2,3,4]])
b = np.array([[2,1,3],
[4,3,1]])
# + - * / ** % //等运算符均是对应元素间运算
print(a + b)
print(a * b)
print(a + 2)
print(a * 2)
print("#################")
# > <等作用于矩阵
c = a > 2
print(c)
print("#################")
# 矩阵乘法(.T代表转置)
print(np.dot(a,b.T))
print(a.dot(b.T))
print("#################")
# 矩阵的转置
print(a.T)
print(np.transpose(a))
# encoding:utf-8
import numpy as np
# 生成0~1之间的随机数
ran1 = np.random.random((2,3))
print(ran1)
print("####################")
# 生成标准正态分布的随机数
ran2 = np.random.normal(size=(2,3))
print(ran2)
print("####################")
# 生成指定范围内的随机整数
ran3 = np.random.randint(0,10,size=(2,3))
print(ran3)
# encoding:utf-8
import numpy as np
ran = np.random.randint(0,10,size=(3,3))
print(ran)
print(np.sum(ran))#求和
print(np.sum(ran,axis=0))#对列求和
print(np.sum(ran,axis=1))#对行求和
print(np.mean(ran))#求平均值
print(ran.mean())#求平均值
print(np.max(ran))#求最大元素
print(np.min(ran))#求最小元素
print(np.argmax(ran))#求最大元素索引
print(np.argmin(ran))#求最小元素索引
print(np.median(ran))#求中位数
print(np.sqrt(ran))#开方
print(np.sort(ran))#按行排序
print(np.clip(ran,2,7))#控制上下界
:用来表示区间,左闭右开
,用来区分行列,左行又列
-表示从后往前数,比如-1是最后一个,-2是倒数第二个
下面是一些例子:
# encoding:utf-8
import numpy as np
ran = np.random.randint(0,10,size=(5,4))
print(ran)
print("#########")
# 取第三列
print(ran[:,2])
print("#########")
# 取倒数第二行
print(ran[-2])
print("#########")
# 取倒数两列
print(ran[:,-2:])
print("#########")
# 取第二行到倒数第二行
print(ran[1:-1])
# encoding:utf-8
import numpy as np
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
# 垂直合并
print(np.vstack((a,b)))
# 水平合并
print(np.hstack((a,b)))
print("############")
# 另一种合并方式axis为0表示垂直合并,为1表示水平合并
print(np.concatenate(([a],[b]),axis=0))
print(np.concatenate(([a],[b]),axis=1))
print("############")
# 升维操作
c = a[np.newaxis,:]
print(a.shape)
print(c.shape)
# 升至至少二维或三维
d = np.atleast_2d(a)
e = np.atleast_3d(a)
print(d.shape)
print(e.shape)
# encoding:utf-8
import numpy as np
a = np.arange(12).reshape((3,4))
print(a)
# 垂直方向的分割
b,c,d = np.split(a,3,axis=0)
print(b)
print(c)
print(d)
e,f,g = np.vsplit(a,3)
print(e)
print(f)
print(g)
print("##############")
# 水平方向的分割
b,c = np.split(a,2,axis=1)
print(b)
print(c)
print(d)
e,f = np.hsplit(a,2)
print(e)
print(f)
print(g)
print("##############")
# 不均等分割
b,c,d = np.array_split(a,3,axis=1)
print(b)
print(c)
print(d)
# encoding:utf-8
import numpy as np
a = np.arange(4)
print(a)
# 浅拷贝(引用类型,共享内存)
b = a
b[0] = 1
print(a)
print(b)
# 深拷贝(值类型,复制内存)
c = a.copy()
c[0] = 2
print(a)
print(c)