numpyでの人工的データ生成



csv形式での出力、scatterplotができます。
c_,r_演算子を使っています。

作れる人工的データ
円環とその組み合わせ(円環の中の円環(2D),xor的パターン(2D),絡み合った円環(3D))
ツイストされ絡み合った円環(3D)
任意の次元の球殻(球殻の中の球殻)
Lorenzアトラクタ
Rosslerアトラクタ

参考
Marsaglia's method
http://stackoverflow.com/questions/15880367/python-uniform-distribution-of-points-on-4-dimensional-sphere


```py:gendata.py
# -*- coding: utf-8 -*-
"""
Created on Wed May 07 21:17:21 2014

@author: xiangze
"""

import csv
import numpy as np
#from matplotlib.pyplot import *
import matplotlib.pyplot as plt

PI=np.pi
PI2=2*PI

def gencircle(rc,rr=0.1,offset=[0,0],num=100,label=0):
    c=[]
    for i in range(num):
        r=rc+np.random.uniform(-rr,rr,1)
        th=np.random.uniform(0,PI2,1)
        c.append([r*np.sin(th)+offset[0],r*np.cos(th)+offset[1]])
    return np.c_[np.array(c).reshape(num,2),np.repeat(label,num)]
    
    
def genring(rc,rr=0.1,offset=[0,0,0],num=100,label=0,normaldir='x'):
    if(normaldir=='x'):
        a=gencircle(rc,rr,[offset[1],offset[2]],num,label)    
        return np.c_[np.repeat(offset[0],num),a[:,0],a[:,1],a[:,2]]
    elif(normaldir=='y'):
        a=gencircle(rc,rr,[offset[0],offset[2]],num,label)    
        return np.c_[a[:,0],np.repeat(offset[1],num),a[:,1],a[:,2]]
    else:
        a=gencircle(rc,rr,[offset[0],offset[1]],num,label)    
        return np.c_[a[:,0],a[:,1],np.repeat(offset[2],num),a[:,2]]
        
def gentwistedring0(rc=[1,0.3],rr=0.1,offset=[0,0,0],num=100,label=0,twistratio=3.0,phase=0):
    c=[]
    for i in range(num):
        r=rc[0]+np.random.uniform(-rr,rr,1)
        th=np.random.uniform(0,PI2,1)
        c1=[r*np.sin(th)+offset[0],r*np.cos(th)+offset[1],offset[2]]
        c2=[rc[1]*np.sin(th*twistratio+phase)*np.sin(th) , rc[1]*np.sin(th*twistratio+phase)*np.cos(th) ,rc[1]*np.cos(th*twistratio+phase)]

        c.append([c1[i]+c2[i] for i in range(len(c1))])
    return np.c_[np.array(c).reshape(num,3),np.repeat(label,num)]

    
def gentwistedring(rc=[1,0.3],rr=0.1,offset=[0,0,0],num=100,label=0,normaldir='x',twistratio=5.0,phase=0):
    a=gentwistedring0(rc,rr,offset,num,label,twistratio,phase)    
    if(normaldir=='x'):
        return a
    elif(normaldir=='y'):
        return np.c_[a[:,1],a[:,2],a[:0],a[:3]]
    else:
        return np.c_[a[:,2],a[:,0],a[:1],a[:3]]
    
#http://stackoverflow.com/questions/15880367/python-uniform-distribution-of-points-on-4-dimensional-sphere
#Marsaglia's method
def gensphere(rc,rr=0.1,offset=[0,0,0],num=100,label=0,dim=3):
    normal_deviates = np.random.normal(size=(dim, num))
    r=rc+np.random.uniform(-rr,rr,1)
    r = np.sqrt((normal_deviates**2).sum(axis=0))*r
    p =normal_deviates/r
    return np.c_[np.array(zip(*p)).reshape(num,dim),np.repeat(label,num)]

def gensphere0(rc,rr=0.1,offset=[0,0,0],num=100,label=0):
    c=[]
    n=int(np.sqrt(num))
    for ph in np.random.uniform(-PI,PI,n):
        for th in np.random.uniform(0,PI2,n):
            r=rc+np.random.uniform(-rr,rr,1)
            c.append([r*np.sin(th)*np.sin(ph)+offset[0],r*np.cos(th)*np.sin(ph)+offset[1],r*np.cos(ph)+offset[2]])
    return np.c_[np.array(c).reshape(num,3),np.repeat(label,num)]

def gensphere1(rc,rr=0.1,offset=[0,0,0],num=100,label=0):
    c=[]
    n=int(np.sqrt(num))
    for ph in np.random.uniform(-PI,PI,n):
        p=0
        if(p>=n):
            break
        else:
            m=int(np.abs(np.sin(ph)*n))
            if(m!=0):
                for th in np.random.uniform(0,PI2,m):
                    r=rc+np.random.uniform(-rr,rr,1)
                    c.append((r*np.sin(th)*np.sin(ph)+offset[0],r*np.cos(th)*np.sin(ph)+offset[1],r*np.cos(ph)+offset[2]))
                p=p+m
    l=len(c)
    return np.c_[np.array(c).reshape(l,3),np.repeat(label,l)]

def genlorenz(init=[0,0.1,0],offset=[0,0,0],rr=0.,num=100,p=10,r=28,b=2.66,label=0,dt=0.01):
    cc=[]
    x=init[0]
    y=init[1]
    z=init[2]
    for t in range(num):
        cc.append([x,y,z])
        x=x+dt*(-p*x+p*y)      +np.random.uniform(-rr,rr,1)
        y=y+dt*(-x*z+r*x-y)    +np.random.uniform(-rr,rr,1)
        z=z+dt*( x*y-b*z)      +np.random.uniform(-rr,rr,1)
    return np.c_[np.array(cc).reshape(num,3),np.repeat(label,num)]

def genrossler(init=[0,5,0],offset=[0,0,0],num=100,a=0.2,b=0.2,c=5.7,label=0,dt=0.05):
    cc=[]
    x=init[0]
    y=init[1]
    z=init[2]
    for t in range(num):
        cc.append([x,y,z])
        x=x+dt*(-y-z)
        y=y+dt*( x+a*y)
        z=z+dt*( b+z*(x-c))
    return np.c_[np.array(cc).reshape(num,3),np.repeat(label,num)]


def cshow2(data):
    cc=zip(*data)
    plt.scatter(cc[0],cc[1],c=cc[2])
    plt.draw()
    plt.show()

def cshow3(data):
    from mpl_toolkits.mplot3d import Axes3D
    fig=plt.figure()
    ax = Axes3D(fig)
    cc=zip(*data)
    ax.scatter(cc[0],cc[1],cc[2],c=cc[3])
    plt.draw()
    plt.show()

def test(data,dump=False,fname="test.csv"):
    if(data.shape[1]==3):
        cshow2(data)
    else:
        cshow3(data)
        
    if(dump):
        np.savetxt(fname,data,delimiter=",")

if __name__=="__main__":
    num=200
    circles=np.vstack([gencircle(1,0.1,num=num,label=0),gencircle(1,0.1,[-2,2],num=num,label=1)])
    test(circles)

#circle in circle
    cinc=np.r_[gencircle(1,0.1,num=num,label=0),gencircle(2,0.1,num=num,label=1)]
    test(cinc)

#XOR-like pattern
    xor0=np.r_[gencircle(0.5,num=num/2,offset=[0,0],label=0),gencircle(0.5,offset=[1,1],label=0)]
    xor1=np.r_[gencircle(0.5,num=num/2,offset=[0,1],label=1),gencircle(0.5,offset=[1,0],label=1)]
    xor=np.r_[xor0,xor1]    
    test(xor)

#3D ring
    rings=np.r_[genring(1,0.1,num=num,offset=[0,0,0],label=0,normaldir='x'),\
                genring(1,0.1,num=num,offset=[0,0,1],label=1,normaldir='y')]
    test(rings)   

    num=400
#sphere in sphere    
    sins=np.r_[gensphere(1,num=num,label=0),gensphere(2,num=num,label=1)]
    test(sins)
    
#twisted rings
    test(np.vstack([gentwistedring(num=num,label=0),gentwistedring(num=num,label=1,phase=PI)]))

    num=1000
    rossler=genrossler(num=num,dt=0.1)
    test(rossler)

    lorenz=genlorenz(num=num,dt=0.05)
    test(lorenz)
```

![circles.png](https://qiita-image-store.s3.amazonaws.com/0/12005/5bff4cf2-5932-d4c9-7535-1bdc67dda2a9.png)
![cinc.png](https://qiita-image-store.s3.amazonaws.com/0/12005/a25c2ca5-9434-9eee-98d9-ef3bc48b3b5b.png)
![xor_like.png](https://qiita-image-store.s3.amazonaws.com/0/12005/1764c8cc-9fd6-1902-c384-3486fa5eec8e.png)
![rings.png](https://qiita-image-store.s3.amazonaws.com/0/12005/f3210c52-7c84-4576-2871-eed778f064d1.png)
![sphere_in_sphere.png](https://qiita-image-store.s3.amazonaws.com/0/12005/15931798-8f69-0453-5d2d-25364eb7b78f.png)

![twistedrings.png](https://qiita-image-store.s3.amazonaws.com/0/12005/0bced8dc-c60a-48b2-8f32-a4e3aeb81ecc.png)
![rossler.png](https://qiita-image-store.s3.amazonaws.com/0/12005/d44675c9-bb6a-fb2f-a015-88b184d409b6.png)

![lorenz.png](https://qiita-image-store.s3.amazonaws.com/0/12005/044706b8-af0f-30f1-93c2-abc4518a0122.png)