Search code examples
pythonnumpyscikit-learnmetrics

How can I calculate scikit-learn rbf_kernel() with very large array?


While using the rbf_kernel() function the array is too large and there is a memory issue, so I have to separate the data and calculate it.

from sklearn.metrics.pairwise import rbf_kernel
result = rbf_kernel([[1,1],[2,2],[3,3]], gamma=60) # A data:[1,1] , B data:[2,2], C data:[3,3]

And result looks like

  A B C
A 1 2 1
B 1 1 1
C 1 1 2 

However, if I insert larger data, there is a memory issue.

result = rbf_kernel([[1,1],[2,2],[3,3],[4,4],[5,5],.... ], gamma=60) 

How can I extract the result without putting data all at once?


Solution

  • Try using:

    l = [[1,1],[2,2],[3,3],[4,4],[5,5], ...]
    newl = []
    for i in range(0, len(l), 10):
        newl.append(rbf_kernel(l[i:i + 10]))