@@ -0,0 +1,2659 @@
+def get_sunrise_sunset():%0A    month_length = %5B31,28,31,30,31,30,31,31,30,31,30,31%5D%0A%0A    with open('dataset/sunrise_sunset.txt') as f:%0A        content = f.readlines()%0A%0A        date = %7B%7D%0A%0A        for (i, month) in enumerate(month_length):%0A            for day in range(0, month):%0A%0A                if i %3E= 9:%0A                    m = str(i+1)%0A                else:%0A                    m = '0' + str(i+1)%0A%0A                if day %3E= 9:%0A                    d = str(day+1)%0A                else:%0A                    d = '0' + str(day+1)%0A%0A                line = content%5Bday%5D%0A                line = line%5B4:%5D%0A%0A                size = 11%0A                sunrise = line%5Bi*size:i*size+4%5D%0A                sunset = line%5Bi*size+5:i*size+9%5D%0A                date%5B'2009-'+m+'-'+d%5D = %5Bsunrise, sunset%5D%0A%0A    return date%0A%0Adef get_changes(feature):%0A    import numpy as np%0A    new_feature = np.zeros(len(feature))%0A    for idx, val in enumerate(feature):%0A        if idx == 0:%0A            new_feature%5Bidx%5D = 0%0A        else:%0A            new_feature%5Bidx%5D = feature%5Bidx%5D - feature%5Bidx-1%5D%0A%0A    return new_feature%0A%0Aimport numpy as np%0Afrom random import choice%0A%0A%0Adef SMOTE(T, N, k):%0A    %22%22%22%0A    Returns (N/100) * n_minority_samples synthetic minority samples.%0A%0A    Parameters%0A    ----------%0A    T : array-like, shape = %5Bn_minority_samples, n_features%5D%0A        Holds the minority samples%0A    N : percetange of new synthetic samples:%0A        n_synthetic_samples = N/100 * n_minority_samples. Can be %3C 100.%0A    k : int. Number of nearest neighbours.%0A%0A    Returns%0A    -------%0A    S : array, shape = %5B(N/100) * n_minority_samples, n_features%5D%0A    %22%22%22%0A    n_minority_samples, n_features = T.shape%0A%0A    if N %3C 100:%0A        #create synthetic samples only for a subset of T.%0A        #TODO: select random minortiy samples%0A        N = 100%0A        pass%0A%0A    if (N %25 100) != 0:%0A        raise ValueError(%22N must be %3C 100 or multiple of 100%22)%0A%0A    N = N/100%0A    n_synthetic_samples = N * n_minority_samples%0A    S = np.zeros(shape=(n_synthetic_samples, n_features))%0A%0A    from sklearn.neighbors import NearestNeighbors%0A    #Learn nearest neighbours%0A    neigh = NearestNeighbors(n_neighbors = k)%0A    neigh.fit(T)%0A%0A    #Calculate synthetic samples%0A    for i in xrange(n_minority_samples):%0A        nn = neigh.kneighbors(T%5Bi%5D.reshape(1, T.shape%5B1%5D), return_distance=False)%0A        for n in xrange(N):%0A            nn_index = choice(nn%5B0%5D)%0A            #NOTE: nn includes T%5Bi%5D, we don't want to select it%0A            while nn_index == i:%0A                nn_index = choice(nn%5B0%5D)%0A%0A            dif = T%5Bnn_index%5D - T%5Bi%5D%0A            gap = np.random.random()%0A            S%5Bn + i * N, :%5D = T%5Bi,:%5D + gap * dif%5B:%5D%0A%0A    return S

Add Support Files