score:0
Additionally to what you have tried, you can also see if
import numpy as np
features = np.nan_to_num(features)
rewards = np.nan_to_num(rewards)
This sets all non-numeric values in your arrays to 0
, and should at least make your algorithm run, unless the error occurs somewhere internal to the algorithm. Make sure there aren't to many non-numeric entries in your data, as setting them all to 0 may cause strange biases in your estimates.
If this is not the case, and you are using weights='distance'
, then please check whether any of the train samples are identical. This will cause a division by zero in inverse distance.
If inverse distances are the cause of division by zero, you can circumvent this by using your own distance function, e.g.
def better_inv_dist(dist):
c = 1.
return 1. / (c + dist)
and then use 'weights': better_inv_dist
. You may need to adapt the constant c
to the right scale. In any case it will avoid division by zero as long as c > 0
.
score:0
I ran into the same problem with KNN regression on scikit-learn. I was using weights='distance' and that led to infinite values while computing the predictions (but not while fitting the KNN model i.e. learning appropriate KD Tree or Ball Tree). I switched to weights='uniform' and the program ran to completion correctly, indicating the supplied weight function was the problem. If you want to use distance-based weights, supply a custom-weight function that doesn't explode to infinity at zero distance as indicated in eickenberg's answer.
Credit To: stackoverflow.com
Related Query
- Scikit grid search for KNN regression ValueError: Array contains NaN or infinity
- Linear Regression ValueError: Input contains NaN, infinity or a value too large for dtype('float64')
- ValueError: Input contains NaN, infinity or a value too large for dtype('float64') in linear regression
- Overcome ValueError for empty array
- Scikit-learn : Input contains NaN, infinity or a value too large for dtype ('float64')
- Efficient 1D linear regression for each element of 3D numpy array
- Treat nan as zero in numpy array summation except for nan in all arrays
- Create a mask both for nan and inf values in an array
- Search for a pattern in numpy array
- Dummify categorical variables for logistic regression with pandas and scikit (OneHotEncoder)
- Customized search for consequtive values in numpy array
- Search numpy array ((x, y, z)...) for z matching nearest x, y
- numpy search array for multiple values, and returns their indices
- Scikit Learn: Randomized Logistic Regression gives ValueError: output array is read-only
- Efficient way to transform array into encoding for ordinal regression
- delete 'nan' or reduce length of numpy array if array contains nan after converting to numpy from pandas
- ValueError: Input contains NaN, infinity or a value too large for dtype('float32'). Why?
- Search for the nearest array in a huge array of arrays
- Gaussian Process regression hyparameter optimisation using python Grid search
- Python, numpy array code not working for NaN elements
- Faster way to search for dates in numpy array of datetime.date objects?
- Python valueError using hstack() (ValueError: all the input array dimensions except for the concatenation axis must match exactly)
- Split array vertically, add rows of data, sort, and then apply regression for rainfall data
- Numpy search for elements of an array in a subset
- How to search in one NumPy array for positions for getting at these position the value from a second NumPy array?
- place random numbers for nan values numpy array
- ValueError: Input contains NaN, infinity or a value too large for dtype('float64'), when using sklearn IterativeImputer
- how to search for unique elements by the first column of a multidimensional array
- ValueError: Input contains NaN, infinity or a value too large for dtype('float64') using fit from KNeighborsRegressor
- ValueError: Expected 2D array, got 1D array instead. Python Function for Linear Regression
More Query from same tag
- How can I take the dot product of multiple vectors in a numpy array all at once?
- Visualize Trees and OOB error: 'numpy.ndarray' object is not callable
- How to remove duplicates from a numpy array with multiple dimensions
- Find the number of islands in a 1D numpy array
- Cannot figure out Numpy equivalent for cv.mat.step[0]
- Why are numpy calculations not affected by the global interpreter lock?
- TypeError: can only concatenate str (not "numpy.float64") to str data set question
- fast categorization (binning)
- Error when importing numba in Python 3
- Extract individual values from numpy array into a list
- How to manage "TypeError" message when plotting functions defined in mpmath module instead of scipy?
- How to speed up iteration over image array / numpy array
- Masking a 3D NumPy array --> returns 5D array
- Use of @ on dataframe pyhton?
- Convert pixel coordinates to frame coordinates