[转载] AttributeError: ‘numpy.ndarray‘ object has no attribute ‘insert‘的解决方法

参考链接: Python中的numpy.insert

AttributeError: 'numpy.ndarray' object  has no attribute 'insert'

代码: 

import numpy as np

np.set_printoptions(threshold = 1e6)

test = np.load('E:/jiaxin/一些烂七八糟/034_pbb.npy')

test = test[0]

print(test)

test1 = test[0]

print(test1)

扫描二维码关注公众号,回复: 12287493 查看本文章

print(test[1:])

print(test[1:]/4)

test2 = test[1:]/4

test2.insert(0, test1)

print(test2)

运行代码出错: 

Traceback (most recent call last):

[  -2.72441649   29.71287845  162.18161245  193.31958149   23.76840839]

  File "E:/jiaxin/一些烂七八糟/reducer/2.py", line 278, in <module>

    test2.insert(0, test1)

-2.72441649437

AttributeError: 'numpy.ndarray' object has no attribute 'insert'

[  29.71287845  162.18161245  193.31958149   23.76840839]

[  7.42821961  40.54540311  48.32989537   5.9421021 ]

运行结果可以看出,list数组和.ndarray的显示也有不同。 解决方法: 

import numpy as np

np.set_printoptions(threshold = 1e6)

test = np.load('E:/jiaxin/一些烂七八糟/034_pbb.npy')

test = test[0]

print(test)

test1 = test[0]

print(test1)

print(test[1:])

print(test[1:]/4)

test2 = test[1:]/4

test2 = np.insert(test2, 0, test1)

print(test2)

即成功:修改的代码:test2.insert(0, test1) 修改为 test2 = np.insert(test2, 0, test1) 

(实验结果) 

[  -2.72441649   29.71287845  162.18161245  193.31958149   23.76840839]

-2.72441649437

[  29.71287845  162.18161245  193.31958149   23.76840839]

[  7.42821961  40.54540311  48.32989537   5.9421021 ]

[ -2.72441649   7.42821961  40.54540311  48.32989537   5.9421021 ]

猜你喜欢

转载自blog.csdn.net/u013946150/article/details/113102352