scikit-learn - 将迭代器传递给拟合/训练/预测函数 - 可能吗?
问题描述
我想知道是否有办法将迭代器传递给那些 varius sk 模型,例如:随机森林/逻辑回归等。
我有一个张量流数据集,可以从那里获取一个 numpy 迭代器,但不能在这些函数中使用它。
任何解决方案?
xs = tfds.as_numpy(tf.data.Dataset.from_tensor_slices(xs))
ys = tfds.as_numpy(tf.data.Dataset.from_tensor_slices(ys))
然后拟合模型:
cls.fit(xs, ys)
导致:
TypeError: float() argument must be a string or a number, not '_IterableDataset'
解决方案
下面是使用存储在列表中的数据来拟合和测试模型的示例:
# Import some libraries
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
# Make some generic data
first_data, first_classes = make_classification(n_samples=100, n_features=5, random_state=1)
second_data, second_classes = make_classification(n_samples=100, n_features=5, random_state=2)
third_data, third_classes = make_classification(n_samples=100, n_features=5, random_state=3)
# Save data and classes into a list
data = [first_data, second_data, third_data]
classes = [first_classes, second_classes, third_classes]
# Declare a logistic regression instance
model = LogisticRegression()
for i in range(len(data)):
# Split data into training and test
X_train, X_test, y_train, y_test = train_test_split(data[i], classes[i], test_size=0.15)
# Fit the model
model.fit(X_train, y_train)
# Print results
print("{} Dataset | Score: {}".format(i+1, model.score(X_test, y_test)))
推荐阅读
- tableau-api - Percentile_rank 计算作为计数计算的过滤器
- php - RSA 签名验证 - OpenSSL 与 phpseclib
- visual-studio - 用于 linux 的 windows 子系统上的外部可执行文件的 launch.vs.json 文件配置
- powershell - 通过 cmd.exe 调用的 powershell.exe 命令中的嵌套 cmd.exe 命令
- salesforce - Salesforce 批量 API 删除运算符
- postgresql - 将文本转换为 UTC 时间戳
- nginx - 在 Ubuntu 服务器上安装 Openresty
- javascript - DataTables 仅打印选定的选项
- laravel - Laravel 用户和多态性
- reactjs - 在 FlatList 中渲染的组件会被重新渲染,即使它们被声明为 PureComponent 并且它们的 props 引用没有改变