当前位置:网站首页>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]]
边栏推荐
- ios面试准备 - 网络篇
- Post export data, return
- 使用近场探头和电流探头进行EMI干扰排查
- iOS面试准备 - ios篇
- Correct user dragging method
- IOS interview preparation - Online
- File operation (Advanced C language)
- (heap sort) heap sort is super detailed, I don't believe you can't (C language code implementation)
- ssm整合增删改查
- STL source code analysis (Hou Jie) notes - STL overview
猜你喜欢

Install the gym corresponding to mujoco in the spinning up tutorial, and the error mjpro150 is reported

Laya中的A星寻路

Idea small settings

VScode 一键编译和调试

Detailed comparison of break and continue functions

DASCTF2022.07赋能赛

Dasctf2022.07 empowerment competition

Reveal安装配置调试

JVM (heap and stack) memory allocation

Auto.js脚本开发环境搭建
随机推荐
Oracle insert data
[C language] PTA 7-47 binary leading zero
IOS interview preparation - Online
ios面试准备 - 网络篇
JVM (heap and stack) memory allocation
Recyclerview switches the focus up and down through the dpad key. When switching to the control outside the interface, the focus will jump left and right
Build auto.js script development environment
Unity基础(3)—— unity中的各种坐标系
Star a pathfinding in LAYA
I++ and ++i details
[c language] PTA 7-49 have fun with numbers (partially correct)
Pyqt5 learning pit encounter and pit drainage (2) buttons in qformlayout layout cannot be displayed
[c language] PTA 7-63 falling ball
IOS interview preparation - other articles
leetcode 763. Partition Labels 划分字母区间(中等)
Various configurations when pulsar starts the client (client, producer, consumer)
钉钉对话框文子转换成图片 不能复制粘贴到文档上
(heap sort) heap sort is super detailed, I don't believe you can't (C language code implementation)
在线教育的推荐系统
Makefile(make)常见规则(二)