model
とは何ですか?どのモジュールからですか? predictions
は2次元配列です。 predictions.shape
とは何ですか?このエラーは[x for x in predictions]
のx
が配列であることを示します。これは単一要素の配列でも構いませんが、それほど配列ではありません。 [x.shape for x in predictions]
を試して、predictions
の各要素(行)の形状を確認することができます。
私は(__add__
から+
代表者など多くの).__round__
メソッドにアクションをround
を使用するための多くの機会があったが、明らかにPythonの機能を委譲していません。
In [932]: round?
Docstring:
round(number[, ndigits]) -> number
Round a number to a given precision in decimal digits (default 0 digits).
This returns an int when called with one argument, otherwise the
same type as the number. ndigits may be negative.
Type: builtin_function_or_method
In [933]: x=12.34
In [934]: x.__round__?
Docstring:
Return the Integral closest to x, rounding half toward even.
When an argument is passed, work like built-in round(x, ndigits).
Type: builtin_function_or_method
In [935]: y=12
In [936]: y.__round__?
Docstring:
Rounding an Integral returns itself.
Rounding with an ndigits argument also returns an integer.
Type: builtin_function_or_method
Pythonの整数は、Pythonの浮動小数点数とは異なる実装です。
Pythonのリストと文字列にはこの定義がありません。round([1,2,3])
はAttributeError: 'list' object has no attribute '__round__'
を返します。
ndarray
と同じです。しかし、numpy
はnp.round
関数を定義し、numpy配列は.round
メソッドを持っています。
In [942]: np.array([1.23,3,34.34]).round()
Out[942]: array([ 1., 3., 34.])
In [943]: np.round(np.array([1.23,3,34.34]))
Out[943]: array([ 1., 3., 34.])
help(np.around)
は、numpyバージョンの完全なドキュメントを提供します。私はあなたのようpredictions
の一部を再構築することができ、あなたの最後の印刷から
===================
:
In [955]: arr = np.array([[ 0.79361773], [ 0.10443521], [ 0.90862566]])
In [956]: arr
Out[956]:
array([[ 0.79361773],
[ 0.10443521],
[ 0.90862566]])
In [957]: for x in arr:
...: print(x, end=' ')
...:
[ 0.79361773] [ 0.10443521] [ 0.90862566]
arr.shape
は(3,1)
です - 1列の2次元配列。
np.round
は、反復を必要とせず、正常に動作します:
In [958]: np.round(arr)
Out[958]:
array([[ 1.],
[ 0.],
[ 1.]])
反復はあなたのエラーを生成します。
In [959]: [round(x) for x in arr]
TypeError: type numpy.ndarray doesn't define __round__ method
である私は、 'predictions'はおそらく、2D配列だと思います形状(11,1)を有する。 – hpaulj