示例#1
0
datapoints, class_to_label = split_dataset_to_data_and_labels(test_input)
print(class_to_label)


backend = BasicAer.get_backend('qasm_simulator')
feature_map = ZZFeatureMap(feature_dim, reps=2)
svm = QSVM(feature_map, training_input, test_input, None)# the data for prediction can be fed later.
svm.random_seed = random_seed
quantum_instance = QuantumInstance(backend, shots=shots, seed_simulator=random_seed, seed_transpiler=random_seed)
result = svm.run(quantum_instance)


print("kernel matrix during the training:")
kernel_matrix = result['kernel_matrix_training']
img = plt.imshow(np.asmatrix(kernel_matrix),interpolation='nearest',origin='upper',cmap='bone_r')
plt.show()

print("testing success ratio: ", result['testing_accuracy'])


predicted_labels = svm.predict(datapoints[0])

predicted_classes = map_label_to_class_name(predicted_labels, svm.label_to_class)
print("ground truth: {}".format(datapoints[1]))
print("preduction:   {}".format(predicted_labels))


# testing success ratio:  0.95

# sumber = https://github.com/qiskit-community/qiskit-community-tutorials/blob/master/machine_learning/qsvm.ipynb
backend = provider.get_backend(
    'ibmq_qasm_simulator')  # Specifying Quantum device

num_qubits = 1

feature_map = SecondOrderExpansion(feature_dimension=num_qubits,
                                   depth=2,
                                   entanglement='full')

svm = QSVM(feature_map, training_data, testing_data)  # Creation of QSVM

quantum_instance = QuantumInstance(backend,
                                   shots=shots,
                                   skip_qobj_validation=False)

print('Running....\n')

result = svm.run(quantum_instance)  # Running the QSVM and getting the accuracy

data = np.array([[1.453], [1.023], [0.135], [0.266]])  #Unlabelled data

prediction = svm.predict(data,
                         quantum_instance)  # Predict using unlabelled data

print(
    'Prediction of Smoker or Non-Smoker based upon gene expression of CDKN2A\n'
)
print('Accuracy: ', result['testing_accuracy'], '\n')
print('Prediction from input data where 0 = Non-Smoker and 1 = Smoker\n')
print(prediction)

quantum_instance=QuantumInstance(backend, shots=shots, skip_qobj_validation=False)


#Run the QSVM for accuracy results
result = svm.run(quantum_instance)


#Unlabelled data
data = np.array([[0, 0.80766, 0.73961, 0.20569, 445, 444, 0.004334704, 2.43E-05, 0.00072],
[1, 0.83967, 0.80944, 0.45038, 259, 257, 0.007310325, 0.00251383, 0.00534],
[1, 0.81525, 0.73462, 0.64849, 303, 302, 0.006381791, 0.000149359, 0.00292],
[1, 0.79163, 0.80358, 0.43866, 330, 329, 0.005844644, 8.21E-05, 0.00278],
[0, 0.7594, 0.68265, 0.39428, 443, 442, 0.004354498, 6.69E-05, 0.00076],
[1, 0.81547, 0.64809, 0.60227, 243, 242, 0.007963248, 9.04E-05, 0.00294],
[1, 0.82586, 0.59259, 0.44395, 354, 353, 0.005455777, 4.0E-05, 0.00129],
[1, 0.73403, 0.61812, 0.50801, 343, 342, 0.00563253, 8.34E-05, 0.00167],
[1, 0.87601, 0.62297, 0.43552, 346, 345, 0.005573364, 5.66E-05, 0.00118]
])


#Test unlabelled data
prediction = svm.predict(data,quantum_instance)


print('Prediction of Parkinsons disease based upon speech indicators\n')
print('Accuracy: ' , result['testing_accuracy'],'\n')
print('Prediction from input data where 0 = Healthy and 1 = Present\n')
print(prediction)