Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2024/11/24 14:07:03.493 (台灣標準時)

Numpy array の操作について

x と y の二つの Numpy array があるとし、新たに z という Numpy array を 作って、その値は [ [x[0], y[0]], [x[1], y[1]], [x[2], y[2]], ... , [x[i], y[i]], ... ] としたいとするでござる。

以下のようにすればよいようでござる。


#!/usr/pkg/bin/python3.12

#
# Time-stamp: <2024/05/01 22:46:16 (UT+8) daisuke>
#

# importing numpy module
import numpy

# array x
x = numpy.array ([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])

# array y
y = numpy.array ([100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0])

# array z
z = numpy.stack ([x, y])

# printing array x and array y
print (f'x = {x}')
print (f'y = {y}')
print (f'z = {z}')

# making array zt = [ [x[0], y[0]], [x[1], y[1]], [x[2], y[2]], ... ]
zt = numpy.transpose (z)

# printing array z
print (f'zt = {zt}')

実行結果は以下の通りでござる。


% ./test_transpose_1.py
x = [0. 1. 2. 3. 4. 5. 6. 7.]
y = [100. 200. 300. 400. 500. 600. 700. 800.]
z = [[  0.   1.   2.   3.   4.   5.   6.   7.]
 [100. 200. 300. 400. 500. 600. 700. 800.]]
zt = [[  0. 100.]
 [  1. 200.]
 [  2. 300.]
 [  3. 400.]
 [  4. 500.]
 [  5. 600.]
 [  6. 700.]
 [  7. 800.]]

.T attribute を使ってもよいようでござる。


#!/usr/pkg/bin/python3.12

#
# Time-stamp: <2024/05/01 22:46:33 (UT+8) daisuke>
#

# importing numpy module
import numpy

# array x
x = numpy.array ([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])

# array y
y = numpy.array ([100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0])

# array z
z = numpy.stack ([x, y])

# printing array x and array y
print (f'x = {x}')
print (f'y = {y}')
print (f'z = {z}')

# making array zt = [ [x[0], y[0]], [x[1], y[1]], [x[2], y[2]], ... ]
zt = z.T

# printing array z
print (f'zt = {zt}')

実行結果は以下の通りでござる。


% ./test_transpose_2.py
x = [0. 1. 2. 3. 4. 5. 6. 7.]
y = [100. 200. 300. 400. 500. 600. 700. 800.]
z = [[  0.   1.   2.   3.   4.   5.   6.   7.]
 [100. 200. 300. 400. 500. 600. 700. 800.]]
zt = [[  0. 100.]
 [  1. 200.]
 [  2. 300.]
 [  3. 400.]
 [  4. 500.]
 [  5. 600.]
 [  6. 700.]
 [  7. 800.]]

.transpose () method を使う方法もあるようでござる。


#!/usr/pkg/bin/python3.12

#
# Time-stamp: <2024/05/01 22:46:54 (UT+8) daisuke>
#

# importing numpy module
import numpy

# array x
x = numpy.array ([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])

# array y
y = numpy.array ([100.0, 200.0, 300.0, 400.0, 500.0, 600.0, 700.0, 800.0])

# array z
z = numpy.stack ([x, y])

# printing array x and array y
print (f'x = {x}')
print (f'y = {y}')
print (f'z = {z}')

# making array zt = [ [x[0], y[0]], [x[1], y[1]], [x[2], y[2]], ... ]
zt = z.transpose ()

# printing array z
print (f'zt = {zt}')

実行結果は以下の通りでござる。


% ./test_transpose_3.py
x = [0. 1. 2. 3. 4. 5. 6. 7.]
y = [100. 200. 300. 400. 500. 600. 700. 800.]
z = [[  0.   1.   2.   3.   4.   5.   6.   7.]
 [100. 200. 300. 400. 500. 600. 700. 800.]]
zt = [[  0. 100.]
 [  1. 200.]
 [  2. 300.]
 [  3. 400.]
 [  4. 500.]
 [  5. 600.]
 [  6. 700.]
 [  7. 800.]]

参考文献



Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Misc___Taiwan/20240207_00.html
  4. Computer___Network/20230516_00.html
  5. Computer___FreeBSD/20220621_0.html
  6. Computer___Python/20220715_0.html
  7. Computer___Network/20230508_00.html
  8. Food___Taiwan/20220429_0.html
  9. Computer___NetBSD/20220817_3.html
  10. Computer___Python/20220410_0.html
  11. Computer___Network/20240416_00.html
  12. Computer___Network/20240130_00.html
  13. Computer___Debian/20210223_1.html
  14. Computer___NetBSD/20230119_00.html
  15. Computer___Python/20210124_0.html
  16. Computer___Python/20221013_0.html
  17. Computer___NetBSD/20220818_1.html
  18. Computer___NetBSD/20220428_0.html
  19. Science___Math/20220420_0.html
  20. Computer___NetBSD/20240101_02.html
  21. Computer___NetBSD/20220808_0.html
  22. Computer___TeX/20230503_00.html
  23. Computer___NetBSD/20230515_00.html
  24. Science___Astronomy/20220503_0.html
  25. Computer___NetBSD/20210127_0.html
  26. Computer___Python/20240101_00.html
  27. Computer___Network/20220413_1.html
  28. Computer___Python/20220816_1.html
  29. Computer___NetBSD/20210204_0.html
  30. Travel___Taiwan/20220809_2.html


HTML file generated by Kinoshita Daisuke.