numpy是一个多维数组库,意味着你可以使用它存储各种数据:一维数组、二维数组、三维数组、四维数组等。
numpy array与list的区别
最主要的区别在于速度
列表速度很慢,而numpy速度很快。
一个原因是numpy使用固定类型,例如在我们创建numpy一个3×4的整数矩阵时,numpy中的元素我们根据我们的需要可以设置为int32、int16、int8,以节约内存。
import numpy as np
a = np.array([1, 2, 3], dtype = 'int8' )
a #运行结果:array([1, 2, 3], dtype=int8)
若在列表中存储,列表使用python的内置int类型(包括Size、Reference Count、Object Type、Object Value),它需要比numpy更多的空间。
numpy使用较少的内存字节,而计算机可以更快地读取较少的字节,而且计算机不用迭代地检查numpy中数组的元素类型。
另一个原因是numpy利用了连续内存,第一个好处是而列表的数据分散在我们计算机内存信息中。
总结:
一个numpy array 是内存中一个连续块,并且array里的元素都是同一类(例如整数)。所以一旦确定了一个array,它的内存就确定了,那么每个元素(整数)的内存大小都确定了。
list完全不同,它的每个元素其实是一个地址的引用,这个地址又指向了另一个元素,这些元素的在内存里不一定是连续的。所以list其实是只能塞进地址的”数组”,而且由于地址不用连续,每当我想加入新元素,我只用把这个元素的地址添加进list。
list中的数据类型不必相同的,而array的中的类型必须全部相同。(在list中的数据类型保存的是数据的存放的地址,简单的说就是指针,并非数据,这样保存一个list就太麻烦了,例如list1=[1,2,3,’a’]需要4个指针和四个数据,增加了存储和消耗cpu。)
其他区别
在列表和numpy中,我们可以执行插入、删除、添加、结合等操作,但是numpy实现的操作比列表多。
例如,矩阵的点乘操作:
#list
a=[1,3,5];b=[2,4,6]
a*b #报错
#numpy
a=np.array([1,3,5]);b=np.array([2,4,6])
a*b # array([ 2, 12, 30])
二者相互转换
#a为python的list类型
#将a转化为numpy的array:
np.array(a)
#将a转化为python的list
a.tolist()
numpy的应用?
1.数学运算
可以使用numpy进行各种数学运算,代替MATLAB。
2.画图
画图(使用matplotlib)
3.后端
可以作为pandas、connect 4等的后端
4.机器学习
完成一些机器学习的工作
numpy的基本语句
import numpy as np
The Basis
a= np.array([1,2,3], dtype='int32')
print(a) #运行结果:[1 2 3]
b = np.array([9.0, 8.0, 7.0],[6.0, 5.0, 4.0]) 是错的 必须在前面在+一层[]
b = np.array([[9.0, 8.0, 7.0],[6.0, 5.0, 4.0]])
print(b) #运行结果:[[9. 8. 7.]
[6. 5. 4.]]
GET Dimension
print(a.ndim) #运行结果:1
GET Shape
print(a.shape) #运行结果:(3,)
print(b.shape) #运行结果:(2, 3)
GET Type
print(a.dtype) #运行结果:int32
#Total size
b.size #运行结果:6
Accessing/Changing specific elements, rows, columns, etc
a= np.array([[1,2,3,4,5,6,7],[8,9,10,11,12,13,14]])
a #运行结果:array([[ 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14]])
Get a specific element [r,c]
a[1,-2] #运行结果:13
initializing different types of arrays
np.zeros((2,3)) #全0数组
np.ones((4,2,2), dtype='int32') #全1数组
np.full((2,2),99) #2*2的每个元素均是99的数组
#Random decimal numbers
np.random.rand(4,2) #生成0~1之间的随机数填充4*2数组
Random Integer values
np.random.randint(80,100,size=(3,3)) #生成80~100之间的随机整数填充3*3数组
生成不重复数字是数组
import random
total = 100
li = [i for i in range(total)]
res = []
num = 20
for i in range(num):
t=random.randint(i,total-1)
res.append(li[t])
li[t],li[i]=li[i],li[t]
print(res)
生成单位矩阵
np.identity(5)
arr= np.array([[1,2,3]])
r1=np.repeat(arr,3,axis=0)
r1
Be careful when copying arrays!!(在复制数组时要注意!)
a=np.array([1,2,3,4])
print(a+2) #[3 4 5 6]
print(a*2) #[2 4 6 8]
#Linear Algebra
a=np.ones((2,3))
print(a)
b=np.full((3,2),2)
print(b)
np.matmul(a,b)
Find the determinant
c=np.identity(3)
np.linalg.det(c)
Statistics
stats= np.array([[1,2,3],[4,5,6]])
print(stats)
print(np.min(stats))
print(np.max(stats,axis=1))
#Reorganizing Arrays
before = np.array([[1,2,3,4],[5,6,7,8]])
print(before)
after = before.reshape((2,2,2))
print(after)
Vertically stacking vectors
v1 = np.array([1,2,3,4])
v2 = np.array([5,6,7,8])
np.vstack([v1,v2,v2,v2])
Horizontal stack
h1=np.ones((2,4))
h2=np.zeros((2,2))
np.hstack((h1,h2))
#Load data from file
filedata=np.genfromtxt('data.txt',delimiter=',')
filedata.astype('int32')
#Boolean Masking and Advanced Indexing
print(filedata>50)
print(filedata[filedata>50])
参考链接:https://www.bilibili.com/video/BV1sV411t7Gw?from=search&seid=14497464106551140268&spm_id_from=333.337.0.0
Original: https://blog.csdn.net/weixin_45928096/article/details/122567892
Author: 来包番茄沙司
Title: python中numpy array与list的区别 & numpy的应用 & numpy的基本使用
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/763132/
转载文章受原作者版权保护。转载请注明原作者出处!