Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I don't understand how the second bracket works

This piece of code is for plotting a series of data by coloring by the classes they belong to. X_train is an array (115,2) and Y_train is another array (115,) with their respective scope values. My question is what does [Y_train == i] do exactly?

colors = ["red", "greenyellow", "blue"]
for i in range(len(colors)):
    xs = X_train[:, 0][Y_train == i]
    ys = X_train[:,1][Y_train == i]
    plt.scatter(xs, ys, c = colors[i])

plt.legend(iris.target_names)
plt.xlabel("Sepal length")
plt.ylabel("Sepal width") 

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Boolean values in python are just subclasses of integers.

Y_train == i just evaluates into either False or True, which is then used to access either index 0 or 1 respectively.

>>> a = ['this string is at index 0', 'this string is at index 1']
>>> a[True]
'this string is at index 1'
>>> a[False]
'this string is at index 0'
>>> a[1 + 2 == 3]  # true
'this string is at index 1'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading