ためすう
numpy.savez を使ってみる
2019-09-16やったこと
numpy.savez を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'
調査
>>> a = np.arange(1, 10)
>>> a
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> np.savez('hoge.npz', x=a)
>>> f = np.load('./hoge.npz')
>>> f['x']
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
ファイルを作成して、内容を読み込むこともできました。
参考
os.listdir を使ってみる
2019-09-16やったこと
os.listdir を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
調査
準備
$ tree
.
├── aaa.txt
├── bbb.txt
├── ccc.txt
└── test.py
test.py
import os
i = os.listdir('./')
for target_file in i:
print(target_file)
実行
出力結果
$ python test.py
ccc.txt
bbb.txt
test.py
aaa.txt
numpy.random.normal を使ってみる
2019-09-16やったこと
numpy.random.normal を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'
調査
Draw random samples from a normal (Gaussian) distribution
上記によれば、ガウス分布でランダムな値が作られます。
任意の平均、標準偏差で生成することができます。
>>> np.random.normal(0, 1, (2, 3))
array([[ 0.37994701, -0.65873701, -0.60940363],
[-0.14211842, -1.40152253, -0.48017185]])
>>> np.random.normal(0, 1, (2, 3))
array([[ 0.80400563, 0.19123044, -0.75771854],
[ 1.07727344, 0.45275647, -2.22720158]])
>>> np.random.normal(0, 1, (2, 3))
array([[ 0.19913532, 0.70579006, -0.24233281],
[-0.42913376, -0.04703086, 0.62174294]])
>>> np.random.normal(0, 1, (2, 3))
array([[-0.92828659, -0.37335564, 0.41829356],
[ 0.99799035, -1.75828243, 1.1866021 ]])
参考
OpenCV で画像をリサイズする
2019-09-15やったこと
OpenCV ライブラリを使い、画像をリサイズしてみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
調査
opencv-img-resize.py
import cv2
print(cv2.__version__)
img = cv2.imread('sample.jpg')
width, height = 600, 400
img = cv2.resize(img, (width, height))
cv2.imwrite('sample-resize2.jpg', img)
出力結果
$ python opencv-img-resize.py
3.4.2
参考
OpenCV で画像を読み込んでみる
2019-09-13やったこと
OpenCV ライブラリを使い、画像を読み込んで表示してみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
調査
opencv-img.py
import cv2
print(cv2.__version__)
img = cv2.imread('sample.jpg')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
opencv-img2.py
import cv2
from matplotlib import pyplot as plt
print(cv2.__version__)
img = cv2.imread('sample.jpg')
rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(rgb)
plt.show()
参考
PIL で画像をリサイズする
2019-09-11やったこと
PIL ライブラリを使い、画像をリサイズしてみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
調査
バージョン確認
$ conda list Pillow
# packages in environment at /anaconda3:
#
pillow 6.1.0 py36hb68e598_0
pil-image-resize.py
from PIL import Image
img = Image.open('./sample.jpg')
width, height = 500, 600
img = img.resize((width,height))
img.save('./sample_resize.jpg')
参考
PIL で画像を読み込んでみる
2019-09-11やったこと
PIL ライブラリを使い、画像を読み込んで表示してみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
調査
Pillow をインストールする
$ conda install Pillow
バージョン確認
$ conda list Pillow
# packages in environment at /anaconda3:
#
pillow 6.1.0 py36hb68e598_0
pil-image-read.py
from PIL import Image
img = Image.open('./sample.jpg')
img.show()
参考
Python で読み込み対象のディレクトリを追加する
2019-09-10やったこと
読み込み対象のディレクトリを追加します。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
調査
ファイル構成
$ tree
mylib/
└── myclass.py
sys-path.py
ファイルの内容
myclass.py
class ddd:
def __init__(self):
pass
sys-path.py
import sys
sys.path.append('../')
### print(sys.path)
from mylib.myclass import ddd
print(ddd())
ファイル実行
$ python sys-path.py
<mylib.myclass.ddd object at 0x104e8a978>
参考
np.argmax を使ってみる
2019-09-10やったこと
np.argmax を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'
調査
>>> b = [0, 5, 2, 3, 4, 5]
>>> argmax(b)
>>> np.argmax(b)
1
>>> a = np.random.random(20)
>>> a = a.reshape(4, 5)
>>> a
array([[0.49340889, 0.29507221, 0.56335119, 0.83046262, 0.7579564 ],
[0.10089851, 0.43410885, 0.5479143 , 0.55304626, 0.92677723],
[0.21139963, 0.06923998, 0.15509698, 0.33185919, 0.70188447],
[0.58790613, 0.74386543, 0.25404441, 0.67682735, 0.45157852]])
>>> np.argmax(a)
9
>>> np.argmin(a)
11
>>> np.argmax(a, axis=0)
array([3, 3, 0, 0, 1])
>>> np.argmax(a, axis=1)
array([3, 4, 4, 1])
axis を指定しない場合、flatな配列としてみたときの indexが返されます。
また、axis=0
で列ごと、axis=1
で行ごとに見ます。
参考
numpy.random.randn を使ってみる
2019-09-09やったこと
numpy.random.randn を使ってみます。
確認環境
$ python
Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.16.4'
調査
numpy.random.randn は、標準正規分布を返すとあります。
Return a sample (or samples) from the “standard normal” distribution
標準正規分布とは、平均0、標準偏差1の正規分布のことです。
>>> np.random.rand(2,3)
array([[0.60826951, 0.10515517, 0.69150681],
[0.40838286, 0.34473871, 0.09018369]])
>>> np.random.rand(2,3)
array([[0.31227934, 0.93830731, 0.42201233],
[0.54689324, 0.99169398, 0.89075624]])
>>> np.random.rand(2,3)
array([[0.01817165, 0.78406566, 0.6051451 ],
[0.43414395, 0.94710879, 0.75136773]])
>>> np.random.rand(2,3)
array([[0.22214423, 0.68967167, 0.25798399],
[0.17294866, 0.62487288, 0.46928062]])