日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

pq 中m函数判断嵌套_Python中numpy的布尔判断、切片、维度变化、合并、通用函数...

發布時間:2024/10/8 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pq 中m函数判断嵌套_Python中numpy的布尔判断、切片、维度变化、合并、通用函数... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關注微xin公共hao:小程在線

import numpy as np

###################################數據的布爾值判斷###發財中國年##########

x=np.array([1,2,3,4,5])

x

Out[1]: array([1, 2, 3, 4, 5])

#判斷X是否小于2

x<2

Out[2]: array([ True, False, False, False, False])

#判斷X小于2或者大于4

(x<2) | (x>4)

Out[4]: array([ True, False, False, False, True])

mask=x<3

mask

Out[5]: array([ True, True, False, False, False])

x[mask]

Out[6]: array([1, 2])

np.sum(x<3)

Out[7]: 2

y=np.array([[1,2,3,4,5],[6,7,8,9,10]])

y

Out[8]:

array([[ 1, 2, 3, 4, 5],

[ 6, 7, 8, 9, 10]])

x == y[0,]

Out[9]: array([ True, True, True, True, True])

#兩個數組比較

a1 = np.arange(9).reshape(3, 3)

a2 = np.arange(9, 0 , -1).reshape(3, 3)

a1 < a2

Out[10]:

array([[ True, True, True],

[ True, True, False],

[False, False, False]])

###########數據的切片選擇(開始位,結束位,步長)############百萬英雄########

#位置值從0開始的

x=np.arange(0,10)

x[4:6]

Out[11]: array([4, 5])

#從0開始,步長為2的數據

x[::2]

Out[12]: array([0, 2, 4, 6, 8])

#倒序

x[::-1]

Out[13]: array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])

#取前6個數據

x[:6]

Out[14]: array([0, 1, 2, 3, 4, 5])

#從第2的位置開始選

x[2:]

Out[15]: array([2, 3, 4, 5, 6, 7, 8, 9])

#二維數據的切片處理

y=np.arange(0,16).reshape(4,4)

y

Out[16]:

array([[ 0, 1, 2, 3],

[ 4, 5, 6, 7],

[ 8, 9, 10, 11],

[12, 13, 14, 15]])

y[:,1:3]

Out[17]:

array([[ 1, 2],

[ 5, 6],

[ 9, 10],

[13, 14]])

y[1:3,2:4]

Out[18]:

array([[ 6, 7],

[10, 11]])

y[[1,3],:]

Out[19]:

array([[ 4, 5, 6, 7],

[12, 13, 14, 15]])

######################數據的維度變化#########發財中國年###########

x=np.arange(0,9)

y=x.reshape(3,3)

y

Out[20]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

x=np.arange(0,9)

y=x.reshape(3,3)

y

Out[21]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

#返回一個新的數組,不是修改原來的數組

y.reshape(9)

Out[22]: array([0, 1, 2, 3, 4, 5, 6, 7, 8])

y

Out[23]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

reshaped = y.reshape(np.size(y))

raveled = y.ravel() #將數據展開成一維的

reshaped[2] = 1000

raveled[5] = 2000

y

Out[24]:

array([[ 0, 1, 1000],

[ 3, 4, 2000],

[ 6, 7, 8]])

y = np.arange(0, 9).reshape(3,3)

flattened = y.flatten() #將數據展開成一維的

flattened[0] = 1000

flattened

Out[25]: array([1000, 1, 2, 3, 4, 5, 6, 7, 8])

flattened.shape = (3, 3)

flattened

Out[26]:

array([[1000, 1, 2],

[ 3, 4, 5],

[ 6, 7, 8]])

#數據的轉置

flattened.T

Out[27]:

array([[1000, 3, 6],

[ 1, 4, 7],

[ 2, 5, 8]])

############################數據的合并處理#########

a = np.arange(9).reshape(3, 3)

b = (a + 1) * 10

a

Out[28]:

array([[0, 1, 2],

[3, 4, 5],

[6, 7, 8]])

b

Out[29]:

array([[10, 20, 30],

[40, 50, 60],

[70, 80, 90]])

#水平方向

np.hstack((a, b))

Out[30]:

array([[ 0, 1, 2, 10, 20, 30],

[ 3, 4, 5, 40, 50, 60],

[ 6, 7, 8, 70, 80, 90]])

#垂直方向

np.vstack((a, b))

Out[31]:

array([[ 0, 1, 2],

[ 3, 4, 5],

[ 6, 7, 8],

[10, 20, 30],

[40, 50, 60],

[70, 80, 90]])

#axis = 1橫軸方向,axis = 0 豎軸方向

np.concatenate((a, b), axis = 1)

Out[32]:

array([[ 0, 1, 2, 10, 20, 30],

[ 3, 4, 5, 40, 50, 60],

[ 6, 7, 8, 70, 80, 90]])

#每列相互拼接

np.dstack((a, b))

Out[33]:

array([[[ 0, 10],

[ 1, 20],

[ 2, 30]],

[[ 3, 40],

[ 4, 50],

[ 5, 60]],

[[ 6, 70],

[ 7, 80],

[ 8, 90]]])

#兩列拼接

one_d_a = np.arange(5)

one_d_b = (one_d_a + 1) * 10

np.column_stack((one_d_a, one_d_b))

Out[36]:

array([[ 0, 10],

[ 1, 20],

[ 2, 30],

[ 3, 40],

[ 4, 50]])

##行疊加

np.row_stack((one_d_a, one_d_b))

Out[37]:

array([[ 0, 1, 2, 3, 4],

[10, 20, 30, 40, 50]])

##########################通用函數###百萬英雄出題官#

m = np.arange(10, 19).reshape(3, 3)

print (m)

print ("{0} min of the entire matrix".format(m.min()))

print ("{0} max of entire matrix".format(m.max()))

##最小值、最大值的位置

print ("{0} position of the min value".format(m.argmin()))

print ("{0} position of the max value".format(m.argmax()))

#每列、每行的最小值

print ("{0} mins down each column".format(m.min(axis = 0)))

print ("{0} mins across each row".format(m.min(axis = 1)))

#每列、每行的最大值

print ("{0} maxs down each column".format(m.max(axis = 0)))

print ("{0} maxs across each row".format(m.max(axis = 1)))

[[10 11 12]

[13 14 15]

[16 17 18]]

10 min of the entire matrix

18 max of entire matrix

0 position of the min value

8 position of the max value

[10 11 12] mins down each column

[10 13 16] mins across each row

[16 17 18] maxs down each column

[12 15 18] maxs across each row

#平均值,標準差,方差

a

Out[41]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])

a = np.arange(1,10)

a.mean(), a.std(), a.var()

Out[39]: (5.0, 2.581988897471611, 6.666666666666667)

#求和,乘積

a.sum(), a.prod()

Out[40]: (45, 362880)

#累加和,累加乘

a.cumsum(), a.cumprod()

Out[42]:

(array([ 1, 3, 6, 10, 15, 21, 28, 36, 45], dtype=int32),

array([ 1, 2, 6, 24, 120, 720, 5040, 40320,

362880], dtype=int32))

總結

以上是生活随笔為你收集整理的pq 中m函数判断嵌套_Python中numpy的布尔判断、切片、维度变化、合并、通用函数...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。