Esempio n. 1
0
def knn(wine_train, wine_test, heart_train, heart_test):
    wine_train_scores = []
    heart_train_scores = []
    wine_test_scores = []
    heart_test_scores = []
    for k in tqdm(range(1, 19, 2)):
        heart_knn = alg.NearestNeighbors(k)
        heart_knn.train(heart_train)
        accuracy = heart_knn.score(heart_train)
        heart_train_scores.append({'k': k, 'accuracy': accuracy})
        accuracy = heart_knn.score(heart_test)
        heart_test_scores.append({'k': k, 'accuracy': accuracy})

        wine_knn = alg.NearestNeighbors(k)
        wine_knn.train(wine_train)
        accuracy = wine_knn.score(wine_train)
        wine_train_scores.append({'k': k, 'accuracy': accuracy})
        accuracy = wine_knn.score(wine_test)
        wine_test_scores.append({'k': k, 'accuracy': accuracy})

    heart_train_results = pd.DataFrame.from_records(heart_train_scores)
    wine_train_results = pd.DataFrame.from_records(wine_train_scores)

    heart_test_results = pd.DataFrame.from_records(heart_test_scores)
    wine_test_results = pd.DataFrame.from_records(wine_test_scores)

    colors = alg.ColorPopper()
    ax = heart_train_results.plot.scatter(x='k',
                                          y='accuracy',
                                          color=colors.pop(),
                                          label='train')
    heart_test_results.plot.scatter(x='k',
                                    y='accuracy',
                                    ax=ax,
                                    color=colors.pop(),
                                    label='test')
    plt.ylim(bottom=0, top=1)
    plt.grid()
    plt.title('Heart Failure KNN Train/Test Accurarcy')
    plt.legend(loc='lower left')
    plt.savefig('./knn_heart.png')

    colors = alg.ColorPopper()
    ax = wine_train_results.plot.scatter(x='k',
                                         y='accuracy',
                                         color=colors.pop(),
                                         label='train')
    wine_test_results.plot.scatter(x='k',
                                   y='accuracy',
                                   ax=ax,
                                   color=colors.pop(),
                                   label='test')
    plt.ylim(bottom=0, top=1)
    plt.grid()
    plt.title('Wine Quality KNN Train/Test Accurarcy')
    plt.legend(loc='lower left')
    plt.savefig('./knn_wine.png')
Esempio n. 2
0
def boosting(wine_train, wine_test, heart_train, heart_test):
    wine_train_scores = []
    heart_train_scores = []
    wine_test_scores = []
    heart_test_scores = []
    for alpha in np.linspace(0, 0.05, 20):
        heart_dt = alg.Boosting(alpha)
        heart_dt.train(heart_train)
        accuracy = heart_dt.score(heart_train)
        heart_train_scores.append({'alpha': alpha, 'accuracy': accuracy})
        accuracy = heart_dt.score(heart_test)
        heart_test_scores.append({'alpha': alpha, 'accuracy': accuracy})

        wine_dt = alg.Boosting(alpha)
        wine_dt.train(wine_train)
        accuracy = wine_dt.score(wine_train)
        wine_train_scores.append({'alpha': alpha, 'accuracy': accuracy})
        accuracy = wine_dt.score(wine_test)
        wine_test_scores.append({'alpha': alpha, 'accuracy': accuracy})

    heart_train_results = pd.DataFrame.from_records(heart_train_scores)
    wine_train_results = pd.DataFrame.from_records(wine_train_scores)

    heart_test_results = pd.DataFrame.from_records(heart_test_scores)
    wine_test_results = pd.DataFrame.from_records(wine_test_scores)

    colors = alg.ColorPopper()
    ax = heart_train_results.plot.scatter(x='alpha',
                                          y='accuracy',
                                          color=colors.pop(),
                                          label='train')
    heart_test_results.plot.scatter(x='alpha',
                                    y='accuracy',
                                    ax=ax,
                                    color=colors.pop(),
                                    label='test')
    plt.ylim(bottom=0, top=1)
    plt.grid()
    plt.title('Heart Failure Random Forest Train/Test Accurarcy')
    plt.legend(loc='lower left')
    plt.savefig('./boost_heart.png')

    colors = alg.ColorPopper()
    ax = wine_train_results.plot.scatter(x='alpha',
                                         y='accuracy',
                                         color=colors.pop(),
                                         label='train')
    wine_test_results.plot.scatter(x='alpha',
                                   y='accuracy',
                                   ax=ax,
                                   color=colors.pop(),
                                   label='test')
    plt.ylim(bottom=0, top=1)
    plt.grid()
    plt.title('Wine Quality Random Forest Train/Test Accurarcy')
    plt.legend(loc='lower left')
    plt.savefig('./boost_wine.png')
Esempio n. 3
0
def svm(wine_train, wine_test, heart_train, heart_test):
    wine_train_scores = []
    heart_train_scores = []
    wine_test_scores = []
    heart_test_scores = []
    for kernel in tqdm(['linear', 'poly', 'rbf']):
        heart_svm = alg.SupportVectorMachine(kernel)
        heart_svm.train(heart_train)
        accuracy = heart_svm.score(heart_train)
        heart_train_scores.append({'kernel': kernel, 'accuracy': accuracy})
        accuracy = heart_svm.score(heart_test)
        heart_test_scores.append({'kernel': kernel, 'accuracy': accuracy})

        wine_svm = alg.SupportVectorMachine(kernel)
        wine_svm.train(wine_train)
        accuracy = wine_svm.score(wine_train)
        wine_train_scores.append({'kernel': kernel, 'accuracy': accuracy})
        accuracy = wine_svm.score(wine_test)
        wine_test_scores.append({'kernel': kernel, 'accuracy': accuracy})

    heart_train_results = pd.DataFrame.from_records(heart_train_scores)
    wine_train_results = pd.DataFrame.from_records(wine_train_scores)

    heart_test_results = pd.DataFrame.from_records(heart_test_scores)
    wine_test_results = pd.DataFrame.from_records(wine_test_scores)

    colors = alg.ColorPopper()
    ax = heart_train_results.plot.bar(x='kernel',
                                      y='accuracy',
                                      color=colors.pop(),
                                      label='train')
    heart_test_results.plot.bar(x='kernel',
                                y='accuracy',
                                ax=ax,
                                color=colors.pop(),
                                label='test')
    plt.ylim(bottom=0, top=1)
    plt.grid()
    plt.title('Heart Failure SVM Train/Test Accurarcy')
    plt.legend(loc='lower left')
    plt.savefig('./svm_heart.png')

    colors = alg.ColorPopper()
    ax = wine_train_results.plot.bar(x='kernel',
                                     y='accuracy',
                                     color=colors.pop(),
                                     label='train')
    wine_test_results.plot.bar(x='kernel',
                               y='accuracy',
                               ax=ax,
                               color=colors.pop(),
                               label='test')
    plt.ylim(bottom=0, top=1)
    plt.grid()
    plt.title('Wine Quality SVM Train/Test Accurarcy')
    plt.legend(loc='lower left')
    plt.savefig('./svm_wine.png')
Esempio n. 4
0
 def nn_plot(df):
     colors = alg.ColorPopper()
     fig, ax = plt.subplots()
     for key, grp in df.groupby(['hidden_layers']):
         ax = grp.plot.scatter(ax=ax,
                               x='hidden_size',
                               y='accuracy',
                               label=f'{key} hidden layers',
                               color=colors.pop())
     plt.legend(loc='lower left')
     plt.show()