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

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

Input contains infinity or a value too large for dtype('float64') 当我运行此代码时出现错误。我该如何解决?

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())

你的数据有无限的价值,用这个删除它们:

    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())

尝试运行这个。您需要用您选择的数字替换 Nan 和 Inf 值

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())