博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《python数据分析组键篇》之numpy
阅读量:4230 次
发布时间:2019-05-26

本文共 3743 字,大约阅读时间需要 12 分钟。

前言

python中的list可以存放多种元素,造成了维护成本高,遍历速度慢,所以采用使用底层语言写的numpy,提高了速度。

第一部分ndarry

import numpy as nplis = [[1,2],[1,3]]print type(lis)#
np_lis1 = np.array(lis)np_lis2 = np.array(lis,dtype=np.float)print type(np_lis1)print type(np_lis2)#
#
print np_lis1.shape # 形状 : 几行几列:(2L, 2L)print np_lis1.ndim # 维度 : 2print np_lis1.dtype # 类型 : int32print np_lis2.dtype # 类型 : float64print np_lis1.itemsize # 每个元素大小 : 4字节print np_lis1.size # 大小 : 一共有多少元素 4个,注意不是两个。

np只允许有一种数据类型,如bool, int8/16/32/64, float16/32/64,complex复数 等等,正因为如此,则需要限定其元素类型

使用
np_lis = np.array(lis,dtype=np.float)
注意:dtype的类型 需要加np

np.array 中的特殊属性

np.shape # 形状 : 几行几列
np.ndim # 维度
np.dtype # 类型
np.itemsize # 每个元素大小字节
np.size # 大小 : 一共包含所有的元素个数

第二部分 常用Array

import numpy as npnp.zeros([2,4])# 2行4列的0矩阵               np.ones([3,5])                # 3行5列的单位矩阵np.random.rand(2,4)           # 2行4列的随机数(均匀分布)np.random.rand()              # 一个随机数  : 0.18846516np.random.randint(1,10,2)     # 随机整数 low high size : [9 3]np.random.randn(2,4)          # 标准正态分布np.random.choice([10,20,30])  # 选择指定数组的随机数 : 10np.random.beta(1,10,5)        #B分布 1-10 生成5  low high size

第三部分 Array 的常用操作

numpy是可以的改变的

import numpy as npprint np.arange(1,11)                 # 等差数列 ,1到10print np.arange(1,11).reshape(2,5)    # reshape 修改数组维度print np.arange(1,11).reshape(2,-1)   # 其中5可以缺省为负数print "---------------------"lis = np.arange(1,11).reshape(2,-1)print np.exp(lis)    # 自然指数print np.exp2(lis)   # 自然指数的平方print np.sqrt(lis)   # 开方print np.sin(lis)print np.cos(lis)print np.log(lis)    # 对数

输出结果

np.arange(1,11)

np.arange(1,11).reshape(2,5)

[[ 1 2 3 4 5]
[ 6 7 8 9 10]]

np.arange(1,11).reshape(2,-1)

[[ 1 2 3 4 5]
[ 6 7 8 9 10]]

函数操作

import numpy as nplis = 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]]                ])print lis.sum()          #252 所有的元素的求和# 可以通过维度进行求和,从0开始,深度从浅到深print lis.sum(axis=0)# 深度最浅,则认为是三个元素,每个元素对应位置相加,计算深度为最外层# [[22 25 28 31],[32 35 38 41]]print lis.sum(axis=1)# 深度为1 ,则进入每个元素中,在每个元素的对应位置相加,计算深度为每个元素层#[[ 5  7  9 11],[17 19 21 23],[32 34 36 38]]print lis.sum(axis=2)# 深度为2, 则进入每个元素中的小元素,对每个小元素求和,计算深度为每个元素内的元素层#[[10 22],[34 46],[62 78]]# 类似的有相似的函数lis.max(axis=)lis.min(axis=)

两个array的操作

import numpy as nplis1 = np.array([10,20,30,40])lis2 = np.array([4,3,2,1])print lis1+lis2     # [14 23 32 41]print lis1-lis2     # [ 6 17 28 39]print lis1*lis2     # [40 60 60 40]print lis1/lis2     # [ 2  6 15 40]# 平方print lis1**2       # [ 100  400  900 1600]# 点乘print np.dot(lis1.reshape([2,2]),lis2.reshape([2,2]))# [[ 80  50]#  [200 130]]# 追加print np.concatenate((lis1,lis2),axis=0)#np.concatenate(a_tuple=,axis=)#[10 20 30 40  4  3  2  1]# 追加成两行print np.vstack((lis1,lis2))# [[10 20 30 40]#  [ 4  3  2  1]]# 追加成一行print np.hstack((lis1,lis2))# [10 20 30 40  4  3  2  1]print "---------------------------"ls1 = [10,20,30,40]ls2 = [4,3,2,1]print ls1+ls2   #追加# [10, 20, 30, 40, 4, 3, 2, 1]# 区别就是 这是个list 有逗号

第四部分 矩阵操作和线性方程组

import numpy as npfrom numpy.linalg import *# numpy.linalg模块包含线性代数的函数。使用这个模块,# 可以计算逆矩阵、求特征值、解线性方程组以及求解行列式等。# 生成单位矩阵lst = np.eye(3)print lst# [[ 1.  0.  0.]#  [ 0.  1.  0.]#  [ 0.  0.  1.]]lis = np.array([[1.,2.],[3.,4.]])print '矩阵的逆:'print inv(lis)print '矩阵的转置:'print lis.transpose()print '行列式:'print det(lis)print '特征值和特征向量:'print eig(lis)   # 结果第一个array是特征值第二array是特征向量print '解方程组:'y = np.array([[5.],[7.]])print solve(lis,y)

输出结果:

单位矩阵 np.eye()

[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]

矩阵的逆: inv(lis)

[[-2. 1. ]
[ 1.5 -0.5]]

矩阵的转置:lis.transpose

[[ 1. 3.]
[ 2. 4.]]

行列式:det(lis)

-2.0

特征值和特征向量: eig(lis)

(array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))

解方程组:solve(a,b)

[[-3.]
[ 4.]]

转载地址:http://fliqi.baihongyu.com/

你可能感兴趣的文章
Beginning SQL Server 2005 Express for Developers: From Novice to Professional
查看>>
Multimedia over IP and Wireless Networks: Compression, Networking, and Systems
查看>>
Hacking Ubuntu: Serious Hacks Mods and Customizations
查看>>
PHP 5 Advanced: Visual QuickPro Guide
查看>>
Adobe Illustrator CS3 Classroom in a Book
查看>>
Sams Teach Yourself Adobe Photoshop CS3 in 24 Hours
查看>>
FileMaker Pro 8.5 Bible
查看>>
AutoCAD: Secrets Every User Should Know
查看>>
Combating Spyware in the Enterprise
查看>>
Microsoft Windows Vista Unveiled
查看>>
How to Cheat at Securing a Wireless Network
查看>>
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
查看>>
Smarty Php Template Programming And Applications
查看>>
Web Site Measurement Hacks
查看>>
The Best Damn Windows Server 2003 Book Period
查看>>
Cleaning Windows XP For Dummies
查看>>
The Windows 2000 Device Driver Book: A Guide for Programmers (2nd Edition)
查看>>
Python in a Nutshell
查看>>
Microsoft Visual C++ .NET Professional Projects
查看>>
Excel 2007 Advanced Report Development
查看>>