所以我的随机森林分类器运行得很好,直到我添加了新功能。当我尝试运行它时,我不断收到以下错误代码:
\Anaconda2\lib\site-packages\sklearn\utils\validation.pyc in _assert_all_finite(X)
56 and not np.isfinite(X).all()):
57 raise ValueError("Input contains NaN, infinity"
---> 58 " or a value too large for %r." % X.dtype)
59
60
ValueError: Input contains NaN, infinity or a value too large for dtype('float32').
train和test都是从csv文件中读取的np.DataFrame对象。我正在尝试为更好的预测器添加更多功能,但每当我尝试拟合时,最终会得到上述错误。我确实尝试删除NaN和无限值,但仍然得到相同的错误。
以下是我的代码:
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import log_loss
def features(df):
df["num_photos"] = df["photos"].apply(len)
df["num_features"] = df["features"].apply(len)
df["year_created"] = df["created"].dt.year
df["month_created"] = df["created"].dt.month
df["day_created"] = df["created"].dt.day
df["desc_len"] = df["description"].apply(lambda x: len(x.split(" ")))
#New features begin here
df["pricePerBed"] = df['price'] / df['bedrooms']
df["pricePerBath"] = df['price'] / df['bathrooms']
df["pricePerRoom"] = df['price'] / (df['bedrooms'] + df['bathrooms'])
df["bedPerBath"] = df['bedrooms'] / df['bathrooms']
df["bedBathDiff"] = df['bedrooms'] - df['bathrooms']
df["bedBathSum"] = df["bedrooms"] + df['bathrooms']
df["bedsPerc"] = df["bedrooms"] / (df['bedrooms'] + df['bathrooms'])
df = df.replace([np.inf, -np.inf], np.nan)
df = df.fillna(1)
return df
features(train)
features(test)
key_features = ["bathrooms", "bedrooms", "latitude", "longitude", "year_created",
"month_created", "day_created", "price", "num_photos", "num_features", "desc_len",
"pricePerBed",
"pricePerBath",
"pricePerRoom",
#"bedPerBath",
"bedBathDiff",
"bedBathSum"]
X = train[key_features]
y = train["interest_level"]
X.fillna(1) #I tried getting rid of NaN
X.isnull().any()
bedPerBath变量为isnull()。any()提供了一个True,所以我把它留了出去,剩下的都给了我假。但是,当我尝试拟合估算器时,我仍然得到“ValueError”。
X_train, X_cv, y_train, y_cv = train_test_split(X, y, test_size = 0.3)
X_train.isnull().any()
clfRF = RandomForestClassifier(n_estimators = 1000)
clfRF.fit(X_train, y_train)
#CV
y_cv_pred = clfRF.predict_proba(X_cv)
log_loss(y_cv, y_cv_pred)
我注意到错误消息对于dtype('float32')来说太大了,而我的值大部分是float64,这是否会导致错误?如果是这样的话?
谢谢。
答案 0 :(得分:1)
尝试:
import numpy as np
X_train, X_cv, y_train, y_cv = train_test_split(np.nan_to_num(X), y, test_size = 0.3)
clfRF = RandomForestClassifier(n_estimators = 1000)
clfRF.fit(X_train, y_train)
#CV
y_cv_pred = clfRF.predict_proba(X_cv)
log_loss(y_cv, y_cv_pred)