# 数组形状:T/.reshope()/.resize()
import numpy as np
ar1 = np.arange(5)
ar2 = np.ones((5,2))
print(ar1,'\n',ar1.T)
print(ar2,'\n',ar2.T)
# .T方法:转置,例如原shape为(3,4)/(2,3,4),转置结果为(4,3)/(4,3,2) -> 所以以为数据转置后结果不变
ar3 = ar1.reshape(1,5) # 用法:直接将已有数据改变形状
ar4 = np.zeros((4,6)).reshape(3,8) # 用法2:生成数组后直接改变形状
ar5 = np.reshape(np.arange(12),(3,4)) # 用法:参照内添加数组,目标形状
print(ar1,'\n',ar3)
print(ar4)
print(ar5)
print('----------')
# numpy.reshape(a, newshape, order='C'):为数组提供新形状,而不更改其数据,所以元素数量需要一致
ar6 = np.resize(np.arange(5),(3,4))
print(ar6)
# numpy.resize(a, new_shape):返回具有指定形状的新数据,如有必要可重复填充所需数量的元素
# 注意了:T/.reshape()/.resize() 都是生成新的数据
# 数组的复制
import numpy as np
ar1 = np.arange(10)
ar2 = ar1
print(ar2 is ar1)
ar1[2] = 9
print(ar1,ar2)
# 回忆python的赋值逻辑,指向内存中生成的一个值 -> 这里ar1和ar2指向同一个值,所以ar1改变,ar2一起改变
ar3 = ar1.copy()
print(ar3 is ar1)
ar1[3] = 1
print(ar1,ar3)
# copy方法生成数组及其数据的完整拷贝
# .T/.reshape()/.reszie()都是生成新的数组
# 数组类型转换 .astype()
import numpy as np
ar1 = np.arange(10,dtype=float)
print(ar1,ar1.dtype)
print('----------')
# 可以再参数位置设置数组类型
ar2 = ar1.astype(np.int32)
print(ar2,ar2.dtype)
# 数组堆叠
import numpy as np
# a = np.arange(5) # a为一维数组,5个元素
# b = np.arange(5,9) # b 为一维数组,4个元素
# ar1 = np.hstack((a,b)) # ((a,b)): 这里的形状可以不一样
# print(a,a.shape)
# print(b,b.shape)
# print(ar1,ar1.shape)
# a = np.array([[1],[2],[3]]) # a 为二维数组,三行一列
# b = np.array([['a'],['b'],['c']]) # b 为二维数组,三行一列
# ar2 = np.hstack((a,b)) # ((a,b)) 这里形状必须要一致
# print(a,a.shape)
# print(b,b.shape)
# print(ar2,ar2.shape)
# print('---------')
# numpy.hstack(tup): 水平(按列顺序)堆叠数组
# a = np.arange(5)
# b = np.arange(5,10)
# ar1 = np.vstack((a,b))
# print(a,a.shape)
# print(b,b.shape)
# print(ar1,ar1.shape)
# a = np.array([[1],[2],[3]])
# b = np.array([['a'],['b'],['c']])
# ar2 = np.vstack((a,b))
# print(a,a.shape)
# print(b,b.shape)
# print(ar2,ar2.shape)
# print('---------')
# numpy.vstack(tup): 垂直(按列顺序)堆叠数组
a = np.arange(5)
b = np.arange(5,10)
ar1 = np.stack((a,b))
ar2 = np.stack((a,b),axis = 1)
print(a,a.shape)
print(b,b.shape)
print(ar1,ar1.shape)
print(ar2,ar2.shape)
# numpy.stack(arrays,sxis=0): 沿河新轴链接数组的序列,形状必须一样
# axis 参数的意思:::假设两个数组[1 2 3 ] 和[4 5 6 ] ,shape均为(3,0)
# axis=0: [[1 2 3] [4 5 6]],shape为(2,3)
# axis=1: [[1 4] [2 5] [3 6]],shape为(3,2)
# 数据拆分
import numpy as np
ar1 = np.arange(16).reshape(4,4)
ar2 = np.hsplit(ar1,2)
print(ar1)
print(ar2,type(ar2))
# numpy.heplit(ary,indices_or_sections):将数组水平(逐列)拆分为多个子数组 -> 按列拆分
# 输出结果为列表,列表中元素为数组
ar3 = np.vsplit(ar1,4)
print(ar3,type(ar3))
# numpy.vsplit(ary,indices_or_sections): 将数组垂直(行方向)拆分为多个子数组 -> 按行拆
# 数组的简单运算
import numpy as np
ar = np.arange(6).reshape(2,3)
print(ar)
print(ar + 10) # 加法
print(ar * 2) # 乘法
print(ar - 10) # 减法
print(1 / (ar+1)) # 除法
# 与标量的运算
print(ar.mean()) # 求平均值
print(ar.max()) # 求最大值
print(ar.min()) # 求最小值
print(ar.std()) # 求标准差
print(ar.var()) # 求方差
print(ar.sum(),np.sum(ar,axis = 0)) # 求和,np.sum() -> axis为0 按列求和 ; axis为1 按行求和
print(np.sort(np.array([1,4,3,2,5,6]))) # 排序
# 作业1:创建一个20个元素的数组,分别改变成两个形状:(4,5),(5,6)(超出范围用resize)
'''
[[0 1 2 3 4 ]
[5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
-----------
[[0 1 2 3 4 5]
[6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 0 1 2 3]
[4 5 6 7 8 9 ]]
'''
import numpy as np
ar1 = np.reshape(np.arange(20),(4,5))
ar2 = np.resize(np.arange(20),(5,6))
print(ar1)
print(ar2)
# 作业2:创建一个(4,4)的数组,把其元素类型改成字符型
'''
[['0' '1' '2' '3']
['4' '5' '6' '7']
['8' '9' '10' '11']
['12' '13' '14' '15']]
'''
import numpy as np
ar1 = np.reshape(np.arange(16),(4,4))
ar2 = ar1.astype(np.str)
print(ar2)
# 作业3:根据要求创建数组,运用数组的运算方法得到结果:result = ar *10+100,并求出result的均值及求和
# 创建数组为:
'''
[[0 1 2 3]
[4 5 6 7]
[8 9 10 11]
[12 13 14 15]]
----------
计算后的数组为:
[[100 110 120 130]
[140 150 160 170]
[180 190 200 210]
[220 230 240 250]]
------------
result的均值为:
175.0
---------
result求和为:
2800
-----------
'''
import numpy as np
ar1 = np.reshape(np.arange(16),(4,4))
ar2 = (ar1 * 10) + 100
print(ar1)
print(ar2)
print(ar2.mean())
print(ar2.sum())