委员会查询绘制性能图

时间:2019-03-07 10:56:42

标签: python

当我尝试绘制性能历史记录图时。图形中仅显示第一次迭代的一个值,然后程序终止。线

performance_history.append(committee.score(voice.drop('label',axis=1), voice['label']))

由于没有附加精确度,因此必须有错误。

我的代码:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
from sklearn.datasets import load_iris
from copy import deepcopy
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from modAL.models import ActiveLearner, Committee
import pandas as pd

RANDOM_STATE_SEED = 1
np.random.seed(RANDOM_STATE_SEED)

voice = pd.read_csv("voice.csv")

mapping = {'male': 1, 'female': 0}
voice = voice.replace({'label':mapping})
X = voice.drop('label',axis=1).values
y = voice['label'].values

X_pool = deepcopy(X)
y_pool = deepcopy(y)

#print(X_pool.shape,y_pool.shape)

n_members = 2
learner_list = list()

for member_idx in range(n_members):
    # initial training data
    n_initial = 2
    train_idx = np.random.choice(range(X_pool.shape[0]), size=n_initial, replace=False)
    X_train = X_pool[train_idx]
    y_train = y_pool[train_idx]

    # creating a reduced copy of the data with the known instances removed
    X_pool = np.delete(X_pool, train_idx, axis=0)
    y_pool = np.delete(y_pool, train_idx)

 # initializing learner
    learner = ActiveLearner(
        estimator=RandomForestClassifier(),
        X_training=X_train, y_training=y_train
    )
    learner_list.append(learner)

committee = Committee(learner_list=learner_list)

#unqueried_score = committee.score(voice.drop('label',axis=1), voice['label'])

unqueried_score = committee.score(X,y)

#print (unqueried_score)

performance_history = [unqueried_score]

print (performance_history)

n_queries = 20
for idx in range(n_queries):
    query_idx, query_instance = committee.query(X_pool)
    committee.teach(
        X=X_pool[query_idx].reshape(1, -1),
        y=y_pool[query_idx].reshape(1, )
    )

    #print(committee.teach)

**history = performance_history.append(committee.score(voice.drop('label',axis=1),voice['label']))**

#print(history)


#remove queried instance from pool
X_pool = np.delete(X_pool, query_idx, axis=0)
y_pool = np.delete(y_pool, query_idx)


fig, ax = plt.subplots(figsize=(8.5, 6), dpi=130)
ax.plot(performance_history)
ax.scatter(range(len(performance_history)), performance_history, s=13)
#print(len(performance_history))
ax.xaxis.set_major_locator(mpl.ticker.MaxNLocator(nbins=5, integer=True))
ax.yaxis.set_major_locator(mpl.ticker.MaxNLocator(nbins=10))
ax.yaxis.set_major_formatter(mpl.ticker.PercentFormatter(xmax=1))

ax.set_ylim(bottom=0, top=1)
ax.grid(True)

ax.set_title('Incremental classification accuracy')
ax.set_xlabel('Query iteration')
ax.set_ylabel('Classification Accuracy')
plt.show()

0 个答案:

没有答案