## Matplotlib exercise

Godzilla
Posts: 1
Joined: Tue Feb 16, 2021 12:28 am

### Re: Matplotlib exercise

argho33 wrote: Tue Aug 18, 2020 7:41 am Found a really easy 4 lines of code solution to this problem.

Code: Select all

``````import numpy as np
import matplotlib.pyplot as plt

#Generate random 2D array with 2000 elements and dimensionality of 2 and values between -1 to 1
x = np.random.uniform(-1, 1, size=(2000,2))

#Generate a new 2D array of just 0's and 1's using the previous random 2D array. If the random value is previous 2D array is Positive then store 1 ,
# if the random value is negative then store 0
xx = np.where(x[:,:]<0, 0,1)

#Perform the XOR check for each of the 2000 elements by checking whether column 0 and column 1 of the new 2D array is not equal and store a 1 in y when not equal
y = np.where(xx[:,0] != xx[:,1],1,0)

#Plot the scatter plot
plt.scatter(x[:,0],x[:,1],c=y);``````

This is a really elegant solution. I did mine with an IF statement first, but I like how clean this looks. Out of curiosity I ran it as a speed comparison, just to see if numpy.where was any faster, but to my surprise it was actually slower. Not insanely slower, but i figured it might be faster based on the numpy exercise.

For 25,000 elements, standard loop/for is ~0.047323 seconds, and numpy.where is 5.034207 seconds

Code: Select all

``````import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt

A = np.random.uniform(-1,1, size=(25000,25000))

t0 = datetime.now()
B = np.zeros(25000)
for i in range(len(B)):
if A[i,0] > 0 and A[i,1] > 0:
B[i]=0;
elif A[i,0] < 0 and A[i,1] > 0:
B[i]=1;
elif A[i,0] < 0 and A[i,1] < 0:
B[i]=0;
else:
B[i]=1;
dt1 = datetime.now() - t0

t0 = datetime.now()
#Generate a new 2D array of just 0's and 1's using the previous random 2D array. If the random value is previous 2D array is Positive then store 1 ,
# if the random value is negative then store 0
xx = np.where(A[:,:]<0, 0,1)

#Perform the XOR check for each of the 2000 elements by checking whether column 0 and column 1 of the new 2D array is not equal and store a 1 in y when not equal
y = np.where(xx[:,0] != xx[:,1],1,0)
dt2 = datetime.now() - t0

plt.scatter(A[:,0],A[:,1], c=B)
plt.show()

print("Time for method 1:", dt1.total_seconds())
print("Time for method 2:", dt2.total_seconds())

``````
thuat
Posts: 2
Joined: Fri Mar 05, 2021 12:36 pm

### Re: Matplotlib exercise

This is my code for the Matplot exercise:

import numpy as np
import matplotlib.pyplot as plt

X = np.random.uniform(-1, 1, size=(2000,2))
Y = np.zeros(2000)
Y = (X[:,0] < 0) * (X[:,1] > 0)
Y |= (X[:,0] > 0) * (X[:,1] < 0)
plt.scatter(X[:,0], X[:,1], c=Y);
BabaKirtos
Posts: 5
Joined: Sat Apr 03, 2021 1:00 pm

### Re: Matplotlib exercise

Similar to Efortier's method, but number of datapoints can be changed.

Code: Select all

``````# DLP - Matplotlib - Exercise

# Generate an plot the XOR datasheet

import numpy as np
import matplotlib.pyplot as plt

datapoints = 1500

x = np.random.rand(datapoints,2)*2 - 1 # generating and shifting numbers

l = np.zeros(datapoints) # generating labels

for n in range(datapoints):
if ((x[n][0] < 0 and x[n][1] > 0) or (x[n][0] > 0 and x[n][1] < 0)):
l[n] = 1

plt.title('XOR Datasheet')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.scatter(x[:,0],x[:,1],c=l);
``````
BabaKirtos
Posts: 5
Joined: Sat Apr 03, 2021 1:00 pm

### Re: Matplotlib exercise

Godzilla wrote: Tue Feb 16, 2021 12:38 am
argho33 wrote: Tue Aug 18, 2020 7:41 am Found a really easy 4 lines of code solution to this problem.

Code: Select all

``````import numpy as np
import matplotlib.pyplot as plt

#Generate random 2D array with 2000 elements and dimensionality of 2 and values between -1 to 1
x = np.random.uniform(-1, 1, size=(2000,2))

#Generate a new 2D array of just 0's and 1's using the previous random 2D array. If the random value is previous 2D array is Positive then store 1 ,
# if the random value is negative then store 0
xx = np.where(x[:,:]<0, 0,1)

#Perform the XOR check for each of the 2000 elements by checking whether column 0 and column 1 of the new 2D array is not equal and store a 1 in y when not equal
y = np.where(xx[:,0] != xx[:,1],1,0)

#Plot the scatter plot
plt.scatter(x[:,0],x[:,1],c=y);``````

This is a really elegant solution. I did mine with an IF statement first, but I like how clean this looks. Out of curiosity I ran it as a speed comparison, just to see if numpy.where was any faster, but to my surprise it was actually slower. Not insanely slower, but i figured it might be faster based on the numpy exercise.

For 25,000 elements, standard loop/for is ~0.047323 seconds, and numpy.where is 5.034207 seconds

Code: Select all

``````import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt

A = np.random.uniform(-1,1, size=(25000,25000))

t0 = datetime.now()
B = np.zeros(25000)
for i in range(len(B)):
if A[i,0] > 0 and A[i,1] > 0:
B[i]=0;
elif A[i,0] < 0 and A[i,1] > 0:
B[i]=1;
elif A[i,0] < 0 and A[i,1] < 0:
B[i]=0;
else:
B[i]=1;
dt1 = datetime.now() - t0

t0 = datetime.now()
#Generate a new 2D array of just 0's and 1's using the previous random 2D array. If the random value is previous 2D array is Positive then store 1 ,
# if the random value is negative then store 0
xx = np.where(A[:,:]<0, 0,1)

#Perform the XOR check for each of the 2000 elements by checking whether column 0 and column 1 of the new 2D array is not equal and store a 1 in y when not equal
y = np.where(xx[:,0] != xx[:,1],1,0)
dt2 = datetime.now() - t0

plt.scatter(A[:,0],A[:,1], c=B)
plt.show()

print("Time for method 1:", dt1.total_seconds())
print("Time for method 2:", dt2.total_seconds())

``````
Thanks for running the speed tests, its interesting to see standard loop beat numpy where function, I would have read more on what exactly the function does