Python初心者のメモ。内容の間違いなどご注意ください。
import numpy as np
def test():
a = np.zeros([5,1]) #2D array columnができる
b = np.ones([5]) #1D array columnなし。
print a
print
print "Dimension a: ", a.shape
print
print b, b.shape
print
print "Dimension b: ", b.shape
print
print a + b #1D + 2Dの演算になる
print
a = np.squeeze(a)
print a, #1D に変更
print
print a + b
if __name__ == '__main__':
test()
Output:
[[ 0.]
[ 0.]
[ 0.]
[ 0.]
[ 0.]]
Dimension a: (5, 1)
[ 1. 1. 1. 1. 1.] (5,)
Dimension b: (5,)
[[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1.]]
[ 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1.]