0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python備忘録

Posted at

この記事は, python初心者の学習の記録です.
自分のわからなかったところを主に書いてます.
なので順序はごちゃごちゃです. 参考までにどうぞ,,,

#NumPy
##np.array

# -*- coding: utf-8 -*-
import numpy as np

# 2次元配列の宣言・初期化
A = np.array([[1, 2],
              [3, 4],
              [5, 6]])

# 行列の大きさ
print("行列Aの大きさ:", A.shape)
print("行列Aの行数:", A.shape[0])
print("行列Aの列数:", A.shape[1])

"""
行列Aの大きさ:(3, 2)
行列Aの行数:3
行列Aの列数:2
"""

##np.reshape
shape[0]は行数表示, shape[1]は列数表示.

import numpy as np

a = np.arange(24)

print(a)
# [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]

print(a.shape)
# (24,)

print(a.ndim)
# 1

a_4_6 = a.reshape([4, 6])

print(a_4_6)
# [[ 0  1  2  3  4  5]
#  [ 6  7  8  9 10 11]
#  [12 13 14 15 16 17]
#  [18 19 20 21 22 23]]

print(a_4_6.shape)
# (4, 6)

print(a_4_6.ndim)
# 2

np.ndimでは, 簡単に書くと, 要素の数がわかる. reshapeは上記のコードの通り.

##[:, 0]
全ての0列目. 2x2の配列なら省略せずに書くと, a[0:2, 0]となる.
A[a:b, c:d]はa~bまでの行でc~dまで(b, d含まない)の列を抜き出す意味.

##np.where

import numpy as np

a = np.arange(9).reshape((3, 3))
print(a)
# [[0 1 2]
#  [3 4 5]
#  [6 7 8]]

print(np.where(a < 4, -1, 100))
# [[ -1  -1  -1]
#  [ -1 100 100]
#  [100 100 100]]

print(np.where(a < 4, True, False))
# [[ True  True  True]
#  [ True False False]
#  [False False False]]

print(a < 4)
# [[ True  True  True]
#  [ True False False]
#  [False False False]]

where以下が条件だけの場合は, true or falseが出てくる.

##np.hstack

列数が同じ配列の統合. 行数の場合はnp.vstack.

##np.unique()

()内の配列の中で, かぶりなく抜き出していく.

import numpy as np

a = np.array([0, 0, 30, 10, 10, 20])
print(a)
# [ 0  0 30 10 10 20]

print(np.unique(a))
# [ 0 10 20 30]

print(type(np.unique(a)))
# <class 'numpy.ndarray'>
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?