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())
```