python的numpy库

来源:互联网 发布:linux就该这么学书籍 编辑:程序博客网 时间:2024/06/10 11:10

python的numpy库

1.ndarray

创建ndarray

>>> import numpy as np>>> a = np.array([1,2,3])>>> aarray([1,2,3])

数组的维数和元素的数量由数组的型(shape)来确定。
数组的维统称为轴(axes),属性使用ndim。
轴的数量被称为秩(rank)。
数组的长度使用size属性。

np_list = np.array(list)print(type(np_list))np_list = np.array(list,dtype=np.int32)print("dtype:",np_list.dtype)print("维度:",np_list.ndim)print("数组的型:",np_list.shape)print("数组的每个元素的大小:",np_list.itemsize)#每个元素的类型为int64,占字节为8位print("数组的大小:",np_list.size)

2.random

>>> a = np.zeros([2,4],int)>>> a[[0 0 0 0] [0 0 0 0]]>>> b = np.ones([2,4],int)>>> b[[1 1 1 1] [1 1 1 1]] >>> np.random.rand(2,4)#0~1之间的随机数(24列) [[ 0.27398455  0.34846142  0.74117958  0.56374635] [ 0.04071982  0.41595766  0.05428185  0.68141553]] >>> np.random.rand()#0~1之间的随机数 0.0639271960325064 >>> np.random.randint(1,10)#1~10之间的随机整数 5 >>> np.random.randint(1,20,4)#1~20之间的4位随机整数 [ 7  8 12  6] >>> np.random.randn()#标准正态分布的随机数 -0.6254431609811688 >>> np.random.rand(2,4)#24列的标准正态分布的随机数列 [[ 0.19441426  0.71435207  0.73889068  0.57821704] [ 0.47549352  0.49276035  0.84648264  0.74698625]] >>> np.random.choice([10,20,30],2)#在102030之间的随机两个整数 [20 30] >>> np.random.beta(1,10,4)#1~104个贝塔分布 [ 0.00858948  0.00083287  0.06014702  0.1351401 ]

3.array opes

>>> list = [[1,3,5],[2,4,6]]>>> np.arange(1,11,3)#1~10等公差为3的等差数列[ 1  4  7 10]>>> np.arange(1,11).reshape([2,5])#1~1025列的等差数列[[ 1  2  3  4  5] [ 6  7  8  9 10]] >>> np.exp(list)#自然指数 >>> np.exp2(list)#指数的平方 >>> np.sqrt(list)#开放 >>> np.sin(list)#三角函数 >>> np.log(list)#自然对数 >>> list = np.array(        [[[1,2,3],[4,5,6]],        [[7,8,9],[10,11,12]],        [[14,15,16],[17,18,19]]]) >>> list.sum()#求和操作 177 >>> list.sum(axis=1)#1维求和(axis <= 数组的维度-1),max和min函数同sum函数 [[ 5  7  9] [17 19 21] [31 33 35]]

4.np.array追加

 >>> list1 = np.array([1,2,3,4]) >>> list2 = np.array([5,6,7,8]) >>> np.concatenate((list1,list2),axis=0) [1 2 3 4 5 6 7 8] >>> np.vstack((list1,list2)) [[1 2 3 4] [5 6 7 8]] >>> np.hstack((list1,list2)) [1 2 3 4 5 6 7 8] >>> np.split(list1,2) [array([1, 2]), array([3, 4])] >>> np.copy(list1) [1 2 3 4]

5.liner线性方程

需要导入from numpy.linalg import *

 >>> np.eye(3)#单位矩阵 [[ 1.  0.  0.] [ 0.  1.  0.] [ 0.  0.  1.]] >>> list = np.array([[1.,2.],                     [3.,4.]]) >>> inv(list)#逆矩阵 [[-2.   1. ]] >>> list.transpose()#矩阵的转置矩阵 [[ 1.  3.] [ 2.  4.]] >>> det(list)#矩阵的行列式 -2.0 >>> eig(list)#特征值和右特征向量的矩阵 (array([-0.37228132,  5.37228132]), array([[-0.82456484, -0.41597356],       [ 0.56576746, -0.90937671]]))  >>> y = np.array([[5.],[7.]])  >>> solve(list,y))#计算确定的即完整秩线性矩阵方程ax = b的“精确”解x  [[-3.] [ 4.]]

6.others

  >>> np.fft.fft(np.ones([1,8]))#计算一维离散傅里叶变换  [[ 8.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j  0.+0.j]]  >>>np.corrcoef([1,0,1],[0,2,1])#返回Pearson乘积矩相关系数  [[ 1.        -0.8660254] [-0.8660254  1.       ]] >>> np.poly1d([2,1,3])#一元多次函数,一个方便类,用于将“自然”操作封装在多项式上 2 2 x + 1 x + 3

7.保存和读取二级制文件

 >>> a = np.random.rand(4,4)  [[ 0.05091429  0.07073984  0.70885816  0.32457814] [ 0.76351142  0.9994996   0.79168411  0.11646921] [ 0.1555821   0.745124    0.53163456  0.17308067] [ 0.73460196  0.7243972   0.20994666  0.05945963]] >>> np.save("Binary",a)#保存二级制文件(自动加后缀.npy) >>>load_data = np.load("Binary.npy") >>>load_data [[ 0.05091429  0.07073984  0.70885816  0.32457814] [ 0.76351142  0.9994996   0.79168411  0.11646921] [ 0.1555821   0.745124    0.53163456  0.17308067] [ 0.73460196  0.7243972   0.20994666  0.05945963]]

8.读取文件中列表形式数据

data.csv文件内容
id,value1,value2,value31,234,1.2,122,110,1.4,343,212,3.5,45

Numpy的genfromtxt()函数

作用:可以从文本文件中读取数据病将其插入数组中参数:存放数据的文件名     用于分割值的字符     是否含有标题
>>> data = np.genfromtxt("data.cvs",delimiter=',',names=True)>>> data[( 1.,  234.,  1.2,  12.) ( 2.,  110.,  1.4,  34.) ( 3.,  212.,  3.5,  45.)]
原创粉丝点击