0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【数値計算】最近使ったライブラリ※日々更新

Posted at

小数点の丸め方

数値を丸めたい

ある数値(int型)を小数第二位まで表示たい時

python
a = 3.2344567
print(a:.2f)

行列を丸めたい

ある行列(ndarray型)を小数第二位まで表示させたい時np.round(数値,表示させたい桁数)

python
a = np.array([1.2345, 2.3456, 3.4567])
print(np.round(a, 2))

全ての行列(ndarray型)を小数第二位まで表示させたい時

python
np.set_printoptions(precision=2) #この設定後に表示する全ての ndarray が小数点以下2桁になる
a = np.array([1.2345, 2.3456, 3.4567])
print(a)

よく使うnumpyのライブラリ

L2ノルム正規化np.linalg.norm(a)

python
a = np.array([3, 4, 5])
norm_l2 = np.linalg.norm(a)
print(a/norm)

L2ノルム正規化(手動でする場合)

python
a = np.array([3, 4, 5])
norm_l2 = np.sqrt(np.sum(x**2 for x in a))
for i, value in enumerate(a): #インデックス番号と数値を同時に取り出す
  a[i] = a[i] / norm_l2
a = np.round(a, 2) #小数第二位まで表示
print(a/norm)

L1ノルム正規化np.linalg.norm(a, ord=1)

python
a = np.array([3, 4, 5])
norm_l1 = np.linalg.norm(a, ord=1)
print(a/norm_l1)
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?