How did you guys complete the matplotlib exercise
How did you guys complete the matplotlib exercise
I' m not sure how to go about plotter the xor scatter plot. I've only been learning python intensively over the last two weeks, so i am loaded up on raw knowledge, but I'm lacking in the experience necessary to make certain connections between raw knowledge and applications apparent at this point. Despite my best reasoning on what has been covered, i can't complete this task. I assume this exercise requires the use of some loop and conditional statement, but despite trying over and over things that make sense to me, I only get the wrong outputs or errors. Please help, thank you.

 Posts: 1
 Joined: Mon May 25, 2020 6:00 pm
Re: How did you guys complete the matplotlib exercise
The way I did it is generate random samples (say 1000) from the uniform distribution. Then I equally divided those points to 4 quadrants (250 points per quadrant in this case). We have to add/subtract 1 to the coordinates according to the quadrant. Now we have set of points in 4 quadrants. Each quadrant is given a target value (Quadrants 1 and 3 have target 0 and Quadrants 2 and 4 have target 1).
Re: How did you guys complete the matplotlib exercise
Here's my solution:
It seems to work well.
Code: Select all
# Import required libraries
import numpy as np
import matplotlib.pyplot as plt
# Make 1D arrays for x and y axes and spread them around 1 and 1
x1 = (np.random.random(2000)0.5)*2
x2 = (np.random.random(2000)0.5)*2
# Make an array for color picking
Y = np.zeros(2000)
for e in range(len(Y)):
if (x1[e] <= 0 and x2[e] > 0) or (x1[e] > 0 and x2[e] <= 0):
Y[e] = 1
# Plot it all
plt.xlabel('x1')
plt.ylabel('x2')
plt.title('Matplotlib Exercise')
plt.scatter(x1,x2,c=Y);
Re: How did you guys complete the matplotlib exercise
Hi amusensei,
I am sorry to hear you got stuck. I am currently stuck at the pandas exercise, so I know how you feel! I'll share my solution with you, hopefully it will benefit you. Good luck! If new questions arise, feel free to send me a DM (assuming that is possible) for further explanation.
Kind regards,
Martijn
I am sorry to hear you got stuck. I am currently stuck at the pandas exercise, so I know how you feel! I'll share my solution with you, hopefully it will benefit you. Good luck! If new questions arise, feel free to send me a DM (assuming that is possible) for further explanation.
Code: Select all
import numpy as np
import matplotlib.pyplot as plt
inputs = np.random.uniform(0,2,(2000,2))
inputs[:,0] = 1
inputs[:,1] = 1
inputs.shape
labels = []
for i in range(inputs.shape[0]):
if inputs[i,0] < 0 and inputs[i,1] <= 0:
labels.append(0)
elif inputs[i,0] < 0 and inputs[i,1] >= 0:
labels.append(1)
elif inputs[i,0] > 0 and inputs[i,1] <= 0:
labels.append(1)
else:
labels.append(0)
plt.scatter(inputs[:,0],inputs[:,1],c=labels, cmap='rainbow');
Martijn