Thursday, May 30, 2019

What is purpose of ":" in python array access code line colors[rnd_ind, :]?




I am new to python and many constructions blows away my C styled brain.

Now I need to understand how some python code works.



#Training inputs for RGBcolors
colors = np.array(
[[0.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 0.5],
[0.125, 0.529, 1.0],
[0.33, 0.4, 0.67],
[0.6, 0.5, 1.0],

[0.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
[0.33, 0.33, 0.33],
[0.5, 0.5, 0.5],
[0.66, 0.66, 0.66]])


for i in range(num_training):
rnd_ind = np.random.randint(0, len(colors))
s.train(colors[rnd_ind, :]) //what's going on here?


This is the train body:



def train(self, input_x):
# Compute the winning unit
bmu_index, diff = self.session.run([self.bmu_index, self.diff], {self.input_placeholder:[input_x], self.current_iteration:self.num_iterations})


# Update the network's weights
self.session.run(self.update_weights, {self.diff_2:diff, self.dist_sliced:self.dist[bmu_index[0],:], self.current_iteration:self.num_iterations})

self.num_iterations = min(self.num_iterations+1, self.num_expected_iterations)


I set breakpoint to train beginning to see how it's input parameter looks like, but I no see anything unusual. This is just an array.



I tried searching and found this Colon (:) in Python list index question, but it looks like this is something different, because in my case : written after ,, but in their cases it follows after some value.



Answer



This has nothing to do with standard python arrays. If you use it in python lists you ll get an error.



Traceback (most recent call last):
File "asd.py", line 3, in
print(x[0, :])
TypeError: list indices must be integers or slices, not tuple


This is specific to numpy. It is an indexing of a multidimensional array. First number is the first dimension, second is the second and so on.




import numpy as np

x = np.array([[1,2,3], [3,4,5], [2,3,4], [4,7,8]])

print(x[0, 1:2])


Will output [2]


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...