1,创建ndarray数组
1.1 ndarray 支持的数据类型
1.2 快速生成ndarray
1.2.1 ones所有成员都为1的数组
1.2.2 zreos所有成员都为0的数组
1.2.3 random 随机数组
1.2.4 arange 顺序数组
1.2.5 linspace 均匀分布的数组
1.3 改变ndarray的形状
2 ndarray 的算术运算
3 简单绘图
4 数组的访问和切片
5 数组的合并和切片
6 unique 去重
numpy提供了跑一趟鸿对多维数组对象的支持:ndarray,具有矢量运算能力,快速、节省空间。
numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
1,创建ndarray数组
import numpy as np
x = np.array([1,2,3,4])
1.1 ndarray 支持的数据类型
使用ndim属性访问一个ndarray的维度的个数。使用shape属性访问每个维度的长度
print(x.nidm) #访问每个维度的个数
print(x.shape) #访问每个维度的长度
print(x.shape[0]) #获取行
print(x.len(x)) #长度
print(x.nbytes) #占取多少字节
1.2.1 ones所有成员都为1的数组
import numpy as np
np.ones(5)
1.2.2 zreos所有成员都为0的数组
import numpy as np
np.zeros(3)
np.zreos((4,2))
1.2.3 random 随机数组
np.random.random((3,4))
这里插入一些random的一些只是点
random.choice([1,5,9,8]) //随机选择一个
random.shuffle(l) //打乱顺序
random.randint() //产生随机整数
random.random () //产生随机浮点数(0-1)
random.uniform()//产生1-10的随机浮点数
1.2.4 arange 顺序数组
arange函数和Python自带的range函数是基本相同的,只是arrange是返回一个ndarray
np.arange(3)
1.2.5 linspace 均匀分布的数组
np.linspace(1,3,9)
1.3 改变ndarray的形状
x = np.arange(15).reshape(3,5)
2 ndarray 的算术运算
shape相同,逐个运算
当这两个ndarray的shape相同的时候,即x.shape==y.shape 就会逐个元素进行计算
x.sum(axis=1)#表示求一行的总额
x.sum(axis=0)#表示求每一列的
#其余的公式以此类推
3简单绘图
x=np.linspace(0,10)
y=np.sin(x)
plt.title("y=sin(x)") #注释
import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(x,y)
plt.show()
4数组的访问和切片
访问
b = np.arange(1,7).reshape(3,2)
b[0][1]
b[0,1]
切片
import numpy as np
x=np.arange(9).reshape(3,3)
x[0:2]
x[0:2,1:3]
5数组的合并和切片
a=np.arange(4).reshape(2,2)
b=np.arange(5,9).reshape(2,2)
pp=np.stack((a,b))
np.hstack((a,b)) #横着合并 水平
np.vstack((a,b)) #竖着合并 垂直
数组的筛选
x = np.random.rand(7,4)
x[x>0] #单个条件
x[(x>0.8) & (x<0.9)] #两个条件注意!
6 unique去重函数
u,c=np.unique(x, return_counts=True) #有 去重效果 return_counters 返回一个