Input contains infinity or a value too large for dtype('float64') error

Viewed 134

Input contains infinity or a value too large for dtype('float64') error shows up when I run this code. How can I solve it?

from sklearn import preprocessing
from tensortrade.data.cdd import CryptoDataDownload 
import pandas as pd

cdd = CryptoDataDownload()

data = cdd.fetch("Bitstamp", "USD", "BTC", "1h")

for col in data.columns:
  if col not in ['date', 'unix']:
    data[col]=data[col].pct_change()
    data.dropna(inplace=True)
    data[col] = preprocessing.scale(data[col].values)
    
print(data.head())
2 Answers

You have infinite value in your data, remove them with this:

    data[col] =data[col][data[col]  != float('inf') ]
from sklearn import preprocessing
from tensortrade.data.cdd import CryptoDataDownload 
import pandas as pd

cdd = CryptoDataDownload()

data = cdd.fetch("Bitstamp", "USD", "BTC", "1h")

for col in data.columns:
  if col not in ['date', 'unix']:
    data[col]=data[col].pct_change()
    data.dropna(inplace=True)
    data[col] =data[col][data[col]  != float('inf') ]
    data[col] = preprocessing.scale(data[col].values)
    
print(data.head())

Try running this. You need to replace Nan and Inf values with a number of your choice

import numpy as np
from sklearn import preprocessing
from tensortrade.data.cdd import CryptoDataDownload 
import pandas as pd

cdd = CryptoDataDownload()

data = cdd.fetch("Bitstamp", "USD", "BTC", "1h")

for col in data.columns:
  if col not in ['date', 'unix']:
    data[col]=data[col].pct_change()
    data.dropna(inplace=True)
    print(data[col].tolist())
    # replace inf or nan with a number (for my example zero is selected)
    data[col] = [0 if np.isnan(x) or np.isinf(x) else x for x in data[col]]

    data[col] = preprocessing.scale(data[col].values)
    
print(data.head())
Related