fromqiskit_algorithms.utilsimportalgorithm_globalsfromqiskit_machine_learning.datasetsimportad_hoc_dataseed=12345algorithm_globals.random_seed=seedadhoc_dimension=2train_features,train_labels,test_features,test_labels,adhoc_total=ad_hoc_data(training_size=20,test_size=5+5,# 5 for test, 5 for predictn=adhoc_dimension,gap=0.3,plot_data=False,one_hot=False,include_sample_total=True,)
# the `test_features` and `test_labels` are of size which is double that of the `test_size` argument# Since there are `test_size` items for each `adhoc_dimension`importnumpyasnpdefsplit(obj:np.ndarray,n:int=20):quarter=n//4half=n//2first=np.concatenate((obj[:quarter],obj[half:half+quarter]))second=np.concatenate((obj[quarter:half],obj[half+quarter:]))returnfirst,secondtest_features,predict_features=split(test_features)test_labels,predict_labels=split(test_labels)
# Plot dataimportmatplotlib.pyplotaspltimportnumpyasnpplt.figure(figsize=(5,5))plt.ylim(0,2*np.pi)plt.xlim(0,2*np.pi)plt.imshow(np.asmatrix(adhoc_total).T,interpolation="nearest",origin="lower",cmap="RdBu",extent=[0,2*np.pi,0,2*np.pi],)plt.scatter(train_features[np.where(train_labels[:]==0),0],train_features[np.where(train_labels[:]==0),1],marker="s",facecolors="w",edgecolors="b",label="A train",)plt.scatter(train_features[np.where(train_labels[:]==1),0],train_features[np.where(train_labels[:]==1),1],marker="o",facecolors="w",edgecolors="r",label="B train",)plt.scatter(test_features[np.where(test_labels[:]==0),0],test_features[np.where(test_labels[:]==0),1],marker="s",facecolors="b",edgecolors="w",label="A test",)plt.scatter(test_features[np.where(test_labels[:]==1),0],test_features[np.where(test_labels[:]==1),1],marker="o",facecolors="r",edgecolors="w",label="B test",)plt.legend(bbox_to_anchor=(1.05,1),loc="upper left",borderaxespad=0.0)plt.title("Ad hoc dataset for classification")plt.show()