Use a list with the fmt parameter to specify the formatting for each column:

fmt=['%d', '%1.1f', '%1.1f', '%1.1f']

Complete example:

import numpy as np
prob_rf = [[1, 0.4, 0.4, 0.4],
           [2, 0.5, 0.5, 0.5],
           [3, 0.6, 0.6, 0.6]]
np.savetxt("foo.csv", prob_rf, delimiter=",", fmt=['%d', '%1.1f', '%1.1f', '%1.1f'])

The resulting file:

1,0.4,0.4,0.4
2,0.5,0.5,0.5
3,0.6,0.6,0.6
Answer from Carsten on Stack Overflow
Discussions

Python Numpy add an array at array index - Stack Overflow
I'm struggling with something that may be very simple or not possible. I want to add an numpy array to another numpy array at a specific index. a = np.zeros(shape=(17, 1, 2)) for i in range(10):... More on stackoverflow.com
🌐 stackoverflow.com
May 8, 2018
arrays - Adding elements to a specified index in python Numpy - Stack Overflow
How can I place a value inside an array witha specified index. Like how can I place the number 3 inside between the 4th and the 5th element in array. number = 3 index= 5 array= np.array([ 31, 28, ... More on stackoverflow.com
🌐 stackoverflow.com
numpy - Add another column(index) into the array - Stack Overflow
What works somehow is : for item in enumerate(array): print >> f , item where f = open('random.txt','w') ... Are you sure you need to directly store the index? NumPy has functions like argmin, where, argsort, etc that normally mean you don't need to do things like this. More on stackoverflow.com
🌐 stackoverflow.com
python 3.x - np.add.at indexing with array - Stack Overflow
So the key is to understand dW[x]. This is the concept of indexing an array(dW) using another array(x). If you are not familiar with this concept, can check out this link · https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html More on stackoverflow.com
🌐 stackoverflow.com
🌐
NumPy
numpy.org › doc › stable › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.5 Manual
There are some tools to facilitate the easy matching of array shapes with expressions and in assignments. Ellipsis expands to the number of : objects needed for the selection tuple to index all dimensions. In most cases, this means that the length of the expanded selection tuple is x.ndim. There may only be a single ellipsis present. From the above example: ... Each newaxis object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimension. The added ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.insert.html
numpy.insert — NumPy v2.5 Manual
Input array. ... Object that defines the index or indices before which values is inserted. Changed in version 2.1.2: Boolean indices are now treated as a mask of elements to insert, rather than being cast to the integers 0 and 1.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc. ... Get the second element from the following array. import numpy as np arr = np.array([1, 2, 3, 4]) print(arr[1]) Try it Yourself » · Get third and fourth elements from the following array and add them.
🌐
Stack Overflow
stackoverflow.com › questions › 50233649 › python-numpy-add-an-array-at-array-index
Python Numpy add an array at array index - Stack Overflow
May 8, 2018 - I acknowledge numpy to be a powerful library, but you ask it to initialize zeros, which are int then want to add in list. You cannot expect the constructor to know at creation time that it needs to allocate space for object type data. What you want is to help the numpy ndarray constructor with type inference. a = np.zeros(shape=(17, 1, 2), dtype=object) for i in range(10): b = [i] c = [1,2,3,4] b.append(c) a[i] = b a #array([[[0, [1, 2, 3, 4]]], # # [[1, [1, 2, 3, 4]]], # # [[2, [1, 2, 3, 4]]], # # [[3, [1, 2, 3, 4]]], # # [[4, [1, 2, 3, 4]]], # [[5, [1, 2, 3, 4]]], # [[6, [1, 2, 3, 4]]], # [[7, [1, 2, 3, 4]]], # [[8, [1, 2, 3, 4]]], # [[9, [1, 2, 3, 4]]], # [[0, 0]], # [[0, 0]], # [[0, 0]], # [[0, 0]], # [[0, 0]], # [[0, 0]], # [[0, 0]]], dtype=object)
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-indexing
Numpy Array Indexing - GeeksforGeeks
December 17, 2025 - Here it adds a new axis helps in converting the 1D array into a 2D column vector with shape (3,1). We can modify array elements directly by using indexing or slicing. This makes it easy to update specific elements or ranges of elements in an array. ... The slice arr[1:3] selects elements at indices 1 and 2 and replaces them with 99. By mastering these techniques, we'll be able to manipulate and analyze data more efficiently with NumPy...
🌐
NumPy
numpy.org › devdocs › user › basics.indexing.html
Indexing on ndarrays — NumPy v2.6.dev0 Manual
There are some tools to facilitate the easy matching of array shapes with expressions and in assignments. Ellipsis expands to the number of : objects needed for the selection tuple to index all dimensions. In most cases, this means that the length of the expanded selection tuple is x.ndim. There may only be a single ellipsis present. From the above example: ... Each newaxis object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimension. The added ...
🌐
NumPy
numpy.org › devdocs › user › how-to-index.html
How to index ndarrays — NumPy v2.6.dev0 Manual
To index specific elements in each column, make use of Advanced indexing as below: >>> arr = np.arange(3*4).reshape(3, 4) >>> arr array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) >>> column_indices = [[1, 3], [0, 2], [2, 2]] >>> np.arange(arr.shape[0]) array([0, 1, 2]) >>> row_indices = np.arange(arr.shape[0])[:, np.newaxis] >>> row_indices array([[0], [1], [2]])
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.indices.html
numpy.indices — NumPy v2.1 Manual
The indices can be used as an index into an array. >>> x = np.arange(20).reshape(5, 4) >>> row, col = np.indices((2, 3)) >>> x[row, col] array([[0, 1, 2], [4, 5, 6]]) Note that it would be more straightforward in the above example to extract the required elements directly with x[:2, :3].
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.insert.html
numpy.insert — NumPy v2.1 Manual
Input array. ... Object that defines the index or indices before which values is inserted. New in version 1.8.0. Support for multiple insertions when obj is a single scalar or a sequence with one element (similar to calling insert multiple times).
Top answer
1 of 3
18
In [226]: x = [[0,4,1], [3,2,4]]
     ...: dW = np.zeros((5,6),int)

In [227]: np.add.at(dW,x,1)
In [228]: dW
Out[228]: 
array([[0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0]])

With this x there aren't any duplicate entries, so add.at is the same as using += indexing. Equivalently we can read the changed values with:

In [229]: dW[x[0], x[1]]
Out[229]: array([1, 1, 1])

The indices work the same either way, including broadcasting:

In [234]: dW[...]=0
In [235]: np.add.at(dW,[[[1],[2]],[2,4,4]],1)
In [236]: dW
Out[236]: 
array([[0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 2, 0],
       [0, 0, 1, 0, 2, 0],
       [0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0]])

possible values

The values have to be broadcastable, with respect to the indexes:

In [112]: np.add.at(dW,[[[1],[2]],[2,4,4]],np.ones((2,3)))
...
In [114]: np.add.at(dW,[[[1],[2]],[2,4,4]],np.ones((2,3)).ravel())
...
ValueError: array is not broadcastable to correct shape
In [115]: np.add.at(dW,[[[1],[2]],[2,4,4]],[1,2,3])

In [117]: np.add.at(dW,[[[1],[2]],[2,4,4]],[[1],[2]])

In [118]: dW
Out[118]: 
array([[ 0,  0,  0,  0,  0,  0],
       [ 0,  0,  3,  0,  9,  0],
       [ 0,  0,  4,  0, 11,  0],
       [ 0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0]])

In this case the indices define a (2,3) shape, so (2,3),(3,), (2,1), and scalar values work. (6,) does not.

In this case, add.at is mapping a (2,3) array onto a (2,2) subarray of dW.

2 of 3
7

recently I also have a hard time to understand this line of code. Hope what I got can help you, correct me if I am wrong.

The three arrays in this line of code is following:

x , whose shape is (N,T)
dW,  ---(V,D)
dout ---(N,T,D)

Then we come to the line code we want to figure out what happens

np.add.at(dW, x, dout)

If you dont want to know the thinking procedure. The above code is equivalent to :

for row in range(N):
   for col in range(T):
      dW[ x[row,col]  , :] += dout[row,col, :]

This is the thinking procedure:

Refering to this doc

https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.ufunc.at.html

We know that the x is the index array. So the key is to understand dW[x]. This is the concept of indexing an array(dW) using another array(x). If you are not familiar with this concept, can check out this link

https://docs.scipy.org/doc/numpy-1.13.0/user/basics.indexing.html

Generally speaking, what is returned when index arrays are used is an array with the same shape as the index array, but with the type and values of the array being indexed.

dW[x] will give us an array whose shape is (N,T,D), the (N,T) part comes from x, and the (D) comes from dW (V,D). Note here, every element of x is inside the range of [0, v).

Let's take some number as concrete example

x:    np.array([[0,0],[0,0]]) ---- (2,2) N=2, T=2
dW:   np.array([[0,0],[2,2]]) ---- (2,2) V=2, D=2
dout: np.arange(1,9).reshape(2,2,2)  ----(2,2,2) N=2, T=2, D=2

dW[x] should be [ [[0 0] #this comes from the dW's firt row
                  [0 0]]

                  [[0 0]
                   [0 0]] ]

dW[x] add dout means that add the elemnet item(here, this some trick, later will explian)

np.add.at(dW, x, dout) gives 
 [ [16 20]
   [ 2  2] ]

Why? The procedure is:

It add [1,2] to the first row of dW, which is [0,0].

Why first row? Because the x[0,0] = 0, indicating the first row of dW, dW[0] = dW[0,:] = the first row.

Then it add [3,4] to the first row of dW[0,0]. [3,4]=dout[0,1,:]. [0,0] again, comes from the dW, x[0,1] = 0, still the first row of dW[0].

Then it add [5,6] to the first row of dW.

Then it add [7,8] to the first row of dW.

So the result is [1+3+5+7, 2+4+6+8] = [16,20]. Because we do not touch the second row of dW. The dW's second row remains unchanged.

The trick is that we will only count the origin row once, can think that there is no buffer, and every step plays in the original place.

🌐
Statology
statology.org › home › how to add elements to numpy array (3 examples)
How to Add Elements to NumPy Array (3 Examples)
June 15, 2022 - You can use the following methods to add one or more elements to a NumPy array: ... #insert 95 and 99 starting at index position 2 of the NumPy array new_array = np.insert(my_array, 2, [95, 99])
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.insert.html
numpy.insert — NumPy v2.2 Manual
Input array. ... Object that defines the index or indices before which values is inserted. Changed in version 2.1.2: Boolean indices are now treated as a mask of elements to insert, rather than being cast to the integers 0 and 1.
🌐
DataCamp
datacamp.com › doc › numpy › insert-numpy
NumPy insert()
import numpy as np arr = np.array([1, 2, 3, 4]) # Insert 9 at index 2 new_arr = np.insert(arr, 2, 9) # Resulting array: [1, 2, 9, 3, 4] import numpy as np arr = np.array([[1, 2], [3, 4]]) # Insert row [5, 6] before the second row new_arr = np.insert(arr, 1, [5, 6], axis=0) # Resulting array: [[1, 2], [5, 6], [3, 4]]
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › insert()
Python Numpy insert() - Insert Elements
November 15, 2024 - Define an initial array. Use numpy.insert() to add an element at a desired index.