from sklearn.pipeline import Pipeline from sklearn.ensemble import…
- 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 добавить помимо RandomForestClassifier еще и LogisticRegression используя при этом один gs_clf
- Assistant
для обоих моделей. Вот код:
Create a combined pipeline with both RandomForestClassifier and LogisticRegression
clf = Pipeline([ ('vect', CountVectorizer(ngram_range=(1,2))), ('tfidf', TfidfTransformer()), ('reduce_dim', TruncatedSVD()), ('clf', None) ])
Define parameter grid for GridSearchCV
parameters = { 'vect__max_df': [0.8, 1.0], 'vect__max_features': [5000, 10000, 15000], 'vect__ngram_range': [(1,1), (1,2)], 'tfidf__use_idf': [True, False], 'tfidf__norm': ['l1', 'l2'], 'reduce_dim__n_components': [50, 100, 200], 'clf': [RandomForestClassifier(), LogisticRegression()] }
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 for RandomForestClassifier
print('Best hyperparameters for RandomForestClassifier:') print(gs_clf.best_params_)
Print the best hyperparameters for LogisticRegression
print('Best hyperparameters for LogisticRegression:') print(gs_clf.best_params_)