python数据分析之numpy自学

来源:互联网 发布:四方所 知乎 编辑:程序博客网 时间:2024/06/05 23:47
# -*- coding:utf-8 -*-"""@author: Fane@file: numpyTest.py@time:2017/8/9 21:15"""import numpy as npdef main():    #numpy的基本使用    lst=[[1,3,5],[2,4,6]]    print(type(lst))    np_lst=np.array(lst)  #<type 'list'>    print (type(np_lst))  #<type 'numpy.ndarray'>    np_lst=np.array(lst,dtype=np.float)    print (np_lst.shape)  #(2, 3)    print (np_lst.ndim)   #2   维数    print (np_lst.dtype)  #float64    print (np_lst.itemsize)  #8  64位占用8个字节    print (np_lst.size)      #6  元素个数    #常用固定数组    print ("常用固定数组")    print (np.zeros([2,4]))    #元素为0的2行4列数组    print (np.ones([3,5]))     #元素为1的3行5列数组    #随机数    print("Random:")    print (np.random.rand(2,4)) #2行4列的随机数组(元素0-1之间)    print (np.random.randn(2,4))#2行4列的标准正态分布的随机数组    print (np.random.rand())     #打印一个随机数    print ("RandInt:")    print (np.random.randint(1,10,3))#打印3个1到10之间的随机数    #输出选定数字中    的一个随机数    print (np.random.choice([10,20,30,2,6]))     #输出呈beta分布的1-10之间的100个数    print (np.random.beta(1,10,100))      #numpy的数组操作    #[ 1  2  3  4  5  6  7  8  9 10]    输出1到10的一维数组    print (np.arange(1,11))       #[[ 1  2  3  4  5] 以下两句都是把1-10分成2行5列的二维数组    # [ 6  7  8  9 10]]  -1就是自动分为2行,列数自动填充    print (np.arange(1,11).reshape([2,5]))       print (np.arange(1, 11).reshape([2, -1]))      list=np.arange(1,11).reshape([2,-1])    print ("exp:")    print(np.exp(list))  #自然指数    print(np.exp2(list))  #自然指数的平方    print(np.sqrt(list))  #开方    print(np.sin(list))   #三角函数    print(np.log(list))   #取对数    list2=np.array([[[1,2,3,4],[4,5,6,7]],                    [[7,8,9,10],[10,11,12,13]],                    [[14,15,16,17],[18,19,20,21]]])        #list2为3个2维数组的集合数组    #axis表示计算的深入维度,axis的值越大越深入细化    print (list2.sum(axis=0))      #输出结果为[[1+7+14,2+8+15,3+9+16,...[,,]]以此类推的两行四列的数组    print (list2.sum(axis=1))  #输出结果为[[1+4,2+5,3+6,4+7],[7+10]...[,,]]   以此类推的三行四列的数组    print (list2.sum(axis=2))      #输出结果为[[1+2+3+4,4+5+6+7],[7+8+9+10,10+11+12+13],[,]]   以此类推的三行两列的数组    print ("max and min")    print (list2.max(axis=2))    print (list2.min(axis=0))    ls1 = np.array([4,20,30,40])    ls2 = np.array([10,3,2,1])    print (ls1+ls2)   #可以对数组做加减乘除,平方等等    print (ls1-ls2)    print (ls1**2)    #平方    #数组点乘    print(np.dot(ls1.reshape(2,2),ls2.reshape(2,2)))     #先转化为2x2的两个数组,然后dot点乘    print ("数组的拆分合并:")    print (np.concatenate((ls1,ls2),axis=0))       #合并为一维数组    print (np.vstack((ls1,ls2)))       #合并为二维数组    print (np.hstack((ls1,ls2)))       #合并为一维数组    print (np.split(ls1,2))     #把ls1分成等长的两个数组    print (np.copy(ls1))        #数组拷贝    #数组的矩阵与线性方程       from numpy.linalg import *   #引入包    print (np.eye(3))           #三阶单位矩阵    lst = np.array([[1,2],[3,4]])    print (inv(lst))            #逆矩阵    print (lst.transpose())     #转置矩阵    print (det(lst))            #行列式    print (eig(lst))            #特征值与特征向量    y= np.array([[5.],[7.]])    print (solve(lst,y))          #求解lst和y组成的方程组   x+2y=5   3x+4y=7  得出结果x=-3,y=4    #numpy 的其他应用    print (np.fft.fft(np.array([1,1,1,1,1,1,1,1,])))        #fft信号处理    print(np.corrcoef([1,0,1],[0,2,1]))     #吉尔逊相关系数    #生成一元多次函数2x+x+3if __name__=="__main__":    main()
原创粉丝点击