raise ValueError("Input contains NaN") ValueError: Input contains NaN when trying to build machine learning model
raise ValueError("Input contains NaN") ValueError: Input contains NaN when trying to build machine learning model
我正在尝试构建预测模型,但目前不断收到错误消息:raise ValueError("Input contains NaN") ValueError: Input contains NaN
。我尝试使用 np.any(np.isnan(dataframe))
和 np.any(np.isnan(dataframe))
,但我不断收到新错误。例如,TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
.
目前的代码如下:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import numpy as np
dataframe = pd.read_csv('file.csv', delimiter=',')
le = LabelEncoder()
dfle = dataframe
dfle2 = dfle.apply(lambda col: le.fit_transform(col.astype(str)), axis=0, result_type='expand')
newdf = dfle2[['column1', 'column2', 'column3', 'column4', 'column5', 'column6', 'column7']]
X = dataframe[['column1', 'column2', 'column4', 'column5', 'column6', 'column7']].values
y = dfle.column3
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ohe = OneHotEncoder()
ColumnTransformer([('encoder', OneHotEncoder(), [0])], remainder='passthrough')
# np.all(np.isfinite(dfle))
# np.any(np.isnan(dfle))
X = ohe.fit_transform(X).toarray()
你可以做很多事情来处理这个错误
首先,您可以用 0 dataframe = pd.read_csv('file.csv', delimiter=',').fillna(0)
填充 Nan 值
或者您可以使用 sklearn
插补技术来填充 Nan 值。
https://scikit-learn.org/stable/modules/classes.html#module-sklearn.impute
可以使用多重插补技术,但您应该使用 KNNImputer
。
错误
TypeError: ufunc 'isfinite' not supported for the input types,
and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
可能是因为您在执行 col.astype(str)
时转换为 str
。改用 astype(float)
之类的东西。
至于 NaN
错误,您需要考虑是否可以通过将其替换为零 (fillna(0)
) 来解决问题,或者是否需要进行更复杂的操作,例如例如卡尔曼滤波器。
我正在尝试构建预测模型,但目前不断收到错误消息:raise ValueError("Input contains NaN") ValueError: Input contains NaN
。我尝试使用 np.any(np.isnan(dataframe))
和 np.any(np.isnan(dataframe))
,但我不断收到新错误。例如,TypeError: ufunc 'isfinite' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
.
目前的代码如下:
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import numpy as np
dataframe = pd.read_csv('file.csv', delimiter=',')
le = LabelEncoder()
dfle = dataframe
dfle2 = dfle.apply(lambda col: le.fit_transform(col.astype(str)), axis=0, result_type='expand')
newdf = dfle2[['column1', 'column2', 'column3', 'column4', 'column5', 'column6', 'column7']]
X = dataframe[['column1', 'column2', 'column4', 'column5', 'column6', 'column7']].values
y = dfle.column3
from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ohe = OneHotEncoder()
ColumnTransformer([('encoder', OneHotEncoder(), [0])], remainder='passthrough')
# np.all(np.isfinite(dfle))
# np.any(np.isnan(dfle))
X = ohe.fit_transform(X).toarray()
你可以做很多事情来处理这个错误
首先,您可以用 0 dataframe = pd.read_csv('file.csv', delimiter=',').fillna(0)
或者您可以使用 sklearn
插补技术来填充 Nan 值。
https://scikit-learn.org/stable/modules/classes.html#module-sklearn.impute
可以使用多重插补技术,但您应该使用 KNNImputer
。
错误
TypeError: ufunc 'isfinite' not supported for the input types,
and the inputs could not be safely coerced to any supported types
according to the casting rule ''safe''
可能是因为您在执行 col.astype(str)
时转换为 str
。改用 astype(float)
之类的东西。
至于 NaN
错误,您需要考虑是否可以通过将其替换为零 (fillna(0)
) 来解决问题,或者是否需要进行更复杂的操作,例如例如卡尔曼滤波器。