pythonmachine-learningpcasvdtruncated

How to save a trunckated svd model in python


I am working on a machine learning project. I have applied truncated svd on my data for feature reduction and then trained the neural networks on that data. I have saved the neural network model using to_json(). How can I save the truncated svd model for transforming unseen data later.

Here is how I have used truncated svd.

from sklearn.decomposition import TruncatedSVD
model = TruncatedSVD(n_components=600,n_iter=10).fit(train_features)
train_features= model.transform(train_features)
test_features= model.transform(test_features)`

I hope that I made this question clear. Can someone assist me in solving this question?


Solution

  • You can use the python pickle library

    import pickle
    # To save
    pickle.dump(model, "model.p")
    # To load again
    with open('model.p', 'r') as fp:
        model = pickle.load(fp)