示例#1
0
def run_ica_EM(data, principal_components=1):
    clf = FastICA(n_components=principal_components)
    data = clf.fit_transform(data)

    scores = []
    for i in range(1, len(data[0]) * 2):
        print(f'EM and ICA: with K = {i}')
        clf = GaussianMixture(n_components=i)
        clf.fit(data)
        scores.append(clf.score(data))

    return scores
epoch = 0
while epoch < N_EPOCHS:
    print('epoch: ', epoch)
    # SHUFFLING
    random_perm = np.random.permutation(X_train.shape[0])
    mini_batch_index = 0
    while True:
        # MINI-BATCH
        indices = random_perm[mini_batch_index:mini_batch_index + N_BATCH]
        clf.partial_fit(X_train[indices], y_train[indices], classes=N_CLASSES)
        mini_batch_index += N_BATCH

        if mini_batch_index >= N_TRAIN_SAMPLES:
            break

    # SCORE TRAIN
    scores_train.append(clf.score(X_train, y_train))

    # SCORE TEST
    scores_test.append(clf.score(X_test, y_test))

    epoch += 1
plt.figure()
plt.plot(scores_train, color='b', alpha=0.8, label='Train')
plt.plot(scores_test, color='r', alpha=0.8, label='Test')
plt.title("Credit Cards Neural Network Accuracy over epochs", fontsize=14)
plt.xlabel('Epochs')
plt.ylabel("Accuracy")
plt.legend(loc='upper left')
plt.savefig('charts/creditcards.neuralnetwork.ICA.accuracy.png')