3
6

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 5 years have passed since last update.

Numpyについて

Posted at

Numpy

まずはNumpyについて学んでいきましょう。

import numpy as np 
list_3 = [1, 2, 3, 4, 5, 6, 7, 8, 9]

array_c = np.array(list_3).reshape(3,3)

print(array_c)

出力結果では3行3列の3次元の数が返ってきます。このようにNumpyは様々な配列を返すことが出来ます。

.flatten()で戻します。

array_d = array_c.flatten()
print(array_d)

10の数は2行5列に変換

array_f = np.arange(10).reshape(2,5)
print(array_f)

単位行列

E_1 = np.identity(3)
E_2 = np.eye(6)

print(E_1)
print(E_2)

転置行列

arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

arr_t1 = np.transpose(arr)
arr_t2 = arr.T

print(arr)
print(arr_t1)
print(arr_t2)

axisで繰り返す方向を指定

list_2 = np.arange(6).reshape(2,3)
array_1 = list_2.repeat(2, axis=0)

乱数を取得

random_array = np.random.rand(100)

print(random_array)

乱数を固定

np.random.seed(10)
A1 = np.random.randn(10)

機械学習でよく使用する内積の取り方

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A)
print(B)

AB = np.dot(A, B)

print(AB)

外積

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])

S = np.cross(a, b)

print(S)

演算(参考)

A = np.arange(1, 10).reshape(3,3)
B = np.arange(1, 4).repeat(3).reshape(3,3)
C = A + B 
print(C)
print(B)

np.eye(3)は1となる。

A = np.arange(1,10).reshape(3,3)
B = np.eye(3)* np.arange(1,4)
AB = np.dot(A, B)

print(AB)

3つの配列を書き表して、演算

A = np.array([-1, 1, 5, -1]).reshape(2,2)
B = np.array([4, -1, 5, -2]).reshape(2,2)
C = np.array([1, 1, 5, 1]).reshape(2,2)

ABC = np.dot(A, B).dot(C)
print(ABC)

.Tで転置を取ることができる。

A = np.arange(1,10).reshape(3,3)
B = np.arange(2,11).reshape(3,3)
print(A)
print(B)
AB = np.dot(A, B).T
print(AB)
A = np.arange(1,10).reshape(3,3).T
B = np.arange(2,11).reshape(3,3).T
print(A)
print(B)
AB = np.dot(A, B)
print(AB)

今日はここまで!

3
6
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
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?