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 :
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'