当前位置:网站首页>Slice and index of array with data type
Slice and index of array with data type
2022-07-03 08:49:00 【Thebluewinds】
Numpy Array basic index and slice attached ndarray Data type of
One 、 Slicing of arrays
Slice of the list
In understanding numpy Before the array slice of , Let's first look at list Slicing operation . obviously , Slicing will copy the selection of the original array and assign it to a new variable list_slice, In this way, when operating on slices , The original list value will not change .
list = [1 for x in range(10)]
print(list)
list_slice = list[3:5]
list_slice[1] = 12
print(list)
print(list_slice)
""" output: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [1, 1, 1, 1, 1, 1, 1, 1, 1, 1] [1, 12] """
Automatic propagation of array slices
Perform slicing operations, such as arr[2:3]=1 when , This assignment will be automatically propagated to the original array . For example, the following operations :
First create an all one array . Then on arr Slice and give another variable arr_slice. Then on arr_slice Assign a value to the second value of , Then the output arr. You can go to the original array arr It has also changed . This explanation , We are in the process of slicing , An array slice is just a reduced view of the original array . It means , Slicing does not copy data , Any numerical changes in the slice will be reflected in the original array .
arr_slice = arr[3:5]
arr_slice[1]=12
print(arr)
# output : [ 1. 1. 1. 1. 12. 1. 1. 1. 1. 1.]
Two 、 Boolean index
First, let's look at a failed case :
You can see , The output result is false, Note: Boolean index operation cannot be directly performed on the list .
import numpy as np
name = ['wuqian', 'hua','wuqian', 'wanglihon']
date = np.random.randn(4,4)
name == 'wuqian'
#output:False
You should first convert the character list into array type .
import numpy as np
name = np.array(['wuqian', 'hua','wuqian', 'wanglihon'])
data = np.random.randn(4,4)
name == 'wuqian' # This operation is the same as arithmetic operation , Array comparison operations are also vectorized , Will be compared one by one , Then the output True or False
# The output is a Boolean array
""" output:array([ True, False, True, False]) """
# Then the Boolean array can index the array
data[name == 'wuqian']
""" The output is data In the corresponding Boolean array of data True Location data . # output: array([[-0.05095068, -0.21921446, 0.30479235, 0.07790257], [-0.46580753, -0.3457661 , -0.40843969, -0.97014426]]) """
One point that needs great attention is
Use this index , The length of Boolean array must be consistent with the axis length of root index .
In addition, it can be used flexibly &、|、!= Boolean arithmetic symbols such as non, or greater than less than , Here's an example :
huairen = (name == 'wuqian')|(name = 'hua')
name !='wuqian'
score = np.array([100, 99, 78, 12, 0, 65])
score = score.astype(np.unicode_)
score[score>'60'] = ' pass '
score
# output: array(['100', ' pass ', ' pass ', '12', '0', ' pass '], dtype='<U11')
3、 ... and 、Ndarray Data type of
1. Data type view
arr1 = np.array([1,3,4])
arr1.dtype
# If no specific data type is specified , It's usually float64
Data type symbol
string_ Fixed length string type , One byte per character
unicode_ Fixed length unicode type , The number of bytes depends on the platform used
float64 Standard single precision floating point numbers
…
2. How to change the data type
You can specify the data type when creating data :
arr = np.array([1,3,4,1],dtype = np.int64)
If the array has been established, it needs to be changed , You can do the following :
arr = arr.astype(np.unicode)
边栏推荐
- First Servlet
- MySQL index types B-tree and hash
- Markdown directory generation
- Unity editor expansion - controls, layouts
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- Cesium for unreal quick start - simple scenario configuration
- Notes and bugs generated during the use of h:i:s and y-m-d
- Unity notes 1
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
猜你喜欢

Unity editor expansion - controls, layouts

First Servlet

OpenGL learning notes

SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership
![[concurrent programming] Table hopping and blocking queue](/img/b7/023991a00956e469af855e7a81e126.jpg)
[concurrent programming] Table hopping and blocking queue

Deep parsing JVM memory model

Log4j2 vulnerability recurrence and analysis

Chocolate installation

22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
随机推荐
Dom4j traverses and updates XML
[rust note] 10 operator overloading
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
[concurrent programming] explicit lock and AQS
JS ternary operator - learning notes (with cases)
Unity editor expansion - the framework and context of unity imgui
First Servlet
如何应对数仓资源不足导致的核心任务延迟
[concurrent programming] working mechanism and type of thread pool
Convert video to GIF
Allocation exception Servlet
Servlet的生命周期
Markdown directory generation
ES6 promise learning notes
How to delete CSDN after sending a wrong blog? How to operate quickly
Monotonic stack -42 Connect rainwater
Try to reprint an article about CSDN reprint
Intersectionpicker in osgearth
Really explain the five data structures of redis
Redux - learning notes