Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 ' List[ Conditional statement ] ' What is this code supposed to do.
#1
What does X[y==yi] (Line: 18) do ?

This is the full code:

from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import numpy as np

iris = load_iris()

X = iris.data[:, [0, 2]]
#print(iris.data)

y = iris.target

def plot_scatter(X,y):
    
    colors = ["red","blue","black","yellow","green","purple","orange"]
    markers = ('s', 'x', 'o', '^', 'v')
    
    for i, yi in enumerate(np.unique(y)):
        Xi = X[y==yi]
        print(Xi)
        plt.scatter(Xi[:,0], Xi[:,1], color=colors[i], marker=markers[i], label=yi) 
   
    plt.xlabel('X label')
    plt.ylabel('Y label')
    plt.legend(loc='upper left')
    
plot_scatter(X,y)
Quote
#2
y == yi evaluates to a boolean value (True or False). But bools are actually a subclass of int, so True = 1 and False = 0. So if y == yi, it selects X[1], and if y != yi it selects X[0].

There was a ternary expression added to Python to avoid this sort of code, so that could be rewritten as:

Xi = X[1] if y == yi else X[0]
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Conditional If Statement: If value contains string then set another column equal to s Jack_Sparrow 2 1,120 Jun-15-2018, 03:33 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)