[Reprint] AttributeError:'numpy.ndarray' object has no attribute'insert' solution

Reference link: numpy.insert in Python

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

 

Code: 

import numpy as np

np.set_printoptions(threshold = 1e6)

test = np.load('E:/jiaxin/some ugly/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.insert(0, test1)

print(test2)

 

Error running code: 

Traceback (most recent call last):

[  -2.72441649   29.71287845  162.18161245  193.31958149   23.76840839]

  File "E:/jiaxin/some rotten mess/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 ]

 

It can be seen from the running result that the display of list array and .ndarray is also different. Solution: 

import numpy as np

np.set_printoptions(threshold = 1e6)

test = np.load('E:/jiaxin/some ugly/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)

 

That is success: modified code: test2.insert(0, test1) modified to test2 = np.insert(test2, 0, test1) 

(Experiment result) 

[  -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 ]

Guess you like

Origin blog.csdn.net/u013946150/article/details/113102352
Recommended