4

python原生list数组与numpy的array

 2 years ago
source link: https://www.bobobk.com/321.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

python原生list数组与numpy的array

2021年12月22日

| 技术

在python中存储集合数据可以选择多种原生数据类型,包括list,array,tuple,dictionary四种类型.其中list可变性强,可存储任意内容并且可变,应用范围广泛.而在进行科学运算,存储纯数字时,numpy被广泛应用,可以说基本完全替代了list.那么它们之间有何不同,差距到底有多大,实际过程中应该如何应用呢?

当然,使用实际案例最能说明问题.

运算速度比较

简单的加减乘除,以10000内的数字做个比较.

首先是求和

mylist = []
for i in range(1,10001):
    mylist.append(i)

#  list
from time import time
start = time()
total=sum(mylist)
print(total)
end = time()
print(f"total:{end-start}s")
## 50005000
## total:0.0003197193145751953s

# numpy  np.sum
import numpy as np
myarray = np.array(mylist)
start = time()
total = np.sum(myarray)
print(total)
end = time()
print(f"total:{end-start}s")

## 50005000
## total:0.00041031837463378906s

# numpy sum
start = time()
total = sum(myarray)
print(total)
end = time()
print(f"total:{end-start}s")

## 50005000
## total:0.0012726783752441406s

可以看到,在使用求和时原生的数组求和时间为0.0003,而使用numpy的np.sum却需要0.0004,采用内置的sum求numpy中array的和时耗时最久,为0.001,差不多两倍的时间了.而这还不包括将list转换为array的时间,可见在求和上内置的list明显占据上风.而很多其他文章比较时采用循环的方式当然会慢,但是不符合真实速度.

其次是求积

同样采用mylist数据作为基础,再次比较两者速度

#  list
from time import time
start = time()
total = 1
for i in total:
    total *= i
end = time()
print(f"total:{end-start}s")
## 50005000
## total:0.0003197193145751953s

# numpy  np.sum
import numpy as np
myarray = np.array(mylist)
start = time()
total = np.prod(myarray)
end = time()
print(f"total:{end-start}s")
## total:0.01838994026184082s
## total:0.000213623046875s

在进行连乘时,由于没有内置的乘法,只能采用循环的方式进行,不可避免的造成速度的降低,而numpy由于有prod函数,极大的提升了连乘的计算速度.

本文从实际运算的角度比较了python内置的list与numpy的array的计算速度,发现在计算加和是numpy并不占优势,而且类型转换上会多消耗时间,而在计算连乘时numpy速度提升非常大,因此在计算连乘时numpy下频率高. 总的来说,list应用范围广,求和速度快.而在科学运算,机器学习等领域则使用numpy.因numpy的array在计算连乘等方面速度极快,并且由于pandas的dataframe,series等广泛应用在科学计算上占据绝对优势,也使得其依赖numpy在科学运算中占据绝对优势.


Recommend

  • 5

    关于numpy.array和列表list的区别 Oldpan 2021年1月17日 0条评论 136次阅读 0人点赞 ...

  • 6
    • www.laravelcode.com 2 years ago
    • Cache

    Python Numpy Array Tutorial

    Python Numpy Array Tutorial   49 views   2 weeks ago Python Python is the most popular general purpose program...

  • 6

    Sort a NumPy Array in descending order in Python In this article we will learn how to sort a NumPy Array in descending order. Table Of Contents Given a NumPy Array we need to to...

  • 5

    Remove Last element from a NumPy Array in Python In this article we will learn how to remove last element from a NumPy Array. Given a NumPy array, we need to remove last element from a NumPy Array i.e....

  • 6

    Remove First element from a NumPy Array in Python In this article, we will discuss different ways to remove first element from a NumPy Array. Given a NumPy array, we need to remove first element from th...

  • 11
    • thispointer.com 2 years ago
    • Cache

    Add Row to NumPy Array in Python

    Add Row to NumPy Array in Python In this article, we will learn how to add a row to a 2D NumPy Array in python. Given a NumPy array, we need to add a row to the array. For example,

  • 9

    How to Add Columns to NumPy Array in Python In this article, we will learn how to add a column to a NumPy Array in Python. Table Of Contents Given a 2D NumPy Array, we need to add a Column to...

  • 3

    Remove rows with NaN values from Numpy Array – Python In this article, we will learn how to remove rows with NaN values from a NumPy Array. Table Of Contents The NaN stand...

  • 1

    In this article, we will learn how to convert a NumPy Array to an image in Python. Table Of Contents Given a NumPy array we need to convert it into an image in Python.

  • 4
    • xujinzh.github.io 1 year ago
    • Cache

    Python 中 numpy 数组拼接

    Python 中 numpy 数组拼接发表于2023-04-14|更新于2023-04-17|technology

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK