当前位置:网站首页>Numpy basic learning
Numpy basic learning
2022-07-29 04:47:00 【__ Meursault__】
Basic operation
a = np.array([1,2,3,4]).reshape((2,2))
b = np.arange(4).reshape((2,2))
c = a-b
d = a*b
# Multiply the corresponding number of the matrix
e1 = np.dot(a, b)
e2 = a.dot(b)
# Matrix multiplication
print(a<3)
# You can perform logical operations on matrix elements Return ture or flose
a = np.random.random((2,4))
# np in random And normal random similar If random.random((2,4)) production 2 That's ok 4 Column ,0~1 Between random numbers
# If the data is already array Some can be written directly A.sum() and np.sum(A) equally
a_sum = np.sum(a)
a_min = np.min(a)
a_max = np.max(a)
# You can just do it Or column The biggest and the smallest Sum up
a_max_c = np.max(a,axis=1)
a_max_r = np.max(a,axis=0)
# axis=1 Return the maximum value of each line Return a list
# axis=0 Return the maximum value of each column Return a list
print(np.argmax(a))
# a Index value of the maximum value in also argmix
print(np.mean(a))
# a The average of
print(np.average(a))
# average Average
print(np.median(a))
# median Median
print(np.cumsum(a))
# a The accumulation function of result [ 2 5 9 14 20 27 35 44 54 65]
print(np.diff(a))
# Cumulative difference function The difference between the latter and the former in each line
print(np.sort(a))
# and sort Function similar to Make an arrangement
print(np.transpose(a))
print(a.T)
# Both of the above methods are right a To transpose
print(np.clip(a,5,10))
# clip( trim ) clip(Array,Array_min,Array_max) about a Small and medium Array_min Replace it with Array_min,Array_max For more than replacement
array Lookup
a = np.arange(3,15).reshape((3,4))
print(a,'\n')
print(a[2])
# For the first of the matrix indexes [] That's the line , The above result is a The second line of
print(a[1][1])
print(a[1,1])
# Specific to a certain location Above results 2 That's ok 2 Number of columns The above two expressions are the same
print(a[1,1:4])
# It's equivalent to slicing [1:,2] Second elements 2~5 Column
print("---------")
for row in a:
print('\n',row)
# for The loop will print line by line There is no direct way to print column by column It can be done to a Transpose it column by column as follows
print("---------")
for col in a.T:
print('\n',col)
# If you want to iterate over all the elements of the matrix , You need to turn it into one line , Use a.flat( This is an iterator, not array, If you want to see array want a.flatten())
print("---------")
print(a.flatten())
for item in a.flat:
print(item)
array Assignment
# assignment copy
a = [1,2,3,4,5]
b = a
c = b
print(b)
print(c)
# If you change a The value of or b Value all abc Will change Because if directly a = b amount to a And b relation
a[1] = 10
print(a)
print(b)
# For those who only want to assign the same value without Association need b = a.copy()
q = a.copy()
print('q', q)
a[2] = 9
print(b) # [1, 10, 9, 4, 5]
print(q) # [1, 10, 3, 4, 5]
array Merge
# Merge the matrix hstack vstack
a = np.array([1,2,3])
b = np.array([11,22,33])
print(np.vstack((a,b))) # vertical( vertical ) stack
print(np.hstack((a,b))) # horizontal( level ) stack
print("---------")
# For a row vector , If you use the previous transpose method (.T or transpose) Cannot transpose , Because it is not a array
# Notice that it's ()
print(a.T) # Still [1 2 3]
# Method np.newaxis()
print("---------")
print(a[:,np.newaxis])
# Just add it directly after the vector to be transposed , however newaxis() Cannot act on matrix
# Multiple array Connect You can still use vstack perhaps hstack You can also use concatenate((x,y)) Can pass axis Choose the direction of the merge
# The following two effects are the same
print(np.vstack((a,b,a)),"\n")
print(np.concatenate((a,b,a)))
array Segmentation
a = np.arange(12).reshape((3,4))
print(a)
print('\n',np.split(a,2,axis=1)) # If you don't write axis The default is axis=0( The matrix is partitioned up and down )
# Sometimes I want to divide by different amounts For example, for the following matrix, you want to divide it vertically into three parts Sure arrary_split
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print('\n',np.array_split(a,3,axis=1))
# But I don't know how to control the desired results , You still need to learn
# You can also use vsplit( Vertical segmentation ) and hsplit It's basically useless , The top is enough
print(np.hsplit(a,3)) # be equal to print(np.split(a,2,axis=1))
Add
# np.where Is a vectorized version of ternary expressions , and where second , The three parameters need not be arrays , It can be scalar
a = np.array([1,2,3,4])
b = np.array([6,7,8,9])
c = np.array([True, False, True, False])
result = np.where(c,a,b) # [1, 7, 3, 9]
# If you want to replace some numbers in a set of numbers with scalars
a = np.random.randn(4,4)
a1 = np.where(a > 0, "a", 3) # take a Greater than 0 For a Replace less than with 3
a2 = np.where(a < 0, 2, a) # Only speak a Small and medium 0 Replacement
# a1 [['3' '3' '3' 'a']
['a' '3' 'a' 'a']
['a' '3' 'a' 'a']
['3' 'a' 'a' 'a']]
# a2 [[2. 2. 2. 0.25344902]
[2.27838389 2. 1.44099603 1.32540036]
[0.16570196 2. 1.34380092 2.20212735]
[2. 1.15632934 2.28351864 1.68879952]]
边栏推荐
- The daily life of programmers
- GCC Basics
- [C language] power table of 3 generated by PTA 7-53
- What are the core features of the digital transformation of state-owned construction enterprises?
- Christmas tree web page and Christmas tree application
- C语言之基础语法
- Oracle 插入数据
- On quotation
- Mysql:The user specified as a definer (‘root‘@‘%‘) does not exist 的解决办法
- Post export data, return
猜你喜欢

Reveal installation configuration debugging

2022杭电多校联赛第四场 题解

MySQL - 聚簇索引和辅助索引

There are objections and puzzles about joinpoint in afterreturning notice (I hope someone will leave a message)

Make a virtual human with zego avatar | virtual anchor live broadcast solution

Record the Niua packaging deployment project

Mujoco and mujoco_ Install libxcursor.so 1:NO such dictionary

正确的用户拖拽方式

Vscode one click compilation and debugging

Flutter实战-请求封装(二)之dio
随机推荐
Configure st-gcn environment record [Google lab]
[QT learning notes] * insert pictures in the window
Build auto.js script development environment
Dasctf2022.07 empowerment competition
[c language] PTA 7-63 falling ball
EF Core: 一对一,多对多的配置
After the spinning up installation is completed, use the tutorial to test whether it is successful. There are library "Glu" not found and 'from pyglet.gl import * error solutions
TypeError: Cannot read properties of undefined (reading ‘then‘)
EMI interference troubleshooting with near-field probe and current probe
Christmas tree web page and Christmas tree application
I++ and ++i details
Command line interactive tools (latest version) inquirer practical tutorial
GCC基础知识
i++与++i详解
C语言之基础语法
Log configuration logback
Oracle insert data
Reveal installation configuration debugging
Vscode configuration makefile compilation
Post export data, return