当前位置:网站首页>Four tips in numpy
Four tips in numpy
2022-06-30 06:38:00 【Zhaozhuobufan】
1. introduction
NumPy yes Python One of the most commonly used data science libraries in . It can easily make our daily work simple . This article focuses on Numpy Four skills in , Mastering these skills can make the way we process data more efficient .
Gossip , Let's start straight !
2. Generate special arrays
In Data Science 、 Deep learning and linear algebra , You usually need to generate special arrays , Such as 0 and 1 Array and matrix , Or identity matrix .
NumPy Make the above operation very simple ! Let's see how to use Numpy To code , as follows :
# Creating an array of zeroes
import numpy as np
zeroes = np.zeros(5)
print(zeroes)
# Returns:
# [0. 0. 0. 0. 0.]
In the above code , We used np.zeros() Function to generate an array containing zeros . The above code creates a one-dimensional vector .
Similarly , We can pass in other shape To create other special arrays , as follows :
# Creating Special Arrays in NumPy
import numpy as np
zeros = np.zeros((2,2))
ones = np.ones((2,2))
identity = np.identity(3)
print(zeros)
print(ones)
print(identity)
# Returns:
# [[0. 0.]
# [0. 0.]]
# [[1. 1.]
# [1. 1.]]
# [[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
3. Use where Perform conditional filtration
function np.where() Mainly used for screening ( And replace ) The values in the array . This is a powerful function , It is very convenient to find and replace .
next , Let's start with a basic example of filtering data :
# Filtering Arrays with NumPy where()
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8])
print(np.where(arr > 5))
# Returns:
# (array([5, 6, 7]),)
Similarly , We can use this function to replace the values that meet the filter conditions . Examples are as follows :
# Replacing Values with NumPy where()
import numpy as np
arr = np.array([1,2,3,4,5,6,7,8])
print(np.where(arr > 5, 1, arr))
# Returns:
# [1 2 3 4 5 1 1 1]
In the example above , We will pass in the second parameter instead of the value satisfying the condition . In the third parameter , We pass the value to be used for any object that does not satisfy the condition .
4. Change the shape of the matrix
Changing the shape of the matrix may be in NumPy One of the most common operations to perform in . for example , When using deep neural networks , It is important to ensure that the array has a specific shape . Like other examples ,NumPy Make the above operation very simple !
Let's see how to convert a one-dimensional array to a multi-dimensional matrix :
# Reshaping a NumPy Array
import numpy as np
arr = np.arange(9)
arr = np.reshape(arr, (3,3))
print(arr)
# Returns
# [[0 1 2]
# [3 4 5]
# [6 7 8]]
In the example above , We use np.reshape() Convert a one-dimensional array to 3x3 matrix .
Similarly , We can use np.tranpose() Function to transpose a matrix , As shown below :
# Transposing an Array in NumPy
import numpy as np
arr = np.array([[1,2,3,4]])
print(f'Original Array: \n{arr}')
arr = np.transpose(arr)
print(f'Modified Array: \n{arr}')
# Returns
# Original Array:
# [[1 2 3 4]]
# Modified Array:
# [[1]
# [2]
# [3]
# [4]]
5. Calculate unique values
In the last section , Let's learn how to use functions np.unique() To calculate the unique value in the array .
Of course , We can also pass in counts=True Parameters , At this point, the above function will return a tuple containing a unique value and a count .
Examples are as follows :
# Counting Unique Values in an Array
import numpy as np
arr = [1,1,1,2,2,1,3,4,5,1,2]
values, counts = np.unique(arr, return_counts=True)
print(values)
print(counts)
# Returns:
# [1 2 3 4 5]
# [5 3 1 1 1]
In the example above , The first returned array outputs unique values in the order they appear . The second contains the frequency of occurrence of each value . for example , Array arr Middle number 1 Five times !
6. summary
In this tutorial , We learned to use NumPy Four important methods of . meanwhile Numpy The library is huge , There are many useful features . I hope this tutorial can let us have a deeper understanding of how to effectively use the library for daily work development .

边栏推荐
- VIM view file code
- C # - C # process and convert pixeldata of CT images with fo DICOM
- Use of observer mode and status mode in actual work
- 反編譯正常回編譯出現問題自己解决辦法
- Unable to read file for extraction: gdx64. dll
- Is it safe to open an account online? Can you open an account to speculate on the Internet?
- 不忘初心,能偷懒就偷懒:C#操作Word文件
- Static routing job
- Basic questions (I)
- Never forget the original intention, and be lazy if you can: C # operate word files
猜你喜欢
随机推荐
基础刷题(一)
ini解析學習文檔
Installation and initialization of MariaDB database
Practice summary of Prometheus project in amu Laboratory
Force buckle ------ replace blank space
程序猿入门攻略(十一)——结构体
Hao looking for a job
INI analyse les documents d'apprentissage
Thread safe solutions, communication between threads (classic examples of producers and consumers)
Multithreading advanced level
HCIA day 1
46. full arrangement -dfs double hundred code
Unclear about glide loading picture
MySQL中的InnoDB引擎
To: k210 realizes face recognition (code interpretation attached)
关注这场直播,了解能源行业双碳目标实现路径
Idea run SQL file
Rhcsa day 1
VIM view file code
原来你是这样的数组,终于学会了








