Generate a Dataset distributed in three Interleaving half circle shape in Python

401 views Asked by At

I have been given a task to generate a dataset that is shaped in three interleaving half circles.I have generated 2 half circles using make_moons() function available in "sklearn" library but cant figure out how to make three such half circles.enter image description here

Code:

X, y=make_moons(n_samples=(300,300),noise=0.1)
df=pd.DataFrame(dict(x=X[:,0], y=X[:,1],label=y ))
colors={0:'red', 1:'blue',2:'green'}
fig,ax=plt.subplots()
grouped=df.groupby('label')
for key, group in grouped:
  group.plot(ax=ax,kind='scatter',x='x',y='y',label=key,color=colors[key
plt.show()

I tried to increase the dimension in sample size but it gives an error that "n_samples can be either an int or a two-element tuple."

1

There are 1 answers

0
warped On

here is a function that makes a dataset with an arbitrary number of moons. You can specify vertical shift with the y_shift parameter.

def make_many_moons(
    number_of_moons,
    sigma, 
    radius, 
    number_of_datapoints,
    y_shift = 0.3
):
    
    moons = []
    for y in range(number_of_moons):
        q = np.random.uniform(0,np.pi,size=number_of_datapoints)
        
        if y % 2 == 0:
            factor = 1
        else: 
            factor = -1
        
        moon = np.zeros((number_of_datapoints, 3))
        moon[:,0] = (radius * np.cos(q)) + y
        moon[:,1] = (radius * np.sin(q) * factor) + (factor == -1) * y_shift
        moon[:,2] = y
        moons.append(moon)
        noise = np.random.normal(0, sigma, size=moon[:,:2].shape)
        moon[:,:2] += noise
    moons = np.concatenate(moons)
    return moons[:,:2], moons[:,2]


X, y = make_many_moons(
    number_of_moons=5,
    sigma=0.1, 
    radius=1, 
    number_of_datapoints=100,
    y_shift = 0.3)


plt.scatter(X[:,0], X[:,1], c=y)

enter image description here