Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 ' List[ Conditional statement ] ' What is this code supposed to do. Arindam Unladen Swallow Posts: 1 Threads: 1 Joined: Nov 2018 Reputation: 0 Likes received: 0 #1 Nov-30-2018, 04:48 PM (This post was last modified: Nov-30-2018, 04:49 PM by Arindam. Edited 4 times in total.) 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) ``` ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 272 Likes received: 1264 #2 Nov-30-2018, 04:51 PM `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 « Next Oldest | Next Newest »

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,750 Jun-15-2018, 03:33 PM Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)