首页 > 解决方案 > 将迭代器传递给拟合/训练/预测函数 - 可能吗?

问题描述

我想知道是否有办法将迭代器传递给那些 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'

标签: scikit-learntensorflow-datasets

解决方案


下面是使用存储在列表中的数据来拟合和测试模型的示例:

    # 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)))

推荐阅读