2
0

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

numpy.reshape version1.18.x : ValueError: Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead エラー

Last updated at Posted at 2020-05-13

背景

numpyのVersion1.17.2から1.18.1へのの変更で、**np.reshape(a, size, 1)**で異常が出て通らなくなった。

Version1.17.2 OK ⇒ 1.18.1 NG

np.reshape(a,size,1) ← エラー発生
ValueError: Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead
np.reshape(a,size,order="F") ←修正後エラー無し(正しく文法に従って記述)

0 ⇒ order = "C"
1 ⇒ order = "F"

原因

Version18あたりから**orderで簡単に(0 or 1)**を使用する事が出来なくなった(完全に禁止?)と想定。
##参考

numpy.matrix.flatten
マトリクスのFlattenに関する情報(20120312)
配列を形状変換するNumPyのreshapeの使い方
numpy.reshape(a, newshape, order=’C’)に関する説明(20170524)

【1】 numpy version 1.17.2 正しくフラット化が出来ていたが!!

2020年3月
まず、3x3の配列を作成して、その後にa行方向のベクトルに変更する。

import numpy as np
a=np.arange(9).reshape((3,3))
print(a);

[[0 1 2]
[3 4 5]
[6 7 8]]

"""version1.17.2"""
print(np.reshape(a,9,1))
print(np.reshape(a,9,0))

[0 3 6 1 4 7 2 5 8] OK 1:列優先でのフラット化
[0 1 2 3 4 5 6 7 8] OK 0:行優先でのフラット化

1が列優先、0が行優先

【2】numpy version 1.18.1 ではコードを変えないとダメ

古いコードだとエラー

"""version1.18.1"""
print(np.reshape(a,9,1))  #エラー
print(np.reshape(a,9,0))  #エラー

正しく記載するとOK

"""version1.18.1"""
print(np.reshape(a,9,order="C"))  # OK
print(np.reshape(a,9,order="F"))  # OK
print(np.reshape(a,9,order="A"))  # OK
print(np.reshape(a,9,order="K"))  # order "K" is note permitted for reshaping

[0 1 2 3 4 5 6 7 8]   #行方向優先 (C Style):defalut
[0 3 6 1 4 7 2 5 8]   #列方向優先 (Fortran Style)
[0 1 2 3 4 5 6 7 8]   #行方向優先(A: Fortran条件付きで列優先、それ以外は行)
エラー

(参考)エラー警告
ValueError                                Traceback (most recent call last)
<ipython-input-9-606680189191> in <module>
      2 a=np.arange(9).reshape((3,3))
      3 print(a)
----> 4 print(np.reshape(a,9,1))
<__array_function__ internals> in reshape(*args, **kwargs)
~/.pyenv/versions/anaconda3-2019.10/envs/tf20200320/lib/python3.7/site-packages/\
              numpy/core/fromnumeric.py in reshape(a, newshape, order)
--> 301     return _wrapfunc(a, 'reshape', newshape, order=order)
|
ValueError: Non-string object detected for the array ordering. Please pass in 'C', 'F', 'A', or 'K' instead

##【3】 Version間での比較

######Version1.17.2

"""numpy Version1.17.2"""
import numpy as np
a=np.arange(9).reshape((3,3))
b=np.size(a) 
print(a)                             # 3x3
print(b)                             # 9 
print("0,1を記載")
print(np.reshape(a,9,0))             #[0 1 2 3 4 5 6 7 8] 行優先             
print(np.reshape(a,9,1))             #[0 3 6 1 4 7 2 5 8] 列
print(np.reshape(a,b,1))             #[0 3 6 1 4 7 2 5 8] 列
print(np.reshape(a,np.size(a),1))    #[0 3 6 1 4 7 2 5 8] 列
print("")
print(np.reshape(a,(1,9),1))         #[0 3 6 1 4 7 2 5 8] 列
print(np.reshape(a,[1,9],1))         #[0 3 6 1 4 7 2 5 8] 列 
print("")
print("order記載")
print(np.reshape(a,9,order="C"))     #[0 1 2 3 4 5 6 7 8] 行優先
print(np.reshape(a,9,order="F"))     #[0 3 6 1 4 7 2 5 8] 列
"""全てOK"""

######Version1.18.1

"""numpy Version1.18.1"""
import numpy as np
a=np.arange(9).reshape((3,3))
b=np.size(a) 
print(a)                             # 3x3
print(b)                             # 9 
print("0,1を記載")
print(np.reshape(a,9,0))             #エラー
print(np.reshape(a,9,1))             #エラー
print(np.reshape(a,b,1))             #エラー
print(np.reshape(a,np.size(a),1))    #エラー
print("")
print(np.reshape(a,(1,9),1))         #エラー
print(np.reshape(a,[1,9],1))         #エラー
print("")
print("order記載")
print(np.reshape(a,9,order="C"))     #[0 1 2 3 4 5 6 7 8] 行優先
print(np.reshape(a,9,order="F"))     #[0 3 6 1 4 7 2 5 8] 列
"""全てOK"""

【4】 記述の説明

np.ndarray.flatten?
Docstring:
a.flatten(order='C')

Return a copy of the array collapsed into one dimension.

Parameters
----------
order : {'C', 'F', 'A', 'K'}, optional
    'C' means to flatten in row-major (C-style) order.
    'F' means to flatten in column-major (Fortran-
    style) order. 'A' means to flatten in column-major
    order if `a` is Fortran *contiguous* in memory,
    row-major order otherwise. 'K' means to flatten
    `a` in the order the elements occur in memory.
    The default is 'C'.
参考資料

numpy.matrix.flatten

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?