简体   繁体   English

Numpy.around()将ndarray len 45转换为ndarray len 1

[英]Numpy.around() converts an ndarray len 45 into an ndarray len 1

I have this ndarray 我有这个ndarray

>>> y_pred_test=[1.         3.         1.986      1.         1.79266667 1.048
 1.         3.         2.         2.         3.         3.
 1.         1.976      3.         2.         1.         1.
 2.03       1.         1.976      1.16966667 1.06       2.
 2.         2.         2.         2.         2.02       3.
 1.         1.         2.02       1.02       1.         1.12511111
 3.         2.07       2.         3.         1.24177778 1.
 2.         2.         2.        ]

>>> type(y_pred_test)

numpy.ndarray

>>> len(y_pred_test)

45

and need to round it, so I use np.around 需要四舍五入,所以我使用np.around

>>> np.around([y_pred_test], decimals=0, out=y_pred_test_round[:,])

>>> print(y_pred_test_round)`

[[1. 3. 2. 1. 2. 1. 1. 3. 2. 2. 3. 3. 1. 2. 3. 2. 1. 1. 2. 1. 2. 1. 1. 2.
  2. 2. 2. 2. 2. 3. 1. 1. 2. 1. 1. 1. 3. 2. 2. 3. 1. 1. 2. 2. 2.]]

But the problem is that now I have an ndarray with len 1 但是问题是现在我有了一个len 1的ndarray

>>> type(y_pred_test_round)

numpy.ndarray

>>> len(y_pred_test_round)

1

I also tried 我也试过

for i in range(len(y_pred_test)):
    np.around([y_pred_test], decimals=0, out=y_pred_test_round[:,i])

and get this error 并得到这个错误

ValueError: non-broadcastable output operand with shape (45,) doesn't match the broadcast shape (1,45)

I can't find the way to fix it, anyone can help please? 我找不到解决方法,有人可以帮忙吗?

Change 更改

np.around([y_pred_test], decimals=0, out=y_pred_test_round[:,])

to

np.around(y_pred_test, decimals=0, out=y_pred_test_round[:,])

You don't need to put y_pred_test into [] in your case. 您无需在情况下将y_pred_test放入[]

Thanks a lot, finally I solved this way 非常感谢,终于我这样解决了

y_pred_test_round = np.around(y_pred_test, decimals=0) print(y_pred_test_round) y_pred_test_round = np.around(y_pred_test, decimals=0) print(y_pred_test_round)

[1. [1。 3. 2. 1. 2. 1. 1. 3. 2. 2. 3. 3. 1. 2. 3. 2. 1. 1. 2. 1. 2. 1. 1. 2. 2. 2. 2. 2. 2. 3. 1. 1. 2. 1. 1. 1. 3. 2. 2. 3. 1. 1. 2. 2. 2.] 3. 2. 1. 2. 1. 1. 3. 3. 2. 2. 3. 3. 1. 2. 3. 2. 1. 1. 2. 1. 2. 1. 1. 1. 2. 2. 2。 2. 2. 2. 3. 1. 1. 2. 1. 1. 1. 1. 3. 2. 2. 3. 1. 1. 2. 2. 2. 2.]

len(y_pred_test_round)

45 45

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM