やりたいこと
配列[1,2]と[4,5,6]を使って以下の2次元配列Matrixを作ります。
Matrix =
1 2
4 5 6
その後、Matrix の行・列を転置させたMatrix_tを作ります。
Matrix_t =
1 4
2 5
6
Matrix 既に与えられているので、これを用いてMatrix_t を作り出したい。
困っていること
Matrix_t を作って表示させたが、期待通りの結果にならない。
配列[1,2,3]と[4,5,6]だと期待通り、転置されるが、要素数が異なる配列[1,2]と[4,5,6]だと、以下の実行例のように転置されない。
解決法をご教授いただきたいです。
ソース
python
1import pandas 2import numpy 3 4list1 = [1,2] 5list2 = [4,5,6] 6 7Matrix = [] 8Matrix.append(list1) 9Matrix.append(list2) 10 11print(Matrix) 12Matrix_t = numpy.array(Matrix).T 13print(Matrix_t) 14
出力
[[1, 2], [4, 5, 6]]
[list([1, 2]) list([4, 5, 6])]
C:\User\AppData\Local\Temp/ipykernel_3028/3663226603.py:12: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
Matrix_t = numpy.array(Matrix).T
期待していた出力(やりたいこと)
[[1 4]
[2 5]
[ 6]]

回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。