With the continuous development of data science and deep learning, Python, as one of the mainstream programming languages, its scientific computing library numpy is also constantly innovating. Recently, numpy has released a new version that contains some new features and performance improvements. In this post, we’ll take a deep dive into the new version of numpy and introduce some of its important features and improvements.
Before numpy 1.17.0, the shuffle function would reorder the array elements in random order. However, because the implementation of the shuffle function is different from the standard random algorithm, it may affect performance under certain circumstances. In numpy 1.17.0, the shuffle function was updated to use a new random algorithm, improving its performance and randomness.
The following is a sample code that shows how to use the shuffle function in numpy 1.17.0:
import numpy as np # 创建一个有序数组 arr = np.arange(10) # 将数组随机排序 np.random.shuffle(arr) print(arr)
Output results:
[2 6 5 7 0 9 3 1 4 8]
Numpy version 1.13.0 introduces a new array deduplication method unique, which can handle duplicates faster and easier. In previous versions, numpy used the sort function to sort an array before removing duplicates. However, this approach may cause performance degradation when working with large arrays. In numpy 1.13.0, the unique function uses a hash table algorithm, which has better performance when handling duplicates.
The following is a sample code showing how to use the unique function in numpy 1.13.0:
import numpy as np # 创建一个有重复项的数组 arr = np.array([1, 2, 3, 2, 4, 1, 5, 6, 4]) # 去掉数组中的重复项 arr = np.unique(arr) print(arr)
Output results:
[1 2 3 4 5 6]
Numpy version 1.16.0 introduces a new array assignment method at, which can modify the elements of the array faster and more directly. In previous versions, numpy used loops for array modifications, which resulted in performance degradation. In numpy 1.16.0, the at function is implemented in C code and has higher performance.
The following is a sample code showing how to use the at function in numpy 1.16.0:
import numpy as np # 创建一个3x3的数组 arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # 使用at函数修改数组元素 np.add.at(arr, [0, 1, 2], 1) print(arr)
Output results:
[[ 2 3 4] [ 5 6 7] [ 8 9 10]]
Numpy version 1.14.0 introduces some new array calculation methods, including matmul, einsum and tensordot. These methods make it easier to perform tasks such as matrix calculations and tensor calculations. In previous versions, numpy required the use of a variety of functions to accomplish these tasks, but the new method makes it faster and simpler.
The following is a sample code showing how to use the matmul function for matrix calculations in numpy 1.14.0:
import numpy as np # 创建两个矩阵 a = np.array([[1, 2], [3, 4]]) b = np.array([[5, 6], [7, 8]]) # 使用matmul函数计算矩阵积 c = np.matmul(a, b) print(c)
Output results:
[[19 22] [43 50]]
In addition to the above new features, the new version of numpy also contains some performance improvements. Among them, the most significant improvements are in array copy and array view. In previous versions, numpy required additional copy operations to create array views, resulting in performance degradation. In the latest versions, numpy has improved performance by using a faster method for creating array views. In addition, numpy has also optimized the transpose operation, in1d function and sort function, etc., and has also achieved good performance improvements.
To sum up, the new version of numpy contains some important new features and performance improvements, which make numpy more convenient and efficient. If you need to handle large arrays or perform data science and deep learning tasks, then be sure to upgrade to the latest version of numpy for better performance and functionality.
The above is the detailed content of Interpretation of numpy version updates: new features and improved performance. For more information, please follow other related articles on the PHP Chinese website!