Preface
list是Python最基本的数据结构,就像perl中的array,用中括号[]阔起来,属性和操作也几乎一样。Python中没有一个叫做array的数据结构,而numpy中的array()函数可以将list变成array对象,这个时候用起来就跟perl中的array几乎一样了。
1. Create numpy array:
numpy.array(object, dtype =, ndmin = ) (dtype= bool, int, unit,float, complex)
numpy.empty(shape, dtype =, order = ) (shape [3, 2]表示三行两列)
numpy.random.randn(5)
numpy.zeros(shape, dtype =, order = )
numpy.ones(shape, dtype =, order = )
numpy.arange(start, stop, step, dtype =)
numpy.linspace(start, stop, num, endpoint =, dtype = )等差数列
numpy.logspace(start, stop, num, endpoint =, dtype = )等比数列
numpy.asarray(a, dtype =, order =),a可以是任何参数,如list,tuple,list of list,等等
2. Attributes of numpy array:
ndarray.ndim 数组的维数
ndarray.shape 数组的具体维度,输出为一个tuple
ndarray.size 数组元素的总个数
ndarray.dtype 数组元素类型
3. Slicing and indexing of numpy array:
b=a[start:stop:step]
b=a[n]
b=[n:]
b=a[:n]
b=a[m:n]
b=a[…,2] 第三列所有元素
b=a[2,…] 第三行所有元素
b=a[2:,…] 第三行以及所有剩下的元素
b=a[[0,1,2],[0,1,0]] a数组中(0,0),(1,1)和(2,0)位置处的元素
b=a[a>5] a中出大于5的元素
4. Operation of np array:
a.reshape(newshape) 修改数组形状
for x in a.flat 数组元素迭代器,遍历数组元素
for x in np.nditer(a, order =, op_flags =, ) 数组元素迭代器,遍历数组元素
b=a.flatten(order = ) 拷贝多维数组并展开为一维数组
b=a.ravel(order = ) 多维数组展开为一维数组
b=np.transpose(a) 对换数组维度(转置数组)
b=a.T 对换数组维度(转置数组)
b=a.split(a, sections/indices, axis) 分割数组
b=a.hsplit(a, sections) 水平将数组分割成几个相同维度的数组
b=a.vsplit(a, sections) 垂直将数组分割成几个相同维度的数组
b=np.resize(a,(x,y)) 回指定大小的新数组
b=np.append(a, values, axis=) axis无定义时返回一维数组,axis为0时叠加行,axis为1时叠加列
b=np.insert(a, location, values, axis) axis无定义时返回一维数组,axis为0时叠加行,axis为1时叠加列
b=np.delete(a, object, axis) axis无定义时返回一维数组,axis为0时叠加行,axis为1时叠加列
b=np.unique(a,return_counts =) 去除重复元素
a+b (shape(a)不等于shape(b)的时候,触发广播机制)
y=np.concatenate((a, b), axis =) 叠加数组,不增加维度
y=np.stack((a, b), axis =) 叠加数组,增加维度
y=np.hstack((a, b), axis =) 水平叠加数组,不增加维度
y=np.vstack((a, b), axis =) 垂直叠加数组,不增加维度
5. Functions of np array:
i)位函数
np.botwise_and(a, b) 对a和b进行位与操作
np.botwise_or(a, b) 对a和b进行位或操作
ii)字符串函数
np.char.add(a, b) 依次对两个数组的元素进行字符串连接
np.char.multiply(a, 3) 多重连接字符串
np.char.center(a, width, fillchar) 将字符串居中,并使用指定字符在左侧和右侧进行填充
np.char.capitalize(a) 将字符串的第一个字母转换为大写
np.char.title(a) 将字符串的每个单词的第一个字母转换为大写
np.char.lower(a) 对数组的每个元素转换为小写
np.char.upper(a) 对数组的每个元素转换为大写
np.char.split(a, sep =) 指定分隔符对字符串进行分割,并返回数组
np.char.strip(a, character) 移除开头和结尾处的特定字符
np.char.join(‘ligase’, a) 指定分隔符来连接数组中的元素或字符串
np.char.replace(a, m, n) 用字符串n替换a中的字符串m
b=np.char.encode(a) 对数组中的每个元素调用str.encode函数,默认编码是 utf-8
b=np.char.decode(a) 对编码的元素进行str.decode()解码
iii)数学函数
b=np.sin(a), cos(a), tan(a)
b=np.degrees(a) 将弧度转换为角度
b=np.around(a, decimals =) 返回指定数字的四舍五入值
b=np.floor(a) 返回a的下舍整数
b=np.ceil(a) 返回a的上入整数
iv)算术函数
np.add(a, b) 相加
np.subtract(a, b) 相减
np.multiply(a, b) 相乘
np.divide(a, b) 相除
np.reciprocal(a) 倒数
np.power(a, n) a的n次方
np.mod(a, b) 计算输入数组中相应元素的相除后的余数
v)统计函数
np.amin(a, axis) 数组中的元素沿指定轴的最小值
np.amax(a, axis) 数组中的元素沿指定轴的最大值
np.ptp(a, axis) 数组中元素最大值与最小值的差
np.percentile(q, axis) 数组的百分位数
np.median(a, axis) 数组的中位数
np.mean(a, axis) 数组的算数平均值
np.average(a, axis =,weights =) 数组的加权平均值
np.std(a) 数组的标准差
np.var(a) 数组的方差
vi)排序
np.sort(a, axis, kind, order) 返回输入数组的排序副本
np.argsort(a) 返回数组值从小到大的索引值
np.partition(a, x) 用x对a进行partition
np.argmax(a, axis =) 沿给定轴返回最大元素的索引
np.argmin(a, axis =) 沿给定轴返回最小元素的索引
np.nonzeros(a) 数组中非零元素的索引
np.where(condition) 输入数组中满足给定条件的元素的索引
np.extraction(condition, x) 根据某个条件从数组中抽取元素,返回满条件的元素
6. IO of np array
np.save(file, a, allow_pickle= ,fix_imports= ) 将数组保存到以 .npy 为扩展名的文件中
np.savez(file, args) 将多个数组保存到以 .npy 为扩展名的文件中
np.savetxt(file, a, delimiter =) 以简单的文本文件格式存储数据
np.loadtxt(file, dtype=, delimiter =) 以简单的文本文件格式读取数据
b=np.load(file) 读取文件
7. matplotlib of np array
i)基本
plt.subplot(2, 1, 1) 建立subplot网格,高为2,宽为1, 激活第一个subplot
pyplot.title("Matplotlib demo")
plt.xlabel("x axis caption")
plt.ylabel("y axis caption")
plt.plot(x,y,"ob")
plt.subplot(2, 1, 2) 激活第二个subplot
…
plt.show()
ii)条形图
x = [5,8,10]
y = [12,16,6]
plt.bar(x, y, color=‘g’, align='center') 生成条形图
plt.title('Bar graph')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
iii)频率分布图
a = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
plt.hist(a, bins=[0,20,40,60,80,100])
plt.title("histogram")
plt.show()