0

The data corresponds to 3 rows where the first row is the marks of Exam number one of a particular student and row number two is the marks in Exam number 2 of the student. The third row corresponds to 0 or 1 indicating his probability to enter a particular University. Here is the code given for plotting the graph which I am not able to understand.

# Find Indices of Positive and Negative Examples
pos = y == 1
neg = y == 0

# Plot Examples
pyplot.plot(X[pos, 0], X[pos, 1], 'k*', lw=2, ms=10)
pyplot.plot(X[neg, 0], X[neg, 1], 'ko', mfc='y', ms=8, mec='k', mew=1)

The output is the image given below:

2-Dimensional Plot

Any help in explaining the code is appreciated.

3
  • What is the question? Commented Aug 1, 2020 at 6:17
  • I am not able to understand the code for plotting the 2d graph. Any documentation where I can read through? Commented Aug 1, 2020 at 6:19
  • matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html Commented Aug 1, 2020 at 6:22

2 Answers 2

1

This code consists two different data, put together into one plot. They are all done with 'matplotlib' as you can read documentation here.

First plot is plotting only positive examples, marked as a star. X[pos,0] is x-axis (first row, only positive examples) and X[pos,1] is y-axis (second row, only positive examples). Rest of the arguments: k* means the style will be "stars", lw stands for "linewidth" and ms for "markersize", how big each start is.

Second plot is the same, only now for the circle which are negative. First two arguments are the same, only with negative examples. ko means to represent each dot a circle (hence o). mfc, mec, mew are for choosing the color of the marker.

Sign up to request clarification or add additional context in comments.

2 Comments

So pos and neg are vectors with indices where y== 1 or y==0 respectively?
yes. pos for example return true only for indices with y==1, so it filter them for you
0

Let's understand by example.

Here Y must be matrix which stores 0 and 1 value.

[0. 0. 0. 1. ]

So when you write below code

pos = y == 1
neg = y == 0

Matrix comparison happens, so,

-wherever row is having value 1 marked as True

-Wherever row is having value 0 marked as False

Hence you will get matrix like below

pos = [False False False,True]
neg = [ True  True  True False]

Hence this line of code

 X[pos, 0]--gives 4th row of first column in the matrix X. Because Row 4 is having true.


 X[neg, 0]-- gives 3 rows values, because first 3 rows values of neg matrix are True

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.