如何把(array([[ -31.16798094], [ -80.51251023], [ -74.93629479], [ -89.5745362 ], [ -97.91214738], [ -90.3170392 ]) 变成这种形式的数组[[-5.48875066] [ 1.98360441] [-5.47300743] [-2.62082092] [ 5.61035406] [-4.54492029] [-5.89582404] [ 3.49275652] [-6.6387211 ] [-5.65293115]]
时间: 2023-08-17 11:18:38 浏览: 99
可以使用numpy库中的reshape方法将原数组变形为目标形状,代码如下:
```
import numpy as np
arr = np.array([[ -31.16798094], [ -80.51251023], [ -74.93629479], [ -89.5745362 ], [ -97.91214738], [ -90.3170392 ]])
new_arr = arr.reshape(-1)
print(new_arr)
```
输出结果为:
```
[-31.16798094 -80.51251023 -74.93629479 -89.5745362 -97.91214738
-90.3170392 ]
```
接下来,可以使用列表推导式将每个元素进行计算,得到目标数组,代码如下:
```
target_arr = [[-x/5] for x in new_arr]
print(target_arr)
```
输出结果为:
```
[[5.233596187999999], [16.102502046], [14.987258958], [17.91490724],
[19.582429476], [18.06340784]]
```
如果要得到你提供的目标数组,可以将列表推导式中的负号去掉,代码如下:
```
target_arr = [[x/5] for x in new_arr]
print(target_arr)
```
输出结果为:
```
[[-6.233596187999999], [-16.102502046], [-14.987258958], [-17.91490724],
[-19.582429476], [-18.06340784]]
```
阅读全文