培训代码numpy

import numpy as np

# print(np.__version__)
print("使用列表生成一维数组")
data = [1,2,3,4,5,6]
x = np.array(data)
print(x)
print(type(x))
print(x.dtype)
print(x.ndim)

print("使用列表生成二维数组")
data = [[1,2],[3,4],[5,6]]
x = np.array(data)
print(x)
print(x.ndim)
print(x.shape)

print("使用zeros/ones/empty创建数组:通过shape来创建")
x = np.zeros((2,3),dtype=np.int8)
print(x)
print(x.dtype)

x = np.ones((4,5),dtype=np.float32)
print(x)
print(x.dtype)

print("使用arange生成连续的元素")
print(np.arange(6))
print(np.arange(1,6,2))

print("使用astype复制数组,并且转换类型")
x = np.array([1,2,3,4,5],dtype=np.float64)
y = x.astype(dtype=np.int8)
print(x)
print(y)
z = y.astype(np.float64)
print(y)
print(z)

print("将字符串元素转为数值元素")
x = np.array(['1','2','3','4','5'],dtype=np.string_)
y = x.astype(np.int8)#如果元素中有不为数字的元素,则会抛出异常
print(x)
print(y)

print("使用其他的数组的数据类型作为参数")
x = np.array([1,2,3,4],dtype=np.float64)
y = np.arange(3,dtype=np.int32)
print(y)
print(y.astype(x.dtype))

print("ndarray数组与标量/数组的运算")
x = np.array([1,2,3])
print(x*2)
print(x>2)
y = np.array([2,3,4])
print(x*y)
print(x>y)

print("ndarray的基本索引")
x = np.array([[1,2],[3,4],[5,6]])
print(x.shape)
print(x[0])
print(x[0][1])
print(x[0,1])
x = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
print(x[0])
print(x[0][1])
print(x[0,1,0])

y = x[0].copy()#生成一个副本
z = x[0]#未生成副本
print(y)
print(y[0,0])
y[0,0] = 9
z[0,0] = -1
print(y)
print(x)
print(z)

print("ndarray的切片")
x = np.array([1,2,3,4,5])
print(x[1:3])
print(x[:3])
print(x[1:])
print(x[:])
print(x[0:4:2])

x = np.array([[1,2],[3,4],[5,6]])
print(x[:2])
print(x[:2,:1])
print(x[:2][:1])
x[:2,:1] = 0
print(x)
x[:2,:1] = [[9],[8]]
print(x)

x = np.arange(24).reshape((2,3,4))
print(x)
print(x[:2,:1,:1])

print("ndarray布尔型索引")
x = np.array([3,2,3,1,3,0])
y = np.array([True,True,True,False,False,False])
print(x[y])
print(x[y==False])
print(x>=3)
print(x[x>=3])
print(x[(x==2) | (x==1)])
x[(x==2) | (x==1)] = 0
print(x)

print("ndarray的花式索引:使用整型数组作为索引")
x = np.array([1,2,3,4,5,6])
print(x[[0,1,2]])
print(x[[-1,-2,-3]])
x = np.array([[1,2],[3,4],[5,6]])
print(x[[0,1]])
print(x[[[0,1,2],[0,1,0]]])
print(x[[0,1]][:1,[0,1]])

# x = np.arange(24).reshape((4,3,2))
# print(x)
# print(x[[0,1,2,3],[1],[1]])

print("ndarray数组的转置和轴对称")
k = np.arange(12).reshape((3,4))
print(k)
#转置
print(k.T)
#内积(点乘)
print(np.dot(k,k.T))
#高维数组的轴对象
k = np.arange(24).reshape(2,3,4)
print(k)
print(k[1][1][1])
#轴变换
k = k.transpose(2,1,0)
print(k.shape)
#轴变换做转置
x = np.arange(6).reshape(2,3)
print(x)
x = x.transpose(1,0)
print(x)
#轴交换
x = x.swapaxes(1,0)
print(x)

print("numpy的基本统计方法")
x = np.array([[1,2],[3,4],[5,6]])
print(x.shape)
print(x.mean())
print(x.mean(axis=1))
print(x.mean(axis=0))
print(x.sum())
print(x.sum(axis=1))
print(x.max())
print(x.max(axis=1))

print(".sort就地排序")
x = np.array([[6,1,3],[1,5,2]])
x.sort(axis=0)
print(x)

print("ndarray的存取")
x = np.array([[1,6,2],[6,1,3],[1,5,2]])
np.save("file",x)#以二进制.npy保存
y = np.load("file.npy")
print(y)

print("矩阵求逆")
x = np.array([[1,1],[1,2]])
y = np.linalg.inv(x)
print(y)
print(x.dot(y))

print("numpy中的随机数")
x = np.random.randint(0,2,size=10000)#抛硬币
print((x>0).sum())#正面的次数

print('where函数的使用')
cond = np.array([True,False,False,False])
x = np.where(cond,-2,2)
print(x)
cond = np.array([1,2,3,4])
x = np.where(cond>2,-2,2)
print(x)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值