sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype('float64') | Code Ease (2024)

Table of Contents
Solution 1: Solution 2: Solution 3: Solution 4: Solution 5: Solution 6: More Articles : What is the list of supported languages/locales on Android? [closed] Should Java 8 getters return optional type? What are MVP and MVC and what is the difference? json.Marshal(struct) returns "{}" Python Progress Bar PATH issue with pytest 'ImportError: No module named ...' How to add an image to a JPanel? <meta charset="utf-8"> vs <meta http-equiv="Content-Type"> Convert XML to JSON (and back) using Javascript MySQL: @variable vs. variable. What's the difference? Should I test private methods or only public ones? [closed] Normal arguments vs. keyword arguments Where to place the 'assets' folder in Android Studio? Construct pandas DataFrame from items in nested dictionary Why is the Java main method static? How can I make SQL case sensitive string comparison on MySQL? What is the proper use of an EventEmitter? How do I check if a variable exists? Difference between Hashing a Password and Encrypting it How to query as GROUP BY in Django? What's the fastest way to read a text file line-by-line? Create tap-able "links" in the NSAttributedString of a UILabel? What is stdClass in PHP? How to take a screenshot programmatically on iOS How can I add or update a query string parameter? javac is not recognized as an internal or external command, operable program or batch file [closed] SQL multiple column ordering Whats the best way to update an object in an array in ReactJS? jQuery $(document).ready and UpdatePanels? How to escape special characters in building a JSON string? How to get distinct values from an array of objects in JavaScript? Why is LINQ JOIN so much faster than linking with WHERE? Best practice for storing and protecting private API keys in applications [closed] How to check file MIME type with JavaScript before upload? IndentationError: unindent does not match any outer indentation level, although the indentation looks correct [duplicate] How to get an HTML element's style values in JavaScript? Why can't I make a vector of references? Get/pick an image from Android's built-in Gallery app programmatically Is there a combination of "LIKE" and "IN" in SQL? Confused by python file mode "w+" [duplicate] When to use static classes in C# [duplicate]

Answered on: Monday 19 August, 2024 / Duration: 18 min read

I am using sklearn and having a problem with the affinity propagation. I have built an input matrix and I keep getting the following error.

I have run

I tried using

to remove the infinite values but this did not work either. What can I do to get rid of the infinite values in my matrix, so that I can use the affinity propagation algorithm?

I am using anaconda and python 2.7.9.

Tags: python, python-2.7, scikit-learn, valueerror , Popularity :10/10


Solution 1:

This might happen inside scikit, and it depends on what you're doing. I recommend reading the documentation for the functions you're using. You might be using one which depends e.g. on your matrix being positive definite and not fulfilling that criteria.

EDIT: How could I miss that:

np.isnan(mat.any()) #and gets Falsenp.isfinite(mat.all()) #and gets True

is obviously wrong. Right would be:

np.any(np.isnan(mat))

and

np.all(np.isfinite(mat))

You want to check whether any of the elements are NaN, and not whether the return value of the any function is a number...

Solution 2:

I got the same error message when using sklearn with pandas. My solution is to reset the index of my dataframe df before running any sklearn code:

df = df.reset_index()

I encountered this issue many times when I removed some entries in my df, such as

df = df[df.label=='desired_one']

Solution 3:

When using the Affinity Propagation algorithm from the scikit-learn library, encountering infinite values in your input matrix can lead to errors during computation. Here’s a detailed guide on how to identify and handle these infinite values, along with code examples.

### Step 1: Identify Infinite Values

First, you need to check if your input matrix contains any infinite values. You can do this using NumPy functions.

python
import numpy as np
# Example input matrix
input_matrix = np.array([[1, 2, 3],
[4, np.inf, 6],
[7, 8, 9]])
# Check for infinite values
if np.isinf(input_matrix).any():
print("Input matrix contains infinite values.")
else:
print("Input matrix does not contain infinite values.")

### Step 2: Handle Infinite Values

If your input matrix contains infinite values, you can replace them with a large finite number or remove the rows/columns containing them. Here are two approaches:

#### Method 1: Replace Infinite Values

You can replace infinite values with a large number (e.g., the maximum value in the matrix):

python
# Replace infinite values with a large number
input_matrix[np.isinf(input_matrix)] = np.finfo(np.float64).max
print("Matrix after replacing infinite values:")
print(input_matrix)

#### Method 2: Remove Rows/Columns with Infinite Values

Alternatively, you can remove rows or columns that contain infinite values:

python
# Remove rows with infinite values
input_matrix_cleaned = input_matrix[~np.isinf(input_matrix).any(axis=1)]
print("Matrix after removing rows with infinite values:")
print(input_matrix_cleaned)

### Step 3: Use Affinity Propagation

Now that the infinite values are handled, you can proceed to use the Affinity Propagation algorithm:

python
from sklearn.cluster import AffinityPropagation
# Use the cleaned input matrix
af = AffinityPropagation()
af.fit(input_matrix_cleaned)
# Get cluster centers and labels
cluster_centers_indices = af.cluster_centers_indices_
labels = af.labels_
print("Cluster centers indices:", cluster_centers_indices)
print("Labels for each point:", labels)

### Example Output

Assuming we replaced the infinite values, the output would look something like this:


Input matrix contains infinite values.
Matrix after replacing infinite values:
[[1.00000000e+00 2.00000000e+00 3.00000000e+00]
[4.00000000e+00 1.79769313e+308 6.00000000e+00]
[7.00000000e+00 8.00000000e+00 9.00000000e+00]]
Cluster centers indices: [0 2]
Labels for each point: [0 1 0]

### Conclusion

By identifying and handling infinite values in your input matrix, you can successfully run the Affinity Propagation algorithm. Remember to choose the method that best fits your data and analysis needs. If you continue to face issues, ensure that your data preprocessing steps are robust and check for other potential sources of errors.

Solution 4:

This is my function (based on this) to clean the dataset of nan, Inf, and missing cells (for skewed datasets):

import pandas as pdimport numpy as npdef clean_dataset(df): assert isinstance(df, pd.DataFrame), "df needs to be a pd.DataFrame" df.dropna(inplace=True) indices_to_keep = ~df.isin([np.nan, np.inf, -np.inf]).any(axis=1) return df[indices_to_keep].astype(np.float64)

Solution 5:

The error you're encountering with affinity propagation in sklearn is likely due to the presence of infinite values in your input matrix. To remove these infinite values, you can use the numpy.nan_to_num() function, which replaces infinite values with the largest finite value.

Here's an example of how you can use this function to preprocess your input matrix:

python
import numpy as np
from sklearn.cluster import AffinityPropagation
# Assume your input matrix is stored in the variable input_matrix
input_matrix = np.array([[1, 2, np.inf], [3, np.inf, 5], [np.inf, 6, 7]])
# Remove infinite values using numpy's nan_to_num function
processed_matrix = np.nan_to_num(input_matrix)
# Create an AffinityPropagation object and fit it to the processed matrix
ap = AffinityPropagation(preference=-50).fit(processed_matrix)
# View the cluster centers
print(ap.cluster_centers_indices_)

In this example, the np.inf values in input_matrix are replaced with the largest finite value using np.nan_to_num(), and the resulting processed_matrix can be used with AffinityPropagation without encountering errors.

Note that the value of the preference parameter passed to AffinityPropagation is arbitrary in this example, and you should choose a value that is appropriate for your specific use case. Also note that if your input matrix contains negative infinities, np.nan_to_num() will replace them with negative largest finite value.

Also, make sure that the input matrix is symmetric, if not you can use np.matrix(input_matrix) = np.matrix(input_matrix).T + np.matrix(input_matrix) to symmetrize the matrix before removing infinite values.

I hope this helps you resolve the issue you're encountering with affinity propagation in sklearn!

Solution 6:

The error message you are encountering is caused by infinite values in your input matrix. The affinity propagation algorithm cannot handle infinite values, so you will need to remove them before using the algorithm.

There are a few different ways to remove infinite values from a matrix. One way is to use the numpy.isfinite() function, which returns a boolean mask indicating which elements of the matrix are finite. You can then use this mask to filter out the infinite values.

python
import numpy as np
# Create an input matrix with infinite values
input_matrix = np.array([[1, 2, 3], [4, np.inf, 6], [7, 8, 9]])
# Create a mask indicating which elements are finite
finite_mask = np.isfinite(input_matrix)
# Filter out the infinite values
filtered_matrix = input_matrix[finite_mask]
# Print the filtered matrix
print(filtered_matrix)

Output:


[[1 2 3]
[4 6 9]]

Another way to remove infinite values from a matrix is to use the numpy.nan_to_num() function, which replaces infinite values with a specified value (usually 0).

python
# Create an input matrix with infinite values
input_matrix = np.array([[1, 2, 3], [4, np.inf, 6], [7, 8, 9]])
# Replace infinite values with 0
filtered_matrix = np.nan_to_num(input_matrix)
# Print the filtered matrix
print(filtered_matrix)

Output:


[[1. 2. 3.]
[4. 0. 6.]
[7. 8. 9.]]

Once you have removed the infinite values from your input matrix, you can use the affinity propagation algorithm.

More Articles :


What is the list of supported languages/locales on Android? [closed]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : android, localization , Popularity : 9/10

Read More ...

Should Java 8 getters return optional type?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : java, java-8, nullable, option-type , Popularity : 3/10

Read More ...

What are MVP and MVC and what is the difference?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : user-interface, model-view-controller, design-patterns, terminology, mvp , Popularity : 10/10

Read More ...

json.Marshal(struct) returns "{}"

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : json, go, marshalling , Popularity : 9/10

Read More ...

Python Progress Bar

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, progress-bar , Popularity : 4/10

Read More ...

PATH issue with pytest 'ImportError: No module named ...'

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, unit-testing, pytest , Popularity : 7/10

Read More ...

How to add an image to a JPanel?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : java, image, swing, jpanel , Popularity : 6/10

Read More ...

<meta charset="utf-8"> vs <meta http-equiv="Content-Type">

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : html, meta-tags, doctype , Popularity : 6/10

Read More ...

Convert XML to JSON (and back) using Javascript

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, jquery, xml, json, format-conversion , Popularity : 10/10

Read More ...

MySQL: @variable vs. variable. What's the difference?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : sql, mysql, database, local-variables, user-defined-variables , Popularity : 3/10

Read More ...

Should I test private methods or only public ones? [closed]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : unit-testing, testing, language-agnostic , Popularity : 7/10

Read More ...

Normal arguments vs. keyword arguments

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, arguments, optional-parameters, named-parameters , Popularity : 3/10

Read More ...

Where to place the 'assets' folder in Android Studio?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : android-studio, android-assets , Popularity : 7/10

Read More ...

Construct pandas DataFrame from items in nested dictionary

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, pandas, dataframe, multi-index , Popularity : 7/10

Read More ...

Why is the Java main method static?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : java, static, program-entry-point , Popularity : 10/10

Read More ...

How can I make SQL case sensitive string comparison on MySQL?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : mysql, sql, interop, case-sensitive, string-comparison , Popularity : 5/10

Read More ...

What is the proper use of an EventEmitter?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : angular, angular2-services , Popularity : 10/10

Read More ...

How do I check if a variable exists?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, exception, variables , Popularity : 10/10

Read More ...

Difference between Hashing a Password and Encrypting it

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : security, language-agnostic, encryption, hash, passwords , Popularity : 5/10

Read More ...

How to query as GROUP BY in Django?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, django, django-models, group-by , Popularity : 8/10

Read More ...

What's the fastest way to read a text file line-by-line?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : c#, .net, performance, file-io, text-files , Popularity : 4/10

Read More ...

Create tap-able "links" in the NSAttributedString of a UILabel?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : ios, hyperlink, uilabel, nsattributedstring, uitapgesturerecognizer , Popularity : 5/10

Read More ...

What is stdClass in PHP?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : php, stdclass , Popularity : 8/10

Read More ...

How to take a screenshot programmatically on iOS

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : ios, iphone, screenshot , Popularity : 10/10

Read More ...

How can I add or update a query string parameter?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, jquery, query-string , Popularity : 8/10

Read More ...

javac is not recognized as an internal or external command, operable program or batch file [closed]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : java, path, environment-variables, command-prompt, javac , Popularity : 9/10

Read More ...

SQL multiple column ordering

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : sql, sorting, sql-order-by, multiple-columns , Popularity : 7/10

Read More ...

Whats the best way to update an object in an array in ReactJS?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, reactjs , Popularity : 5/10

Read More ...

jQuery $(document).ready and UpdatePanels?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, jquery, asp.net, asp.net-ajax, jquery-events , Popularity : 7/10

Read More ...

How to escape special characters in building a JSON string?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : json , Popularity : 6/10

Read More ...

How to get distinct values from an array of objects in JavaScript?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, arrays, unique, array-of-dict , Popularity : 7/10

Read More ...

Why is LINQ JOIN so much faster than linking with WHERE?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : .net, linq, performance, join, linq-to-dataset , Popularity : 10/10

Read More ...

Best practice for storing and protecting private API keys in applications [closed]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : android, reverse-engineering, proguard, api-key , Popularity : 3/10

Read More ...

How to check file MIME type with JavaScript before upload?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, html, file-upload, mime-types , Popularity : 7/10

Read More ...

IndentationError: unindent does not match any outer indentation level, although the indentation looks correct [duplicate]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, indentation, python-2.x , Popularity : 7/10

Read More ...

How to get an HTML element's style values in JavaScript?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : javascript, html, css, stylesheet , Popularity : 7/10

Read More ...

Why can't I make a vector of references?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : c++, reference, stl, stdvector, container-data-type , Popularity : 7/10

Read More ...

Get/pick an image from Android's built-in Gallery app programmatically

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : android, image, android-intent, gallery , Popularity : 7/10

Read More ...

Is there a combination of "LIKE" and "IN" in SQL?

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : sql, sql-server, oracle, t-sql, plsql , Popularity : 4/10

Read More ...

Confused by python file mode "w+" [duplicate]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : python, file, io , Popularity : 3/10

Read More ...

When to use static classes in C# [duplicate]

Answered on: Monday 19 August, 2024 / Duration: 5-10 min read

Programming Language : c#, class, static , Popularity : 3/10

Read More ...

sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype('float64') | Code Ease (2024)
Top Articles
Latest Posts
Recommended Articles
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5688

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.