0

I am trying to insert a numpy float in a numpy ndarray. The Code and the output is:

dos = np.sum(atom[:, :, 1:],axis=0)
print("type(dos)")
print(type(dos))
print("dos.shape")
print(dos.shape)
print("dos[15] Before")
print(dos[15])
print("type(atom[1,0,0])")
print(type(atom[1,0,0]))
print("atom[1,0,0]")
print(atom[1,0,0])
for i in range(301):
    dos2=np.insert(dos, 0, atom[1,0,0])
print("dos[15] After ")
print(dos2[15])
print("type(dos2)")
print(type(dos2))

and the corresponding output is:

type(dos)
<class 'numpy.ndarray'>
dos.shape
(301, 18)
dos[15] Before
[ -9.75080030e-02  -8.37110240e-02  -3.13760517e-03  -2.70089494e-03
  -2.07915835e-03  -1.77532740e-03  -2.03548911e-03  -1.73346437e-03
  -1.98000973e-04  -1.64015415e-04  -1.99115166e-04  -1.65569761e-04
  -9.07381374e-05  -7.37546825e-05  -1.48250176e-04  -1.22108731e-04
  -1.18854648e-04  -9.70416840e-05]
type(atom[1,0,0])
<class 'numpy.float64'>
atom[1,0,0]
-4.11
dos[15] After 
0.0
type(dos2)
<class 'numpy.ndarray'>

where the expected result is:

 [ -4.11 -9.75080030e-02  -8.37110240e-02  -3.13760517e-03  -2.70089494e-03
         -2.07915835e-03  -1.77532740e-03  -2.03548911e-03  -1.73346437e-03
         -1.98000973e-04  -1.64015415e-04  -1.99115166e-04  -1.65569761e-04
         -9.07381374e-05  -7.37546825e-05  -1.48250176e-04  -1.22108731e-04
         -1.18854648e-04  -9.70416840e-05]

from the numpy documentation, I cant see where i went wrong. Kindly help.

2 Answers 2

2

From the doc mentionned:

A copy of arr with values inserted. Note that insert does not occur in-place: a new array is returned. If axis is None, out is a flattened array.

This means that your loop:

for i in range(301):
    dos2=np.insert(dos, 0, atom[1,0,0])

Does 300 useless operations, then inserts a single value, and dos2 contains the 301*18 values of dos plus one value (flattened):

>>> dos = np.random.random((3, 3))
>>> dos2 = np.insert(dos, 0, 12)
>>> dos2
array([ 12.        ,   0.30211688,   0.39685661,   0.89568364,
         0.14398144,   0.39122099,   0.8017827 ,   0.35158563,
         0.18771122,   0.89938571])
>>> dos2[5]
0.39122099250162556

What you probably want is to happend that value to each of the elements in dos:

>>> dos2 = np.empty((dos.shape[0], dos.shape[1] + 1), dtype=dos.dtype)
>>> for i in range(dos.shape[0]):
...     dos2[i] = np.insert(dos[i], 0, 12)
...
>>> dos2
array([[ 12.        ,   0.30211688,   0.39685661,   0.89568364],
       [ 12.        ,   0.14398144,   0.39122099,   0.8017827 ],
       [ 12.        ,   0.35158563,   0.18771122,   0.89938571]])

Which can also be expressed simply as:

>>> dos2 = np.empty((dos.shape[0], dos.shape[1] + 1), dtype=dos.dtype)
>>> dos2[:, 0] = 12
>>> dos2[:, 1:] = dos
Sign up to request clarification or add additional context in comments.

Comments

1

From your 'expected result' it looks like you only want to insert a single value at the beginning of the already flat array. There is definitely no need to use a for loop to do this.

>>> insert_value = 100
>>> orig_array = np.array([1, 2, 3, 4, 5])
>>> final_array = np.insert(orig_array, 0, insert_value)
>>> print(final_array)
[100   1   2   3   4   5]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.