Artificial Intelligence (8): Use of Numpy

1 Introduction to Numpy

Numpy (Numerical Python) is an open source Python scientific computing library for fast processing of arrays of arbitrary dimensions.

Numpy supports common array and matrix operations. For the same numerical calculation tasks, using Numpy is much simpler than using Python directly.

Numpy handles multidimensional arrays using ndarray objects, which are fast and flexible big data containers.

2 Introduction to ndarray

NumPy provides an N-dimensional array type, the ndarray,

which describes a collection of “items” of the same type.

NumPy provides an N-dimensional array type ndarray, which describes a collection of "items" of the same type.

Use ndarray for storage:

import numpy as np
# 创建ndarray
score = np.array(
[[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])
score

Return results:

array([[80, 89, 86, 67, 79],
[78, 97, 89, 67, 81],
[90, 94, 78, 67, 74],
[91, 91, 90, 67, 69],
[76, 87, 75, 67, 86],
[70, 79, 84, 67, 84],
[94, 92, 93, 67, 64],
[86, 85, 83, 67, 80]])

Question:

One-dimensional arrays can be stored using Python lists, and multi-dimensional arrays can be realized by nesting lists, so why do we need to use Numpy's ndarray?

3 Comparison of operation efficiency between ndarray and Python native list

Here we experience the benefits of ndarray by running a piece of code

import random
import time
import numpy as np
a = []
for i in range(100000000):
    a.append(random.random())
# 通过%time魔法方法, 查看当前行的代码运行一次所花费的时间
%time sum1=sum(a)
b=np.array(a)
%time sum2=np.sum(b)

The first time shows the time calculated using native Python, and the second content shows the time calculated using numpy:

CPU times: user 852 ms, sys: 262 ms, total: 1.11 s
Wall time: 1.13 s
CPU times: user 133 ms, sys: 653 µs, total: 133 ms
Wall time: 134 ms

From this we see that the calculation speed of ndarray is much faster, saving time.

The biggest feature of machine learning is a large amount of data operations. If there is no fast solution, then python may not be able to achieve good results in the field of machine learning.

Numpy is specially designed for the operation and calculation of ndarray, so the storage efficiency and input and output performance of arrays are far better than nested lists in Python. The larger the array, the more obvious Numpy's advantages are.

think:

Why can ndarray be so fast?

Guess you like

Origin blog.csdn.net/u013938578/article/details/134130811