#markdown常用使用
markdown常用使用
1.序列
引用
加粗
-无序列表
#Dtype
import numpy as np
a = np.array([1.1,2.2],dtype=np.float64)
a.astype(int).dtype 不同类型之间转换
#对象
-列表
-元组
-字典
##ndarray 多维数组对象
1.shepe 数组形状
2.dtype:数据类型
3.buffer 对象暴露缓冲区接口
4.offset 数据偏移量
5.strides 步长
6.order:{“C”,“F”} 行或列为主排列顺序
##创建数组
###arange
np.arange(start, stop, step, dtype=None)
np.arange(3) 默认起点0,步长为1 输出:[0 1 2]
np.arange(3,9) 默认步长为1 输出[3 4 5 6 7 8]左闭右开
numpy.ones(shape, dtype=None, order='C')
shape:用于指定数组形状,例如(1, 2)或 3。
dtype:数据类型。
order:{'C','F'},按行或列方式储存数组。
np.ones(3):
array([1., 1., 1.])
np.ones((3,5)):
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
np.zeros(3)
array([0., 0., 0.])
np.zeros((3,5))
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
np.random.rand() #无参数,那么随机返回0-1之间一个小数
0.9842243049200786
np.random.rand(3,4) #带参数,那么随机返回0-1之间矩阵
array([[0.2616987 , 0.34785532, 0.64052877, 0.8283843 ],
[0.6696717 , 0.36372492, 0.53100318, 0.69890008],
[0.31332759, 0.98002585, 0.73734262, 0.39738664]])
numpy.eye(N, M=None, k=0, dtype=<type 'float'>)
N:输出数组的行数。
M:输出数组的列数。
k:对角线索引:0(默认)是指主对角线,正值是指上对角线,负值是指下对角线。
###linspace
np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
1.start:序列的起始值
2.stop:序列的结束值。
3.num:生成的样本数。默认值为50。
4.endpoint:布尔值,如果为真,则最后一个样本包含在序列内。
5.retstep:布尔值,如果为真,返回间距。
6.dtype:数组的类型。
np.linspace(0,10,10,endpoint=True)
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
np.linspace(0,10,10,False)
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
###从已知数据创建
frombuffer(buffer):将缓冲区转换为 1 维数组。
fromfile(file,dtype,count,sep):从文本或二进制文件中构建多维数组。
fromfunction(function,shape):通过函数返回值来创建多维数组。
fromiter(iterable,dtype,count):从可迭代对象创建 1 维数组。
fromstring(string,dtype,count,sep):从字符串中创建 1 维数组。
np.fromfunction(lambda a,b:a+b,(5,2))
array([[0., 1.],
[1., 2.],
[2., 3.],
[3., 4.],
[4., 5.]])
##数组属性
1.转置
a.T或者a.transpose()都可以
2.数据类型
a.dtype
3.输出虚部
a.imag
3.输出虚部
a.imag
4.输出实部
a.real
5.元素总数
a.size
6.数组元素的字节数
a.itemsize
7.数组总字节数
a.nbytes
8.数组维度
a.ndim
9.数组形状
a.shape
10.用来遍历数组时,输出每个维度中步进的字节数组
a.strides
##数组操作
1.reshape: numpy.reshape(a, newshape)
np.arrange(10).reshape((5,2))
array([[0, 1],
[2, 3],
[4, 5],