Skip to content
Advertisement

Using PyTorch tensors with scikit-learn

Can I use PyTorch tensors instead of NumPy arrays while working with scikit-learn?

I tried some methods from scikit-learn like train_test_split and StandardScalar, and it seems to work just fine, but is there anything I should know when I’m using PyTorch tensors instead of NumPy arrays?

According to this question on https://scikit-learn.org/stable/faq.html#how-can-i-load-my-own-datasets-into-a-format-usable-by-scikit-learn :

numpy arrays or scipy sparse matrices. Other types that are convertible to numeric arrays such as pandas DataFrame are also acceptable.

Does that mean using PyTorch tensors is completely safe?

Advertisement

Answer

I don’t think PyTorch tensors are directly supported by scikit-learn. But you can always get the underlying numpy array from PyTorch tensors

my_nparray = my_tensor.numpy()

and then use it with scikit learn functions.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement