python - scikit-learn 中的机器学习算法是否必须将 pandas 数据帧转换为 numpy 数组

标签 python pandas numpy scikit-learn

scikit-learn 中的机器学习算法是否必须将 pandas 数据帧转换为 numpy 数组?

我知道to_numpy()函数进行转换。这意味着我也必须为 pandas 数据框中的分类列手动创建一个虚拟矩阵。

如果我只使用 pandas dataframe 作为 scikit-learn 中的输入会发生什么?如果我将 pandas 数据帧转换为 numpy 数组,那么这是否意味着我的列名称不再保留在机器学习算法中?当涉及到模型诊断时,需要采取额外的步骤来协调列名与 numpy 数组吗?

最佳答案

提供 float 组是一个安全的选择,但这不是必须的。无论您提供什么,都将尝试在内部转换为 numpy 数组。如果它不是类似数组(见下文),则会引发异常。

如果你采取RandomForestRegressor例如,您会在 sklearn 中发现它们有一个类似数组的概念。例如,请参阅 RandomForestRegressor.fit() 的文档字符串:

X{array-like, sparse matrix} of shape (n_samples, n_features) The training input samples. Internally, its dtype will be converted to dtype=np.float32. If a sparse matrix is provided, it will be converted into a sparse csc_matrix.

您可以通过阅读 glossary 进一步了解什么是类数组 :

array-like
The most common data format for input to Scikit-learn estimators and functions, array-like is any type object for which numpy.asarray will produce an array of appropriate shape (usually 1 or 2-dimensional) of appropriate dtype (usually numeric).

This includes:

  • a numpy array

  • a list of numbers

  • a list of length-k lists of numbers for some fixed length k

  • a pandas.DataFrame with all columns numeric

  • a numeric pandas.Series

It excludes:

  • a sparse matrix

  • an iterator

  • a generator

如果您浏览source ,您会发现您提供给方法的数据将流经 self._validate_data ,这将为您进行转换。

您可以随时通过 sklearn.utils.check_array 提前检查您的数据是否可接受,但它没有太大的实际意义,因为当您向方法提供数据时,无论如何它都会为您完成。

关于python - scikit-learn 中的机器学习算法是否必须将 pandas 数据帧转换为 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65273553/

相关文章:

Python点击: How to implement a help command that behaves like `--help` ?

python - 使用 nargs 参数时,如何使 argparse.ArgumentParser 返回元组或 np.array 而不是列表?

pandas - 指数值与航类(数据范围A行和E行)

python - Numpy 将整数数组保存为 float 组

python - 如何为 Numpy 数组创建切片对象?

python - 我怎样才能定期向数组添加一个新元素,以便它出现在每 500 个元素之后?

python - django "<QuerySet{[ ]}"出现在列表中

python - 根据条件替换来自不同文件的字符串

python - 仅将某些列 reshape 为单个列

python - 带有 apache 和 wsgi 的 Django 抛出 ImportError