Pythonツールキット-気の利いた小さな演習

記事のディレクトリ

import numpy as np

1.現在のNumpyバージョンを印刷します

print(np.__version__)
1.18.1

2.すべてゼロの行列を作成し、それが占めるメモリサイズを出力します

Clichong = np.zeros((5,5))
Clichong
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
# 数组的数量
Clichong.size
25
# 数组每个元素的大小
Clichong.itemsize
8
# 数组的类型
Clichong.dtype
dtype('float64')
# 可以知道Clichong这个二维变量的大小
print("size:",Clichong.size*Clichong.itemsize)
size: 200

3.numpy.addなどの関数のヘルプドキュメントを印刷します

print(help(np.add))

4. 10-49配列を作成し、逆の順序で配置します

# arange(10,50,1)是numpy语句部分,后面的[::-1]是python语句部分,所以可以使用一句就可以实现倒序排序
Clichong = np.arange(10,50,1)[::-1]
Clichong
[49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26
 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10]





array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,
       32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
       15, 14, 13, 12, 11, 10])

5.0ではない配列内のインデックスを検索します

# 测试数组
test = np.array([2,3,5,7,0,2,0,4,2,6,0,5,26,7,0,6,0,0])
print("test.size:",test.size)
test.size: 18
# 方法1:利用where语句
a = np.where(test != 0)
print("position:",a)
position: (array([ 0,  1,  2,  3,  5,  7,  8,  9, 11, 12, 13, 15], dtype=int64),)
# 方法2:利用nonzero函数
a = np.nonzero(test)
print("position:",a)
position: (array([ 0,  1,  2,  3,  5,  7,  8,  9, 11, 12, 13, 15], dtype=int64),)

6. 3 * 3行列をランダムに作成し、最大値と最小値を出力します

# randint创建一个取值为0-100,3x3的随机矩阵
Clichong = np.random.randint(100,size = (3,3))
print("Clichong:\n",Clichong)
# 全部元素进行对比
print("max:",Clichong.max())
print("min:",Clichong.min())
# 对列进行对比
print("max:",Clichong.max(axis = 0))
# 对行进行对比
print("min:",Clichong.min(axis = 1))
Clichong:
 [[95 15 84]
 [55 22 28]
 [ 2 29 91]]
max: 95
min: 2
max: [95 29 91]
min: [15 22  2]

7. 5 * 5行列を作成し、その値をすべて1にして、最外層に0の円を追加します

#利用numpy的pad函数,其相关参数API文档如下,可以print(help(np.pad))来打印出来查看
Clichong = np.ones((5,5))
Clichong = np.pad(Clichong,1,mode='constant',constant_values = 0)
Clichong
#print(help(np.pad))
array([[0., 0., 0., 0., 0., 0., 0.],
       [0., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 0.],
       [0., 1., 1., 1., 1., 1., 0.],
       [0., 0., 0., 0., 0., 0., 0.]])

8.形状(6、7、8)の行列を作成し、100番目の要素のインデックス値を見つけます

# 使用unravel_index函数来找到索引值,print(help(np.unravel_index))
Clichong = np.random.randint(10,size = (6,7,8))
print(Clichong.shape)
# 100是索引值,(6,7,8)是矩阵的形状,只能写出来,不能用Clichong替代,具体参考文档
np.unravel_index(100,(6,7,8))
# print(help(np.unravel_index))
(6, 7, 8)





(1, 5, 4)

9. 5 * 5行列を正規化します

正規化の定義と機能:正規化とは、(特定のアルゴリズムを介して)処理する必要のあるデータを必要な特定の範囲に制限することです。まず第一に、正規化はその後のデータ処理の便宜のためです。

2つ目は、モデルの実行時に収束を高速化することです。

# Clichong = np.random.randint(10,size = (5,5))
Clichong = np.random.random((5,5))
print("before:\n",Clichong)
max = Clichong.max()
min = Clichong.min()
Clichong = (Clichong-min)/(max-min)
print("After:\n",Clichong)
before:
 [[0.42707272 0.81047186 0.91071421 0.23840794 0.3771108 ]
 [0.90182748 0.90802103 0.61158242 0.00489321 0.21548403]
 [0.64966355 0.65816033 0.04054501 0.00760435 0.44263836]
 [0.84498916 0.47003901 0.77073106 0.38147171 0.50009968]
 [0.65061004 0.79489827 0.84465063 0.94343758 0.4159035 ]]
After:
 [[0.4498237  0.85832772 0.96513392 0.24880521 0.3965903 ]
 [0.95566528 0.96226438 0.64641505 0.         0.22438025]
 [0.68698972 0.69604287 0.03798627 0.00288866 0.46640859]
 [0.89510521 0.49560342 0.81598471 0.40123676 0.52763245]
 [0.68799819 0.84173438 0.89474451 1.         0.43792313]]

10.2つの配列で同じ値または同じ値の位置を見つけます

 # 定义两个随机数组取值为0-10,数目为30个
a = np.random.randint(0,20,10)
b = np.random.randint(0,20,10)
print("a:",a)
print("b:",b)
a: [18  1 10 15  6  8 16 10 12 14]
b: [ 5 12  5  0 19  5 18 15  0  0]
# 找出两个数组相同值的位置
np.where(a == b)
(array([], dtype=int64),)
# 找出两个数组中共有的值,利用intersect1d函数
np.intersect1d(a,b)
array([12, 15, 18])

11.今日、明日、昨日の日付を取得します

# 利用datetime64函数 print(help(np.datetime64))
today = np.datetime64('today','D')
yesterday = np.datetime64('today','D') - np.timedelta64(1,'D')
tomorrow = np.datetime64('today','D') + np.timedelta64(1,'D')
print("today:",today)
print("yesterday:",yesterday)
print("tomorrow:",tomorrow)
today: 2021-01-20
yesterday: 2021-01-19
tomorrow: 2021-01-21

12.数値の整数部分を取得します

# 定义一个小数数组,范围是0-10,个数为10个
z = np.random.uniform(0,10,10)
print(z)
# 去除小数部分的函数floor
np.floor(z)
[9.78465077 2.07504483 8.34613494 2.81205194 2.97713123 0.26551266
 6.33476669 6.13764817 5.38474901 0.57251498]





array([9., 2., 8., 2., 2., 0., 6., 6., 5., 0.])

13.変更できないように配列を作成します

Clichong = np.array([1,2,3,4,5,6,7,8,9,0])
# 关闭可改标志
Clichong.flags.writeable = False
Clichong[1] = 0
---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-59-7fa6a686b7d3> in <module>
      2 # 关闭可改标志
      3 Clichong.flags.writeable = False
----> 4 Clichong[1] = 0


ValueError: assignment destination is read-only

14.ビッグデータのすべての値を印刷します(すべてを印刷する必要はありません)

# 通过set_printoptions函数来设置,都只是显示6行6列
# np.set_printoptions(threshold=10000) # 这个参数填的是你想要多少行显示
# np.set_printoptions(linewidth=100) # 这个参数填的是横向多宽

np.set_printoptions(threshold=20)
z = np.zeros((15,15))
z
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]])

15.配列内の数値に最も近いインデックスを見つけます

# 函数argmin返回沿轴的最小值的索引。如果多次出现最小值,则索引对应于第一次出现的返回。
a = np.random.uniform(0,10)
b = np.arange(10)
print("a:",a)
print("b:",b)
# abs()的操作的去绝对值的操作,所以一个很小的负数取绝对值会变成一个很大的数
print((np.abs(b-a)))
print("最接近的索引是:",(np.abs(b-a)).argmin())
#(np.abs(b-a)).argmin()
#print(help(np.argmin))
a: 6.430836025704295
b: [0 1 2 3 4 5 6 7 8 9]
[6.43083603 5.43083603 4.43083603 3.43083603 2.43083603 1.43083603
 0.43083603 0.56916397 1.56916397 2.56916397]
最接近的索引是: 6

16.32ビットfloat型および32ビットint型変換

Clichong = np.array([1,2,3,4,5,6,7,8,9],dtype = np.float32)
print("before_type",Clichong.dtype)
Clichong = Clichong.astype(np.int32)
print("After_type",Clichong.dtype)
before_type float32
After_type int32

17.配列要素の位置座標と値を印刷します

# ndenumerate函数与python中的enumerate类似,reshape可以重新改变数组的结构
Clichong = np.arange(9).reshape(3,3)
for index,value in np.ndenumerate(Clichong):
    print (index,value)
print(np.ndenumerate(Clichong)
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
<numpy.ndenumerate object at 0x00000270CE55DA88>

18.配列の特定の列に従って並べ替えます(より抽象的な場合は、数回確認する必要があります)

# 按照第二列升序
Clichong = np.array([[1,2,4],[3,5,2],[4,3,1],[2,4,0]])
print(Clichong)
[[1 2 4]
 [3 5 2]
 [4 3 1]
 [2 4 0]]
# 方法:lexsort函数
# 其中的 1 表示升序; 若是是 -1 则表示降序
index = np.lexsort([1*Clichong[:,1]])
print("index:",index)
Clichong[index]
index: [0 2 3 1]





array([[1, 2, 4],
       [4, 3, 1],
       [2, 4, 0],
       [3, 5, 2]])
# 方法2:argsort函数
# z[z[:,1].argsort()]
print("index:",Clichong[:,1].argsort())
Clichong[Clichong[:,1].argsort()]
index: [0 2 3 1]





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

19.配列内の各値の出現回数をカウントします

Pythonでは、統計値の出現回数は直接関数によって完成され、複雑な関数を自分で作成する必要はありません。

Clichong = np.array([1,0,1,0,1,2,0,2,3,3,4,5,8])
print(np.bincount(Clichong))
[3 3 2 2 1 1 0 0 1]

20.配列内で最も頻繁に発生する番号を見つけます

Clichong = np.array([1,0,6,6,1,0,1,6,2,6,0,2,3,0,6,3,4,6,0,5,8])
# 统计数组中每个数值出现的次数
Clichong = np.bincount(Clichong)
print(Clichong)
# 返回最多出现数字的下标,也就打印出最常出现的数字
np.argmax(Clichong)
[5 3 2 2 1 1 6 0 1]





6

21.スイッチングマトリックスの2行

Clichong = np.random.randint(10,size = (5,5))
print("Clichong_before:\n",Clichong)

Clichong[[0,1]] = Clichong[[1,0]]
print("Clichong_after:\n",Clichong)
Clichong_before:
 [[1 9 5 5 5]
 [0 5 8 2 9]
 [7 0 3 8 0]
 [3 5 0 7 3]
 [1 5 3 4 3]]
Clichong_after:
 [[0 5 8 2 9]
 [1 9 5 5 5]
 [7 0 3 8 0]
 [3 5 0 7 3]
 [1 5 3 4 3]]

おすすめ

転載: blog.csdn.net/weixin_44751294/article/details/112911081