gpt4 book ai didi

Python SKLearn拟合值错误输入

转载 作者:行者123 更新时间:2023-11-28 22:16:52 25 4
gpt4 key购买 nike

我正在尝试将一些数据拟合并转换为分类器,以便稍后在模型中使用,但它总是给我一个错误,我不明白为什么。请问,有人可以帮助我吗?

##stores the function Pipeline with parameters decided above    
inputPipe = getPreProcPipe(normIn=normIn, pca=pca, pcaN=pcaN, whiten=whiten)
print inputPipe
print

#print devData[classTrainFeatures].values.astype('float32')

print devData[classTrainFeatures].shape
print type(devData[classTrainFeatures].values)

##fit pipeline to inputs features and types
inputPipe.fit(devData[classTrainFeatures].values.astype('float32'))

##transform inputs X
X_class = inputPipe.transform(devData[classTrainFeatures].values.astype(double))
## Output Y, i.e, 0 or 1 as it is the target
Y_class = devData['gen_target'].values.astype('int')
#print Y_class

输出:

Pipeline(memory=None,
steps=[('pca', PCA(copy=True, iterated_power='auto', n_components=None, random_state=None,
svd_solver='auto', tol=0.0, whiten=False)), ('normPCA', StandardScaler(copy=True, with_mean=True, with_std=True))])

(32583, 2)
<type 'numpy.ndarray'>

代码末尾错误:

ValueError: Input contains NaN, infinity or a value too large for dtype('float32').

Code

Error part 1

Error part 2

最佳答案

你必须检查你使用的数据(不是代码)是否包含 NaN(不是数字值),在 numpy 中有函数 .isnan()(https://docs.scipy.org/doc/numpy/reference/generated/numpy.isnan.html)用于这个How to get the indices list of all NaN value in numpy array?

还使用 .isinf() 检查无限值

在这个 kaggle 内核中是用于在数据集中填充 NaN 和 Inf 的示例代码,然后在分类器中使用 https://www.kaggle.com/mknorps/titanic-with-decision-trees ,另见 https://datascience.stackexchange.com/questions/25924/difference-between-interpolate-and-fillna-in-pandas?rq=1对于 interpolate()

删除包含 NaN 和 Inf 的行由

indx = devData[classTrainFeatures].index[devData[classTrainFeatures].apply(np.isnan)]
devData=devData.drop(devData.index[indx]).copy()
devData=devData.reset_index(drop=True)

(获取 NaN 的索引,使用该索引删除所有包含 NaN 的行,重置数据帧的索引)

关于Python SKLearn拟合值错误输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51847654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com