ラベル numpy の投稿を表示しています。 すべての投稿を表示
ラベル numpy の投稿を表示しています。 すべての投稿を表示

2015年8月14日金曜日

[Python][numpy]行列計算

転置行列 (transpose)
a = numpy.arange(0, 6).reshape(2, 3) # 2x3 行列
print "arange(0, 6).reshapre(2, 3):"
print a
print "transpose():"
print a.transpose() # 転置行列 (transpose)
arange(0, 6).reshapre(2, 3):
[[0 1 2]
[3 4 5]]
transpose():
[[0 3]
[1 4]
[2 5]]



逆行列 (inverse)
a = numpy.arange(0, 4).reshape(2, 2) # 2x2 行列
print "arange(0, 4).reshapre(2, 2):"
print a
print "linalg.inv(a):"
print numpy.linalg.inv(a) # 逆行列 (inverse)
arange(0, 4).reshapre(2, 2):
[[0 1]
[2 3]]
linalg.inv(a):
[[-1.5 0.5]
[ 1. 0. ]]


[Python][numpy]様々な行列

すべて 0 の行列
a = numpy.zeros((3, 4)) # 3x4 行列
print "zeros((3, 4)):"
print a
zeros((3, 4)):
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]


すべて 1 の行列
a = numpy.ones((3, 4)) # 3x4 行列
print "ones((3, 4)):"
print a
ones((3, 4)):
[[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]
[ 1. 1. 1. 1.]]


単位行列
i = numpy.identity(3) # 3x3 単位行列 (identity matirx)
print "identity(3):"
print i
identity(3):
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]


要素を指定して行列生成
a = numpy.array([0, 2, 3, 4, 6, 8]).reshape(2, 3)
print "array([0, 2, 3, 4, 6, 8]).reshape(2, 3):"
print a
array([0, 2, 3, 4, 6, 8]).reshape(2, 3):
[[0 2 3]
[4 6 8]]


特定の対角のみ 1 にする行列
a = numpy.eye(3)
print "eye(3):"
print a
a = numpy.eye(3, k=1)
print "eye(3, k=1):"
print a
eye(3):
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
eye(3, k=1):
[[ 0. 1. 0.]
[ 0. 0. 1.]
[ 0. 0. 0.]]


[Python][numpy]numpy 事始

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

a = numpy.arange(5) # start:0, stop:5
print "arange(5): ",
print a

a = numpy.arange(3, 10) # start:3, stop:10
print "arange(3, 10): ",
print a

a = numpy.arange(3, 10, 2) # start:3, stop:10, step:2
print "arange(3, 10, 2): ",
print a

a = numpy.linspace(0, 2, 6) # 0 から 2 までを 6 段階に線形分割
print "linespace(0, 2, 6): ",
print a

a = numpy.arange(10).reshape(2, 5)
print "arange(10).reshape(2, 5):"
print a
print "a.shape: ",
print a.shape
print "a.ndim: ",
print a.ndim
実行結果
> python test00.py
arange(5): [0 1 2 3 4]
arange(3, 10): [3 4 5 6 7 8 9]
arange(3, 10, 2): [3 5 7 9]
linespace(0, 2, 6): [ 0. 0.4 0.8 1.2 1.6 2. ]
arange(10).reshape(2, 5):
[[0 1 2 3 4]
[5 6 7 8 9]]
a.shape: (2, 5)
a.ndim: 2
lispace(0, 2, 6) で 0 から 2 までを 6 段階に線形分割した数列を得られる。
arange(10).reshapre(2, 5) で 0 から 10 まで (10 は含まず) の数値 (0 - 9) を 2x5 行列に並び替えている