当前位置:网站首页>Numpy --- basic learning notes
Numpy --- basic learning notes
2022-07-07 15:50:00 【madkeyboard】
Numpy and List
List The functions that can be done are roughly the same as Numpy It's almost the same , however Numpy The advantage of is that the operation is very fast , because Numpy In storage, a piece of continuous physical address of the computer is used to store data , It takes less time to search data , and List The stored data is scattered in different physical spaces .
So when dealing with large-scale data ,Numpy than List The effect is good .

An example is given to demonstrate the time they spend processing data separately , As you can see from the results , Let a random input increase 10000 Time ,Numpy The calculation speed of is about 10 times , If there's more data , The effect should be better .
import time
import numpy as np
t0 = time.time()
# python list
l = list(range(100))
for _ in range(10000):
for i in range(len(l)):
l[i] += 1
t1 = time.time()
# numpy array
a = np.array(l)
for _ in range(10000):
a += 1
print("Python list spend {:.3f}s".format(t1-t0))
print("Numpy array spend {:.3f}s".format(time.time()-t1))

dimension
Numpy One of its advantages is that it can handle multidimensional data , In machine learning and artificial intelligence, the calculation of multidimensional data often occurs .
Create multidimensional data
For example, it takes time for us to test a car to accelerate for hundreds of miles , First create a list of hundred mile accelerations
car = np.array([5,12,3,13])
print(" Time consuming : ",car,"\n dimension :",car.ndim)

You can see that the output is one-dimensional , Then if you add several groups of data, it means multiple tests , You create two-dimensional data
car = np.array(
[[5,12,3,13],
[5.2,11,14,5],
[6.1,6.6,4.3,6.5]])
print(" Time consuming : ",car,"\n dimension :",car.ndim)

Empathy , Create a three-dimensional representation to test the vehicle on different sites .
car = np.array([
[
[5, 10, 12, 6],
[5.1, 8.2, 11, 6.3],
[4.4, 9.1, 10, 6.6]
],
[
[6, 11, 13, 7],
[6.1, 9.2, 12, 7.3],
[5.4, 10.1, 11, 7.6]
],
])
print(" The total dimension :", car.ndim)
print(" site 1 data :\n", car[0], "\n site 1 dimension :", car[0].ndim)
print(" site 2 data :\n", car[1], "\n site 2 dimension :", car[1].ndim)

Add data
Add one-dimensional data .np.concatenate()
cars1 = np.array([5, 10, 12, 6])
cars2 = np.array([5.2, 4.2])
cars = np.concatenate([cars1, cars2])
print(cars) #[ 5. 10. 12. 6. 5.2 4.2]
Add 2D data
test1 = np.array([5, 10, 12, 6])
test2 = np.array([5.1, 8.2, 11, 6.3])
# First, we need to turn them into two dimensions , The following two methods can add dimensions
test1 = np.expand_dims(test1, 0)
test2 = test2[np.newaxis, :]
print("test1 After adding dimensions ", test1)
print("test2 After adding dimensions ", test2)
# Then superimpose on the first dimension
all_tests = np.concatenate([test1, test2])
print(" After the exhibition \n", all_tests)

Merge data
As long as the dimensions can be aligned , You can merge on any dimension .
import time
import numpy as np
test1 = np.array([5, 10, 12, 6])
test2 = np.array([5.1, 8.2, 11, 6.3])
# First, we need to turn them into two dimensions , The following two methods can add dimensions
test1 = np.expand_dims(test1, 0)
test2 = test2[np.newaxis, :]
all_tests = np.concatenate([test1, test2])
print(" The first dimension is superimposed :\n",np.concatenate([all_tests,all_tests],axis=0))
print(" The second dimension is superimposed :\n",np.concatenate([all_tests,all_tests],axis=1))

np.hstack() Horizontal merger
np.vstach() Vertical merge
- Observation form
test1 = np.array([
[5, 10, 12, 6],
[5.1, 8.2, 11, 6.3],
[4.4, 9.1, 10, 6.6]
])
print(" According to the total :",test1.size)
print(" The first dimension ",test1.shape[0]) # Indicates how many tests have been performed
print(" The second dimension ",test1.shape[1]) # Indicates the number of vehicles tested
print(" All dimensions ",test1.shape)
# The first dimension : 3
# The second dimension : 4
# All dimensions : (3, 4)
Data selection
- Single choice
import numpy as np
b = np.array([
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
])
print("b[1]:\n",b[1]) # Select all numbers in the second row
print("b[2,1]:\n",b[2,1]) # Select the number of the third row and the second column
print("b[[1,0],[2,3]]:\n",
b[[1,0],
[2,3]]) # The two numbers taken are [1,2] and [0,3]

- section
print("b[1]:\n",b[:2]) # Select all the numbers in the first and second rows
print("b[2,1]:\n",b[:2,:3]) # Select the first and second rows , front 3 Number of columns

- To choice
import numpy as np
b = np.array([
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
])
condition = (b < 5) & (b != 2)
print(b[condition]) # 1 3 4
print(np.where(condition,-2,b)) # Replace the value satisfying the condition with -2
print(np.where(condition,-2,-1)) # Replace the value satisfying the condition with -2, If not, replace with -1
Basic operation
import numpy as np
a = np.array([
[1,2],
[3,4]
])
b = np.array([
[5,6],
[7,8]
])
print(a.dot(b)) # Matrix dot product print(np.dot(a, b))
np.max() # Maximum
np.min() # minimum value
np.sum() # Sum up
np.prod() # Multiplicative multiplication
np.size() # total
np.count_nonzero() # Nonzero total
np.mean() # The average
np.median() # Median
np.std() # Standard deviation
np.argmax() # The subscript of the maximum value
np.argmin() # Subscript of minimum value
np.ceil() # Rounding up
np.floor() # Rounding down
np.clip(a,b) # Define a rounded upper 、 Lower bound
a = np.array([150.1, 166.4, 183.7, 170.8])
print("clip:", a.clip(160, 180))
# clip: [160. 166.4 180. 170.8]
# Official documents https://numpy.org/devdocs/user/quickstart.html#basic-operations
Change the data form
Add dimensions
a_2d = a[np.newaxis, :] a_none = a[:, None] a_expand = np.expand_dims(a, axis=1) # These three methods can achieve the same effectReduce dimensions
a_squeeze = np.squeeze(a_expand) a_squeeze_axis = a_expand.squeeze(axis=1) # squeeze Only dimensions can be reduced shape Up for 1 Dimensions , Ensure that the data structure does not change # If you want to change the dimension, you can use reshape a = np.array([1,2,3,4,5,6]) a1 = a.reshape([2, 3]) a2 = a.reshape([3,1,2]) # I don't understand here # a1 shape: (2, 3) [[1 2 3] [4 5 6]] a2 shape: (3, 1, 2) [[[1 2]] [[3 4]] [[5 6]]] # # Matrix transposition np.transpose() np.TMerge
np.column_stack() # columns np.row_stack() # merger # The above two merge methods are similar to vstack and hstack The difference is that : Use vstack and hstack You need to process dimension information first , and column_stack and row_stack Automatic processing feature_a = np.array([1,2,3,4,5,6])[:, None] feature_b = np.array([11,22,33,44,55,66])[:, None] c_stack = np.hstack([feature_a, feature_b]) # np.concatenate() It is suitable for merging in different situations np.concatenate([a, b], axis=0)Take apart
a = np.array( [[ 1, 11, 2, 22], [ 3, 33, 4, 44], [ 5, 55, 6, 66], [ 7, 77, 8, 88]] ) print(np.vsplit(a, indices_or_sections=2)) # In two parts print(np.vsplit(a, indices_or_sections=[2,3])) # Slice into [:2],[2:3], [3:] np.hsplit() # Crosscut The function is similar to
a = np.array( [[ 1, 11, 2, 22], [ 3, 33, 4, 44], [ 5, 55, 6, 66], [ 7, 77, 8, 88]] ) print(np.split(a, indices_or_sections=2, axis=0)) # In two parts print(np.split(a, indices_or_sections=[2,3], axis=1)) # In the second dimension , Slice into [:2],[2:3],[3:] # Use split Custom segmentation
边栏推荐
- Whole process analysis of unity3d rendering pipeline
- Detailed explanation of Cocos creator 2.4.0 rendering process
- Syntax of generator function (state machine)
- Clang compile link ffmpeg FAQ
- Virtual memory, physical memory /ram what
- Excerpted words
- 航運船公司人工智能AI產品成熟化標准化規模應用,全球港航人工智能/集裝箱人工智能領軍者CIMC中集飛瞳,打造國際航運智能化標杆
- [original] all management without assessment is nonsense!
- Super simple and fully automated generation super signature system (cloud Xiaoduo minclouds.com cloud service instance), free application in-house test app distribution and hosting platform, maintenan
- [quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
猜你喜欢

Spin animation of Cocos performance optimization
![[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)](/img/7e/188e57ee026200478a6f61eb507c92.png)
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
![[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)](/img/d3/cab8a1cba3c8d8107ce4a95f328d36.png)
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
![[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)](/img/d3/cab8a1cba3c8d8107ce4a95f328d36.png)
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)

Zhongang Mining: Fluorite continues to lead the growth of new energy market

When opening the system window under UE4 shipping, the problem of crash is attached with the plug-in download address

numpy--疫情数据分析案例

TS as a general cache method

Ue4/ue5 multi thread development attachment plug-in download address

Three. JS introductory learning notes 08:orbitcontrols JS plug-in - mouse control model rotation, zoom in, zoom out, translation, etc
随机推荐
Three. JS introductory learning notes 00: coordinate system, camera (temporarily understood)
Three. Introduction to JS learning notes 17: mouse control of 3D model rotation of JSON file
Cocos creator collision and collision callback do not take effect
Zhongang Mining: Fluorite continues to lead the growth of new energy market
JS array foreach source code parsing
Ida Pro reverse tool finds the IP and port of the socket server
[wechat applet] Chapter (5): basic API interface of wechat applet
Streaming end, server end, player end
Nacos一致性协议 CP/AP/JRaft/Distro协议
保证接口数据安全的10种方案
HW初级流量监控,到底该怎么做
C4D learning notes 3- animation - animation rendering process case
Steps to create P8 certificate and warehousing account
jacoco代码覆盖率
How to release NFT in batches in opensea (rinkeby test network)
Cut ffmpeg as needed, and use emscripten to compile and run
Tkinter after how to refresh data and cancel refreshing
The download button and debug button in keil are grayed out
L'application à l'échelle de la normalisation mature des produits ai des compagnies maritimes, cimc, leader mondial de l'intelligence artificielle portuaire et maritime / intelligence artificielle des
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)