AI E資格

ラビットチャレンジでE資格に挑戦 50日目

とうとうラビットチャレンジを初めてから50日が経過。
まだスタートテストにも合格できてないので、
スタートラインにも立ててないけど、
一旦忘れて、

今日も AIを学ぶ為の本格Python講座 を進める。

■AIを学ぶ為の本格Python講座
PY15_Numpy_行列の演算

 

今日学んだこと

Numpyについて

>>> test1=np.arange(5)
>>> test1
array([0, 1, 2, 3, 4])

足し算

>>> test1+5
array([5, 6, 7, 8, 9])
>>>

引き算

>>> test1-5
array([-5, -4, -3, -2, -1])
>>>

掛け算

>>> test1*2
array([0, 2, 4, 6, 8])
>>>

割り算

>>> test1/2
array([0. , 0.5, 1. , 1.5, 2. ])
>>>

商を整数にする(1未満切り捨て)

>>> test1//2
array([0, 0, 1, 1, 2], dtype=int32)
>>>

べき乗を求める

>>> test1**3
array([ 0, 1, 8, 27, 64], dtype=int32)
>>>
>>>

余りを求める

>>> test1%2
array([0, 1, 0, 1, 0], dtype=int32)
>>>
>>>

計算の組み合わせも可能

>>> -(50*test1+2)**2
array([ -4, -2704, -10404, -23104, -40804], dtype=int32)
>>>

配列の操作

>>> test2=np.random.random(100)
>>> test2
array([0.34801434, 0.24478291, 0.27843152, 0.42472962, 0.81449947,
0.00641039, 0.12280302, 0.05967229, 0.94076182, 0.96089228,
0.76472879, 0.36794931, 0.36538629, 0.67371578, 0.17991318,
0.39328145, 0.47898269, 0.72084507, 0.48744541, 0.78826473,
0.11362156, 0.01700687, 0.66793198, 0.2989936 , 0.16648708,
0.53762054, 0.80583101, 0.44016396, 0.30956813, 0.61679013,
0.00935255, 0.38570892, 0.81322519, 0.3275126 , 0.67008075,
0.34613238, 0.861909 , 0.41982485, 0.96818391, 0.66128984,
0.56124849, 0.95435743, 0.85131971, 0.77135613, 0.09756528,
0.65335301, 0.4469727 , 0.89795431, 0.60652439, 0.09895936,
0.0369377 , 0.79371923, 0.10950649, 0.08730174, 0.72677777,
0.36154285, 0.2049284 , 0.61229351, 0.66673767, 0.52936542,
0.64114044, 0.78344771, 0.31089091, 0.78544921, 0.42416745,
0.99060269, 0.51831617, 0.44762266, 0.51393024, 0.7115072 ,
0.53221898, 0.30471701, 0.74011535, 0.97756652, 0.6196035 ,
0.31038364, 0.18471014, 0.89353983, 0.1501843 , 0.94277425,
0.64569761, 0.02789671, 0.31439393, 0.37282586, 0.23181439,
0.65444955, 0.1185355 , 0.50160081, 0.28370427, 0.52384424,
0.12689333, 0.24688256, 0.34591544, 0.73073037, 0.24649296,
0.05817323, 0.61922893, 0.92396905, 0.77696598, 0.3573507 ])
>>>

合計

>>> np.sum(test2)
48.8177204449352
>>>

この書き方でもOK

>>> test2.sum()
48.8177204449352
>>>

最大

>>> np.max(test2)
0.9906026931432531
>>>

この書き方でもOK
>>> test2.max()
0.9906026931432531
>>>

最小

>>> np.min(test2)
0.0064103906559475066
>>>

この書き方でもOK
>>> test2.min()
0.0064103906559475066
>>>

多次元配列

>>> test3=np.random.random((3,4))
>>>
>>> test3
array([[0.22169504, 0.79208817, 0.29877169, 0.45570854],
[0.28195111, 0.78850201, 0.92057156, 0.83777555],
[0.40938369, 0.15164751, 0.46612088, 0.94717547]])
>>>

合計

>>> test3.sum()
6.571391220428055

最小値

>>> test3.min()
0.1516475096751777
>>>

最大値

>>> test3.max()
0.9471754659789017
>>>

3次元

>>> test3=np.random.random((3,4))
>>>
>>> test3
array([[0.22169504, 0.79208817, 0.29877169, 0.45570854],
[0.28195111, 0.78850201, 0.92057156, 0.83777555],
[0.40938369, 0.15164751, 0.46612088, 0.94717547]])
>>>

合計
>>> test3.sum()
6.571391220428055

>>> test3.min()
0.1516475096751777

>>> test3.max()
0.9471754659789017
>>>

階層を指定する
>>> test3.min(axis=1)
array([0.22169504, 0.28195111, 0.15164751])
>>>
>>>
>>> test3.max(axis=1)
array([0.79208817, 0.92057156, 0.94717547])
>>>

平均値
>>> np.mean(test3)
0.5476159350356712

>>>

中央値
>>> np.median(test3)
0.4609147096293203
>>>

マスク

>>> test4=np.random.randint(5,size=(40))
>>>
>>> test4
array([2, 0, 1, 1, 0, 0, 3, 1, 2, 3, 4, 2, 1, 0, 0, 0, 0, 2, 1, 3, 0, 4,
3, 1, 0, 4, 0, 4, 1, 0, 0, 2, 2, 3, 3, 1, 4, 1, 4, 1])
>>>

各要素で0より大きければTrue、小さいとFalseと表示される。
>>> test4>0
array([ True, False, True, True, False, False, True, True, True,
True, True, True, True, False, False, False, False, True,
True, True, False, True, True, True, False, True, False,
True, True, False, False, True, True, True, True, True,
True, True, True, True])
>>>

抜き出すことも可能
>>> test4
array([2, 0, 1, 1, 0, 0, 3, 1, 2, 3, 4, 2, 1, 0, 0, 0, 0, 2, 1, 3, 0, 4,
3, 1, 0, 4, 0, 4, 1, 0, 0, 2, 2, 3, 3, 1, 4, 1, 4, 1])
>>>
>>> test4_sub=test4[test4>1]
>>>
要素が1より大きい値のものだけ抜き出す。
test4_sub=test4[test4>1]
>>> test4_sub
array([2, 3, 2, 3, 4, 2, 2, 3, 4, 3, 4, 4, 2, 2, 3, 3, 4, 4])
>>

 

勉強時間

今日: 0.5時間

総勉強時間: 32.5時間

 

お得キャンペーンの紹介

ラビットチャレンジの
Amazonギフト券「5,000円」プレゼントキャンペーンのお知らせ

申込時に、以下コードを使えば、入会金が5000円引かれるようです。
また、紹介した僕にも5000円のAmazonギフト券が送られるようです。
お得なので、よかったら申込時お使いください。

紹介コード:friend0019697

※本キャンペーンの期間は、 2021年9月15日~10月31日 です。

「お友達紹介キャンペーン」特設ページ:
https://ai99

-AI, E資格