python - TypeError:不支持的类型对于结构化数据适配器
问题描述
谁能帮我解决上述错误?
### using trasnformers
from sklearn.compose import ColumnTransformer
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
column_trans = ColumnTransformer(
[
('CompanyName_bow', TfidfVectorizer(), 'CompanyName'),
('state_category', OneHotEncoder(), ['state']),
('Termination_Reason_Desc_bow', TfidfVectorizer(), 'Termination_Reason_Desc'),
('TermType_category', OneHotEncoder(), ['TermType'])
],
remainder=MinMaxScaler()
)
X = column_trans.fit_transform(X.head(100))
from sklearn.preprocessing import LabelEncoder
y = LabelEncoder().fit_transform(y.head(100))
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2,random_state=5)
X_train.shape #(80, 92)
X_test.shape #(20, 92)
y_train.shape #(80,)
X_train.todense()
matrix([[0. , 0. , 0. , ..., 0.26921709, 1. ,
0. ],
[0. , 0. , 0. , ..., 0. , 0. ,
1. ],
[0. , 0. , 0. , ..., 0.46148896, 1. ,
0. ],
...,
[0. , 0. , 0. , ..., 0.46148896, 1. ,
0. ],
[0. , 0. , 0. , ..., 0. , 0. ,
1. ],
[0. , 0. , 0. , ..., 0.46148896, 1. ,
0. ]])
type(X_train)
--> scipy.sparse.csr.csr_matrix
print(y_train)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
type(y_train)
numpy.ndarray
# use autokeras to find a model for the sonar dataset
from numpy import asarray
from pandas import read_csv
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from autokeras import StructuredDataClassifier
print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
# define the search
search = StructuredDataClassifier(max_trials=15)
# perform the search
search.fit(x=(X_train), y=y_train, verbose=0)
# evaluate the model
loss, acc = search.evaluate(X_test, y_test, verbose=0)
print('Accuracy: %.3f' % acc)
错误
(80, 92) (20, 92) (80,) (20,)
INFO:tensorflow:Reloading Oracle from existing project .\structured_data_classifier\oracle.json
INFO:tensorflow:Reloading Tuner from .\structured_data_classifier\tuner0.json
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-106-94708e5d279d> in <module>
10 search = StructuredDataClassifier(max_trials=15)
11 # perform the search
---> 12 search.fit(x=(X_train), y=y_train, verbose=0)
13 # evaluate the model
14 loss, acc = search.evaluate(X_test, y_test, verbose=0)
~\anaconda3\lib\site-packages\autokeras\tasks\structured_data.py in fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs)
313 [keras.Model.fit](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit).
314 """
--> 315 super().fit(
316 x=x,
317 y=y,
~\anaconda3\lib\site-packages\autokeras\tasks\structured_data.py in fit(self, x, y, epochs, callbacks, validation_split, validation_data, **kwargs)
132 self.check_in_fit(x)
133
--> 134 super().fit(
135 x=x,
136 y=y,
~\anaconda3\lib\site-packages\autokeras\auto_model.py in fit(self, x, y, batch_size, epochs, callbacks, validation_split, validation_data, **kwargs)
259 validation_split = 0
260
--> 261 dataset, validation_data = self._convert_to_dataset(
262 x=x, y=y, validation_data=validation_data, batch_size=batch_size
263 )
~\anaconda3\lib\site-packages\autokeras\auto_model.py in _convert_to_dataset(self, x, y, validation_data, batch_size)
373 x = dataset.map(lambda x, y: x)
374 y = dataset.map(lambda x, y: y)
--> 375 x = self._adapt(x, self.inputs, batch_size)
376 y = self._adapt(y, self._heads, batch_size)
377 dataset = tf.data.Dataset.zip((x, y))
~\anaconda3\lib\site-packages\autokeras\auto_model.py in _adapt(self, dataset, hms, batch_size)
287 adapted = []
288 for source, hm in zip(sources, hms):
--> 289 source = hm.get_adapter().adapt(source, batch_size)
290 adapted.append(source)
291 if len(adapted) == 1:
~\anaconda3\lib\site-packages\autokeras\engine\adapter.py in adapt(self, dataset, batch_size)
65 tf.data.Dataset. The converted dataset.
66 """
---> 67 self.check(dataset)
68 dataset = self.convert_to_dataset(dataset, batch_size)
69 return dataset
~\anaconda3\lib\site-packages\autokeras\adapters\input_adapters.py in check(self, x)
63 def check(self, x):
64 if not isinstance(x, (pd.DataFrame, np.ndarray, tf.data.Dataset)):
---> 65 raise TypeError(
66 "Unsupported type {type} for "
67 "{name}.".format(type=type(x), name=self.__class__.__name__)
TypeError: Unsupported type <class 'scipy.sparse.csr.csr_matrix'> for StructuredDataAdapter.
解决方案
正如您在与此线程并行打开的Github 问题中所注意到的,AutoKeras (当前)不支持稀疏矩阵,建议将它们转换为密集的 Numpy 数组。事实上,从AutoKeras的文档来看,相应方法中StructuredDataClassifier
的训练数据预计为:x
.fit
字符串、numpy.ndarray、pandas.DataFrame 或 tensorflow.Dataset
而不是 SciPy 稀疏矩阵。
鉴于这里你X_train
真的很小:
X_train.shape
# (80, 92)
您绝对没有理由使用稀疏矩阵。尽管在这里您似乎试图转换X_train
为密集的,但您没有重新分配它,结果是它仍然是稀疏的;从上面你自己的代码:
X_train.todense()
# ...
type(X_train)
# scipy.sparse.csr.csr_matrix
您需要做的只是将其重新分配给密集数组:
from scipy.sparse import csr_matrix
X_train = X_train.toarray()
这是一个简短的演示,它适用于虚拟数据:
import numpy as np
from scipy.sparse import csr_matrix
X_train = csr_matrix((3, 4), dtype=np.float)
type(X_train)
# scipy.sparse.csr.csr_matrix
# this will not work:
X_train.todense()
type(X_train)
# scipy.sparse.csr.csr_matrix # still sparse
# this will work:
X_train = X_train.toarray()
type(X_train)
# numpy.ndarray
X_test
您应该对您的数据遵循类似的程序(您的y_train
并且y_test
似乎已经是密集的 Numpy 数组)。
推荐阅读
- sql - SQL 日期转换 HHMMSS.CCCNNNNNN 到 yyyy-mm-dd hh:mi:ss.mmm
- azure - 如何翻译应用程序授权窗口 Azure AD
- javascript - NodeJS 返回 SQL 查询以传递给另一个文件
- javascript - 角度模式形式的数组长度
- php - 尝试在自定义论坛上发布新主题时出现空白页面
- bash - bsub 命令内的输出重定向
- ios - #if canImport(module) 仍然无法解决 Swift 4.1 中的条件导入语句?
- c# - 试图在 iqueryable any 中进行切换
- amazon-web-services - 通过自动创建新的从机来自动缩放 Jenkins 实例
- facebook - Ionic-Native、Angular 6、Ionic 4 - Facebook 和 Google 登录问题 - 对象不是函数