如何将稀疏的NumPy数组转换为DataFrame?

以下是代码片段,

from sklearn.compose import ColumnTransformerfrom sklearn.preprocessing import OneHotEncoderct = ColumnTransformer(transformers=[('encoder',OneHotEncoder(),[2,3,4])],remainder='passthrough')X = np.array(ct.fit_transform(x_data))X.shape

我得到的形状输出如下

()

当我尝试打印X时,得到的输出如下

array(<8820x35 sparse matrix of type '<class 'numpy.float64'>'    with 41527 stored elements in Compressed Sparse Row format>, dtype=object)

现在当我尝试将这个数组转换为DataFrame时

X = pd.DataFrame(X)

我得到以下错误

ValueError: Must pass 2-d input

如何将我的NumPy数组转换为DataFrame?


回答:

看起来

ct.fit_transform(x_data)

产生了一个稀疏矩阵。

np.array(...)

只是将其包装在一个对象类型数组中。

array(<8820x35 sparse matrix of type '<class 'numpy.float64'>'    with 41527 stored elements in Compressed Sparse Row format>, dtype=object)

使用toarrayA将其正确转换为NumPy数组:

X = ct.fit_transform(x_data).A

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注