Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

from sklearn.pipeline import Pipeline from sklearn.ensemble import…

  1. User

    from sklearn.pipeline import Pipeline from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer,TfidfTransformer from sklearn.metrics import r2_score,classification_report,precision_score,recall_score,accuracy_score,f1_score from sklearn.decomposition import TruncatedSVD from sklearn.linear_model import LogisticRegression from sklearn.model_selection import GridSearchCV

    dd = train_data

    #dd = responcies_df[(responcies_df['rating_grade']==5) | (responcies_df['rating_grade']==1)]

    X_train, X_test, y_train, y_test = train_test_split(dd['lemma'],dd['category'],test_size=0.2);

    clf = Pipeline([ ('vect', CountVectorizer(max_features=4500,ngram_range=(1,2))), # биграммы max_features=4500, ('tfidf', TfidfTransformer()), #(n_components=60, n_iter=12, random_state=42) ('clf', RandomForestClassifier(class_weight='balanced_subsample')) ])

    Define parameter grid for GridSearchCV

    parameters = { "vect__ngram_range": [(1,1),(1,2),(1,3)], # unigram, bigram, trigram "vect__max_features": [3000,3500,4000,4500,6000], "clf__n_estimators": [50,100,150,200] }

    Use GridSearchCV to search for the best hyperparameters using the specified parameter grid

    gs_clf = GridSearchCV(clf, parameters, cv=5, n_jobs=1, verbose=3)

    Fit the GridSearchCV object with training data to find the best hyperparameters

    gs_clf.fit(X_train,y_train)

    Print the best hyperparameters

    print(gs_clf.best_params_) Нужно в GridSearchCV добавить LogisticRegression

  2. Assistant

    To add LogisticRegression to GridSearchCV, you need to specify it as one of the steps in the Pipeline object and define its hyperparameters in the parameters dictionary. Here's an updated code snippet:

    clf = Pipeline([ 
        ('vect', CountVectorizer()), 
        ('tfidf', TfidfTransformer()),
        ('reduce_dim', TruncatedSVD()),
        ('clf', LogisticRegression(class_weight='balanced'))
    ])
    
    # Define parameter grid for GridSearchCV
    parameters = {
        'vect__ngram_range': [(1, 1), (1, 2)],
        'vect__max_features': [3000, 4000, 5000],
        'reduce_dim__n_components': [50, 100],
        'clf__penalty': ['l1', 'l2'],
        'clf__C': [1, 10, 100],
    }
    
    # Use GridSearchCV to search for the best hyperparameters using the specified parameter grid
    gs_clf = GridSearchCV(clf, parameters, cv=5, n_jobs=1, verbose=3)
    
    # Fit the GridSearchCV object with training data to find the best hyperparameters
    gs_clf.fit(X_train, y_train)
    
    # Print the best hyperparameters
    print(gs_clf.best_params_)
    

    In this example, I've added TruncatedSVD for dimensionality reduction and included hyperparameters for it in the parameters dictionary. I've also defined hyperparameters for LogisticRegression (penalty and C). You can adjust these hyperparameters based on your specific use case.