Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
965 views
in Technique[技术] by (71.8m points)

scikit learn - passing an iterator to fit/train/predict functions - is it possible?

i wonder if theres a way to pass an iterator like into those varius sk models for example: random-forest/logistic regression etc.

i have a tensor flow dataset can fetch from there a numpy iterator but cannot use it in those functions.

any solution?

xs = tfds.as_numpy(tf.data.Dataset.from_tensor_slices(xs))
ys = tfds.as_numpy(tf.data.Dataset.from_tensor_slices(ys))

then fitting the model:

cls.fit(xs, ys)

causing:

TypeError: float() argument must be a string or a number, not '_IterableDataset'
question from:https://stackoverflow.com/questions/65938535/passing-an-iterator-to-fit-train-predict-functions-is-it-possible

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An example of fitting and testing a model with your data stored in a list is below:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...