In [1]:
%load_ext watermark

%watermark -a 'Vahid Mirjalili' -d -p numpy,numexpr,pandas,matplotlib,plotly -v


Vahid Mirjalili 15/12/2014 

CPython 2.7.3
IPython 2.3.1

numpy 1.9.1
numexpr 2.2.2
pandas 0.15.1
matplotlib 1.4.2
plotly 1.4.7

In [2]:
from matplotlib import pyplot as plt

import pandas as pd
import numpy as np
import scipy
import sklearn

%matplotlib inline

1. Clusters of Different Sizes with Uniform Density


In [4]:
d1 = np.random.uniform(low=-5, high=7, size=(3000,2))

d1_a = d1[d1[:,0]**2 + d1[:,1]**2 < 9,:]
d1_b = d1[(d1[:,0]+4.2)**2 + d1[:,1]**2 < 1,:]
d1_c = d1[(d1[:,0]-4.2)**2 + d1[:,1]**2 < 1,:]

print("Cluster sizes %s "%[d1_a.shape[0], d1_b.shape[0], d1_c.shape[0]])

d1 = np.concatenate((d1_a, d1_b, d1_c), axis=0)

d1.shape


####################################
fig1 = plt.figure(1, figsize=(10,6))
ax1 = fig1.add_subplot(1, 1, 1)

plt.scatter(d1_a[:,0], d1_a[:,1], color='b', marker='o', s=80, alpha=0.25)
plt.scatter(d1_b[:,0], d1_b[:,1], color='r', marker='<', s=80, alpha=0.25)
plt.scatter(d1_c[:,0], d1_c[:,1], color='g', marker='*', s=80, alpha=0.25)

plt.xlabel('$x_1$', size=20)
plt.ylabel('$x_2$', size=20)
plt.title('3 Different Sizes', size=20)
plt.xlim([-6, 6])
plt.ylim([-4, 4])

plt.setp(ax1.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax1.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()


Cluster sizes [594, 54, 69] 

2. Clusters with Different Densities


In [5]:
d2 = np.random.uniform(low=-3, high=3, size=(200,2))
d2_a = d2[d2[:,0]**2 + d2[:,1]**2 < 9,:]

d2 = np.random.uniform(low=-3, high=7, size=(5000,2))
d2_b = d2[(d2[:,0]-5)**2 + (d2[:,1]-1.5)**2 < 1,:]
d2_c = d2[(d2[:,0]-5)**2 + (d2[:,1]+1.5)**2 < 1,:]

print("Cluster sizes %s "%[d2_a.shape[0], d2_b.shape[0], d2_c.shape[0]])

d2 = np.concatenate((d2_a, d2_b, d2_c), axis=0)

d2.shape

############################
fig2 = plt.figure(2, figsize=(9,6))
ax2 = fig2.add_subplot(1, 1, 1)

plt.scatter(d2_a[:,0], d2_a[:,1], color='b', marker='o', s=80, alpha=0.25)
plt.scatter(d2_b[:,0], d2_b[:,1], color='r', marker='<', s=80, alpha=0.25)
plt.scatter(d2_c[:,0], d2_c[:,1], color='g', marker='*', s=80, alpha=0.25)

plt.xlabel('$x_1$', size=20)
plt.ylabel('$x_2$', size=20)
plt.title('Different Densities', size=20)
plt.xlim([-4, 7])
plt.ylim([-4, 4])

plt.setp(ax2.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax2.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()


Cluster sizes [170, 162, 172] 

3. Non-globular Shapes

3.a


In [8]:
d = np.random.uniform(low=-7, high=7, size=(5000,2))
d3_a = d[d[:,0]**2 + d[:,1]**2 < 9,:]
d3_a = d3_a[d3_a[:,0]**2 + d3_a[:,1]**2 > 4,:]
d3_a = d3_a[d3_a[:,1] < 0,:]

d3_b = d[(d[:,0]-2.5)**2 + (d[:,1]+1.5)**2 < 9,:]
d3_b = d3_b[(d3_b[:,0]-2.5)**2 + (d3_b[:,1]+1.5)**2 > 4,:]
d3_b = d3_b[d3_b[:,1] > -1.5,:]

print("Cluster sizes %s "%[d3_a.shape[0], d3_b.shape[0]])

d3 = np.concatenate((d3_a, d3_b), axis=0)

d3.shape

###############################
fig3 = plt.figure(3, figsize=(9,6))
ax3 = fig3.add_subplot(1, 1, 1)

plt.scatter(d3_a[:,0], d3_a[:,1], color='b', marker='o', s=80, alpha=0.25)
plt.scatter(d3_b[:,0], d3_b[:,1], color='r', marker='<', s=80, alpha=0.25)
#plt.scatter(d3_c[:,0], d3_c[:,1], color='g', marker='*', s=80, alpha=0.25)

plt.xlabel('$x_1$', size=20)
plt.ylabel('$x_2$', size=20)
plt.title('Non-globular Shapes', size=20)
plt.xlim([-4, 6])
plt.ylim([-4, 2])

plt.setp(ax3.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax3.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()


Cluster sizes [205, 192] 

3.B Taoist Symbol


In [42]:
sep_dist = 0.4

d = np.random.uniform(low=-5, high=5, size=(5000,2))
d = d[d[:,0]**2 + d[:,1]**2 < 16,:]

da = d[d[:,0] < 0]
db = d[d[:,0] > 0]

dsa = db[db[:,0]**2 + (db[:,1]+2.1)**2 < 4-sep_dist/2, :]
dsb = da[da[:,0]**2 + (da[:,1]-2.1)**2 < 4-sep_dist/2, :]

d3a = da[da[:,0]**2 + (da[:,1]-2.1)**2 > 4 + sep_dist/2,:]
d3b = db[db[:,0]**2 + (db[:,1]+2.1)**2 > 4 + sep_dist/2,:]

d3a = np.concatenate((d3a, dsa), axis=0)
d3b = np.concatenate((d3b, dsb), axis=0)

d3a[:,0] = d3a[:,0] - sep_dist/2
d3b[:,0] = d3b[:,0] + sep_dist/2
#print(d3b.shape)

print("Cluster sizes %s "%[d3a.shape[0], d3b.shape[0]])

#################################
fig3b = plt.figure(4, figsize=(8,7))
ax3b = fig3b.add_subplot(1, 1, 1)

plt.scatter(d3a[:,0], d3a[:,1], color='b', marker='o', s=80, alpha=0.25)
plt.scatter(d3b[:,0], d3b[:,1], color='r', marker='<', s=80, alpha=0.25)
#plt.scatter(d3_c[:,0], d3_c[:,1], color='g', marker='*', s=80, alpha=0.25)

plt.xlabel('$x_1$', size=20)
plt.ylabel('$x_2$', size=20)
plt.title('Non-globular Shapes - Taoist Symbol', size=20)
plt.xlim([-5, 5])
plt.ylim([-5, 5])

plt.setp(ax3b.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax3b.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()


Cluster sizes [1245, 1210] 

3.C Concentric Circles


In [49]:
d = np.random.uniform(low=-6, high=6, size=(2000,2))

d3a = d[(d[:,0]**2 + d[:,1]**2 <36) & (d[:,0]**2 + d[:,1]**2 >25), :]

d = np.random.uniform(low=-4, high=4, size=(2000,2))

d3b = d[(d[:,0]**2 + d[:,1]**2 <9) & (d[:,0]**2 + d[:,1]**2 > 4),:]
d3c = d[(d[:,0]**2 + d[:,1]**2 <=1), :]

print("Cluster sizes %s "%[d3a.shape[0], d3b.shape[0], d3c.shape[0]])

#################################
fig3c = plt.figure(5, figsize=(8,7))
ax3c = fig3c.add_subplot(1, 1, 1)

plt.scatter(d3a[:,0], d3a[:,1], color='b', marker='o', s=80, alpha=0.25)
plt.scatter(d3b[:,0], d3b[:,1], color='r', marker='<', s=80, alpha=0.25)
plt.scatter(d3c[:,0], d3c[:,1], color='g', marker='*', s=80, alpha=0.25)

plt.xlabel('$x_1$', size=20)
plt.ylabel('$x_2$', size=20)
plt.title('Non-globular Shapes - Concentric Circles', size=20)
plt.xlim([-6.5, 6.5])
plt.ylim([-6.5, 6.5])

plt.setp(ax3c.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax3c.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()


Cluster sizes [499, 506, 98] 

4. Archimedean Spiral $r = θ$


In [64]:
pi = np.pi
theta = np.arange(pi/2, pi*3, pi/40)

rad = theta

x1 = rad * np.cos(theta)
y1 = rad * np.sin(theta)

x2 = rad * np.cos(theta + pi/2)
y2 = rad * np.sin(theta + pi/2)

x3 = rad * np.cos(theta + pi)
y3 = rad * np.sin(theta + pi)

x4 = rad * np.cos(theta + pi*3/2)
y4 = rad * np.sin(theta + pi*3/2)

#################################
fig4 = plt.figure(6, figsize=(8,7))
ax4 = fig4.add_subplot(1, 1, 1)

plt.scatter(x1, y1, color='b', marker='o', s=80, alpha=0.35)
plt.scatter(x2, y2, color='r', marker='<', s=80, alpha=0.35)
plt.scatter(x3, y3, color='g', marker='*', s=80, alpha=0.35)
plt.scatter(x4, y4, color='orange', marker='>', s=80, alpha=0.35)


plt.xlabel('$x_1$', size=20)
plt.ylabel('$x_2$', size=20)
plt.title('Archimedean Spiral', size=20)
plt.xlim([-10, 10])
plt.ylim([-10, 10])

plt.setp(ax4.get_xticklabels(), rotation='horizontal', fontsize=16)
plt.setp(ax4.get_yticklabels(), rotation='horizontal', fontsize=16)

plt.show()



In [ ]: