'I removed the sparsity from my matrix and then applied csr_matrix method to the dataset. I'm trying to pass the csr_data to KNN which gives an error

This is the link for the website that I've been refering to: https://www.analyticsvidhya.com/blog/2020/11/create-your-own-movie-movie-recommendation-system/

This is my code to remove the sparsity:

    from scipy.sparse import csr_matrix
sample = np.array([[0,0,3,0,0],[4,0,0,0,2],[0,0,0,0,1]])
sparsity = 1.0 - ( np.count_nonzero(sample) / float(sample.size) )
print(sparsity)
csr_sample = csr_matrix(sample)
print(csr_sample)
csr_data = csr_matrix(data.values.astype(np.float))
data.reset_index(inplace=True)

This is where I'm getting an error: from sklearn.neighbors import NearestNeighbors from sklearn.neighbors import KNeighborsClassifier knn = NearestNeighbors(metric='cosine', algorithm='brute', n_neighbors=20, n_jobs=-1) knn.fit(csr_data)//error

This the error that I'm getting:

ValueError                                Traceback (most recent call last)
<ipython-input-270-4b5ddca2edd0> in <module>()
      2 from sklearn.neighbors import KNeighborsClassifier
      3 knn = NearestNeighbors(metric='cosine', algorithm='brute', n_neighbors=20, n_jobs=-1)
----> 4 knn.fit(csr_data)

5 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in _assert_all_finite(X, allow_nan, msg_dtype)
    114             raise ValueError(
    115                 msg_err.format(
--> 116                     type_err, msg_dtype if msg_dtype is not None else X.dtype
    117                 )
    118             )

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source