Assign to any column in each row with np.array

I don't know how to do this in detail

>>> import numpy as np
>>> A = np.zeros((4,2))
>>> A
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])
>>> index = np.array([0,1,0,1])
>>> index
array([0, 1, 0, 1])
>>> values = np.array([10,20,10,10])
>>> values
array([10, 20, 10, 10])
>>> A[np.arange(4),index] = values
>>> A
array([[ 10.,   0.],
       [  0.,  20.],
       [ 10.,   0.],
       [  0.,  10.]])

Recommended Posts

Assign to any column in each row with np.array
Things to keep in mind when converting row vectors to column vectors with ndarray
How to extract any appointment in Google Calendar with Python
Try logging in to qiita with Python
How to work with BigQuery in Python
To work with timestamp stations in Python
How to deal with memory leaks in matplotlib.pyplot
Log in to the remote server with SSH
I tried to integrate with Keras in TFv1.1
How to deal with run-time errors in subprocess.call
Log in to Yahoo Business with Selenium Python
Decide to assign a laboratory with Python (fiction)
Easily log in to AWS with multiple accounts
How to use tkinter with python in pyenv
How to count the number of occurrences of each element in the list in Python with weight
To extract the data of a specific column in a specific sheet in multiple Excel files at once and put the data in each column in one row