Dont understand how this example one-hot code indexes a numpy array with [i,j] when j is a tuple?

2024/10/10 3:27:27

I don't get how the line: results[i, sequence] = 1 works in the following.

I am following along in the debugger with some sample code in a Manning book: "Deep Learning with Python" (Example 3.5-classifying-movie-reviews.ipynb from the book) and while I understand what the code does, I don't get the syntax or how it works. I'm trying to learn Python and Deep learning together and want to understand what the code is doing.

def vectorize_sequences(sequences, dimension=10000):# Create an all-zero matrix of shape (len(sequences), dimension)results = np.zeros((len(sequences), dimension))for i, sequence in enumerate(sequences):results[i, sequence] = 1.  # <--- How does this work?return results
  • This creates results, a 25,000 x 10,000 array of zeros.
  • sequences is a list-of-tuples, for example (3, 0, 5). It then walks sequences and for each non-zero value of each sequence, set the corresponding index in results[i] to 1.0. They call it one-hot encoding.
  • I don't understand how the line: results[i, sequence] = 1 accomplishes this in numpy.
  • I get the for i, sequence in enumerate(sequences) part: just enumerating the sequences list and keeping track of i.
  • I'm guessing there is some numpy magic that is somehow setting values in results[i] based on examining sequence[n] element by element and inserting a 1.0 whenever sequence[n] is non-zero(?) Just want to understand the syntax.
Answer

Assuming sequence is a list of integers,

results[i,sequence] = 1

is equivalent to

for j in sequence:results[i][j] = 1
https://en.xdnf.cn/q/118503.html

Related Q&A

convert nested list to normal list using list comprehension in python [duplicate]

This question already has answers here:How do I make a flat list out of a list of lists?(32 answers)Flatten an irregular (arbitrarily nested) list of lists(54 answers)Closed 6 years ago.How can I do t…

Transformation of pandas DataFrame adds a blank row

My original question was posted here. I have a dataframe as follows:ID START END SEQ 1 11 12 1 1 14 15 3 1 13 14 2 2 10 14 1 3 11 15 1 3 16 17 …

How to find a element after a search click checkbox in selenium Python

After I search for a user in the search field, I get the user I searched Now I need to select this user that shown in the list I tried with xpath and did not find the element ! could you help ? So aft…

Running parameterized queries

Quite new to this google bigquery sql thing so please bear with me. Im trying to build a google standardSQL parameterized query. The following sample was used and ran successfully on Google BigQuery We…

how to do this disparity map in OpenCV

paper "Fast Obstacle Detection Using U-Disparity Maps with Stereo Vision"reference paper: "Fast Obstacle Detection Using U-Disparity Maps with Stereo Vision"I want to ask can opencv…

How do I give out a role when I click on a reaction? It doesnt work for me?

Im trying to make sure that when you enter a command, a message is sent and a reaction is set, and those who click on the reaction get a role, but when you enter a command, nothing happens. Here is the…

Python Pandas DataFrame Rounding of big fraction values [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Splitting Data in Python?

it does not work. I want to split data as in code in lines attribute. class movie_analyzer:def __init__(self,s):for c in punctuation:import removiefile = open(s, encoding = "latin-1")movielis…

Use local function variable inside loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Groupby Pandas , calculate multiple columns based on date difference

I have a pandas dataframe shown below: CID RefID Date Group MID 100 1 1/01/2021 A 100 2 3/01/2021 A 100 3 4/01/20…