以博客的方式记录当前学习,致每天最好的自己。
#多维数组
import numpy as np
#数组定义方式
a0 = np.array([1,2,3,4])#列表形式定义
a1 = np.array((1,2,3,4))#元组形式定义
a2 = np.array([[1,2],[3,4]])#二维数组
print(a0)
#创建数组
a =np.arange(0,1,0.1)#随机数组
a1 = np.linspace(0,10,10)#默认加入终值#endpoint=False 不加终值
print(a1)
#随机数组
a = np.random.rand(2,2)#固定shape随机数组,0~1之间随机分布
a1 = np.random.randn(2,2)#固定shape随机数组,标准正态分布随机数
a2 = np.random.randint(0,9,[2,5])#固定shape随机数组,区间内随机整数
a3 = np.random.uniform(0,4,[2,2])#均匀分布随机数
#随机排序列表
a5 = [1,2,3,4,5]
np.random.shuffle(a5)
#生成特定形式数组
a = np.zeros([2,3])
a1 = np.ones([2,2])
a2 = np.zeros_like(a)
a3 = np.ones_like(a2)
#print(a3)
#获取数组信息
a = np.zeros([2,2])
print(a.shape)
print(a.dtype)
print(a.ndim)
#存取数组,一维
a = np.array([4,2,3,1,5,6,7])
print(a[1])#索引
print(a[-2:])#切片
print(a[: :-1])#负切片
#存取数组,二维
a = np.arange(25).reshape(5,5)
print(a)
#print(a[-1])#取某一行
print(a[:,-1])#取某一列
#print(a[1,1])#取特定值
print(a[2:4,2:4])#取特定块
#三维数组
a = np.ones(45).reshape(3,5,3)
a[1,2,1] = 8
print(a)
#数组维度变换
a = np.arange(0,10,1)
b = a.reshape(-1,2)#-1:表示自动生成,满足所有元素除以列数
print(a)
print(b)
#数组维度交换
a = np.arange(10).reshape(2,5)
b = a.swapaxes(0,1)24
a = np.arange(12).reshape(2,3,2)
b = a.swapaxes(0,1)#0,1维度互换
print(a)
print(b)
“”"
[[[ 0 1]#(0,0)
[ 2 3]#(0,1)
[ 4 5]]#(0,2)
[[ 6 7]
[ 8 9]
[10 11]]]
[[[ 0 1]
[ 6 7]]
[[ 2 3]#(1.0)
[ 8 9]]
[[ 4 5]#(2,0)
[10 11]]]
“”"
#数组降维,三种方式
a = np.arange(10).reshape(2,5)
b = np.ndarray.flatten(a)#
c = a.reshape(-1)#
d = a.ravel()#
print(a)
print(b)
print©
print(d)
#堆叠数组,水平和垂直
a = np.array([1,2,3,4])
b = np.array([5,6,7,8])
c = np.hstack((a,b))#水平堆叠
d = np.vstack((a,b))#垂直堆叠
print©
print(d)
#数据可视化matplotlib
import matplotlib.pyplot as plt
#曲线图
x = np.linspace(0,10,100)
#print(x.shape)
y = np.sin(x)
z = np.cos(x)
plt.figure(figsize=(8,4))#调出画布
plt.plot(x,y,label=" s i n sin sin",color=‘red’,linewidth=2)
plt.plot(x,z,“b–”,label=’ c o s cos cos’)
plt.xlabel(‘Time(s)’)
plt.ylabel(‘v’)
plt.title(“matplotlib function”)
plt.axis([0,6,0,1])#限制坐标轴范围
#分别限制x,y范围
# plt.xlim()
# plt.ylim()
plt.grid(True)#方格
plt.legend(loc=1)#显示图例,对应plot中的label
plt.show()
#直方图
a = np.random.randn(10000)
#a:绘画数据,bins长条数目,normed是否归一化,默认0,不归一化;
# facecolor:长条颜色,edgecolor:边框颜色,alpha透明度。
plt.hist(a,bins=40,normed=1,facecolor=“blue”,edgecolor=“black”,alpha=0.7)
plt.show()
#读图
1274

被折叠的 条评论
为什么被折叠?



