Search code examples
nlplogistic-regression

How do we predict 1 single Datapoint with LogisticRegression


I have been working on an NLP problem (text classification).

I first preprocessed the text, then trained a model on that data (After Tfidf)

X_train, X_test, y_train, y_test = train_test_split(ref_red, uniqueOutput...)

clf = LogisticRegression(C = 0.9, penalty = 'l1', solver = 'liblinear') # Grid Search    
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)

print(accuracy_score(y_pred, y_test))

Now, I want to try and test the model on a single datapoint (ML inference). How do pass a single datapt to predict function?

Please let me know.


Solution

  • The only thing to keep in mind is dimension agreement between Training and Inference data.

    X, y = make_classification()
    X_train, X_test, y_train, y_test = train_test_split(X, y)
    dim = X_train.shape[-1]
    clf = LogisticRegression(C = 0.9, penalty = 'l1', solver = 'liblinear') # Grid Search    
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    sample_count = 1
    clf.predict(np.random.rand(sample_count, dim))