hello_world.py 1.62 KB
Newer Older
Xu Jitao's avatar
Xu Jitao committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
def generate_checkers(nb_app, nb_test, cov=[[0.25,0],[0,0.25]]):
    print(cov)
    xapp=np.ndarray((nb_app*16,2))
    yapp=np.ndarray(nb_app*16)
    xtest=np.ndarray((nb_test*16,2))
    ytest=np.ndarray(nb_test*16)

    y=1
    index_app=0
    index_test=0
    for i in range(0,4):
        for j in range(0,4):
            center = [i,j]
            y = 1 if (i+j) % 2 else -1
            xapp[index_app:index_app+nb_app] = np.random.multivariate_normal(center,cov,nb_app)
            yapp[index_app:index_app+nb_app] = np.ones(nb_app)*y
            
            xtest[index_test:index_test+nb_test] = np.random.multivariate_normal(center,cov,nb_test)
            ytest[index_test:index_test+nb_test] = np.ones(nb_test)*y

            index_test += nb_test
            index_app += nb_app
          
    return xapp, yapp, xtest, ytest

def generate_linear_separable_case(nb_app,nb_test):
    from sklearn.datasets import make_blobs
    x, y = make_blobs(n_samples=nb_app+nb_test, centers=2, n_features=2)
    x_app = x[0:nb_app,:]
    y_app = y[0:nb_app].astype(np.int64)
    
    x_test = x[nb_app:,:]
    y_test = y[nb_app:]
    
    y_app[y_app == 0] = -1
    y_test[y_test == 0 ] = -1
    return x_app, y_app, x_test, y_test

def generate_xor(nb_app,nb_test):
    np.random.seed(0)
    x_app = np.random.randn(nb_app, 2)
    y_app = np.logical_xor(x_app[:, 0] > 0, x_app[:, 1] > 0).astype(int)
    y_app[y_app == 0] = -1
    
    np.random.seed(0)
    x_test = np.random.randn(nb_test, 2)
    y_test = np.logical_xor(x_test[:, 0] > 0, x_test[:, 1] > 0).astype(int)
    y_test[y_test == 0] = -1
    
    return x_app,y_app,x_test,y_test