LoginSignup
4
2

More than 5 years have passed since last update.

[ 作成中]Numpy使い方メモ

Last updated at Posted at 2018-10-18

はじめに

KaggleのGStoreコンペのKernelで使用されていたNumpyについて、使い方を備忘録として記載します。

Numpy Basics Cheat Sheet

Numpyの基本的な使い方は以下の画像を参考にしてください。
numpy_cheat_sheet.png

[引用]https://becominghuman.ai/cheat-sheets-for-ai-neural-networks-machine-learning-deep-learning-big-data-678c51b4b463

平方根を求める

sqrt

Python
>>> import numpy as np
>>> np.sqrt(7) # 7**0.5と同等
2.6457513110645907

データ型指定

int64 64ビットの符号付き整数

expm1

expm1(x) は e の x 乗から 1 を引いた値を返します。
非常に小さな x で exp(x) - 1 を計算すると、減算により桁落ちしますが、expm1関数は精度を保ちます。

下のサンプルコードでは x = 1.0e-5 として、exp(x) - 1 と numpy.expm1(x) を計算させています。
※以下のような単純な計算では、Pythonに標準搭載されているMath関数の方が処理時間は速いです。

Python
import numpy as np
a = np.exp(1.0e-5)-1
b = np.expm1(1.0e-5)

print("a =", a)
print("b =", b)
a = 1.0000050000069649e-05
b = 1.0000050000166667e-05

log1p

expm1の逆数であるlog(1 + x)
1を加えた底eの自然対数

4
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2