1.numpy数组对象ndarray
2.数组的创建
import numpy as np
a=np.array([1,2,3])
print(a.size) #数组的元素总数
print(a.shape) #数组的形状,n行m列
print(a.ndim) #数组的维数
print(a.dtype) #数组中元素的类型
3
(3,) #因为a是一维数组
1
int32
b=np.array([[1,2,3],[1,2,4]],dtype=np.float32) #强行数据类型转换
print(b.dtype)
float32
3.修改已创建好的数组
(1)reshape()方法
b.reshape(3,2) #重新设置数组的shape属性
array([[1., 2.],
[3., 1.],
[2., 4.]], dtype=float32)
4.创建等差数组
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
range()和arange()两者的区别仅仅是arange返回的是一个数据,而range返回的是list。
5.创建等比数组
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
在指定的间隔内返回均匀间隔的数字。
返回num均匀分布的样本,在[start, stop]。
这个区间的端点可以任意的被排除在外。
默认五十个数,endpoint=True表示包含尾端点
linspace()
import numpy as np
np.linspace(0,10,10)
array([ 0. , 1.11111111, 2.22222222, 3.33333333, 4.44444444,
5.55555556, 6.66666667, 7.77777778, 8.88888889, 10. ])
import numpy as np
np.linspace(0,10,10,endpoint=False)
array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
logspace()
np.logspace(0,10,10,endpoint=False) #默认以10为底
10**np.linspace(0,10,10,endpoint=False)
结果都是
array([1.e+00, 1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07,
1.e+08, 1.e+09])
np.logspace(0,10,10,endpoint=False,base=2)
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256., 512.])
6.其他创建数组的函数
7.数组数据类型转换
8.创建随机数
9.通过索引访问数组
print(f[:,::2]) #每一行,从开始到结束,隔一个数取
[[9 4]
[0 3]]
10.变换数组的形态
f为
[[9 5 4]
[0 4 3]]
flatten函数可以选择横向或纵向展平
1表示横向,2表示纵向
11.创建numpy矩阵
12.矩阵的运算
12.通用函数
13.numpy读写文件
保存
读取
14.使用数组进行简单数据分析
15.常用的统计函数
16.任务实现