Giter Site home page Giter Site logo

API for extracting train and test kernel matrices and support vectors from QSVC upon completed training about qiskit-machine-learning HOT 2 CLOSED

zoplex avatar zoplex commented on July 29, 2024
API for extracting train and test kernel matrices and support vectors from QSVC upon completed training

from qiskit-machine-learning.

Comments (2)

attp avatar attp commented on July 29, 2024

@zoplex as QSVC extends the scikit-learn svc class, we are restricted with what that returns.

You to get the indices of the support vectors using qsvc.support_, but I don't believe it is possible to get the kernel matrices from svc, or at least I haven't been able to find it in the documentation.

The quantum kernel machine learning tutorial shows you how to calculate the matrices and provide them to scikit-learn svc.

If you want to be more efficient, and not create the full test x train matrix, you could retrain a new svc using just the support vectors:

train_matrix = adhoc_kernel.evaluate(x_vec=train_data)
svc_initial = SVC(kernel='precomputed')
svc_initial.fit(train_matrix, train_labels)
svc_support = svc_initial.support_
train_matrix_support = train_matrix[svc_support,:][:,svc_support]
test_matrix_support = adhoc_kernel.evaluate(x_vec=test_data, y_vec=train_data[svc_support])
svc = SVC(kernel='precomputed')
svc.fit(train_matrix_support, train_labels[svc_support])
svc_score = svc.score(test_matrix_support, test_labels)

from qiskit-machine-learning.

zoplex avatar zoplex commented on July 29, 2024

Thank you for the quick response. We already upgraded the code to the latest qiskit version and we are using .evaluate to calculate both train and test kernel matrices (full size) just like in the same code on qiskit site/above in your example, we were not sure how to get support vectors. This solution above resolves it, I will try this approach. Using support vectors dimension instead of full train in test x train will save some time too.

Best regards

from qiskit-machine-learning.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.