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": [18000,19000,20000,21000,22000], 'clf':[LogisticRegression(max_iter=30000), RandomForestClassifier(class_weight='balanced_subsample'), ] }
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) Как сюда добавить kmeans? И какие нибудь еще методы помимо логистической регрессии
- Assistant
To add kmeans, you can add it as another step in the pipeline before the classifier
Here's an example of adding kmeans with 50 clusters:
from sklearn.cluster import KMeans clf = Pipeline([ ('vect', CountVectorizer(max_features=4500,ngram_range=(1,2))), ('tfidf', TfidfTransformer()), ('kmeans', KMeans(n_clusters=50)), # added kmeans step ('clf', RandomForestClassifier(class_weight='balanced_subsample')) ])
To add other methods besides logistic regression, you can add them to the 'clf' parameter grid:
parameters = { "vect__ngram_range": [(1,1),(1,2),(1,3)], "vect__max_features": [18000,19000,20000,21000,22000], 'clf':[LogisticRegression(max_iter=30000), RandomForestClassifier(class_weight='balanced_subsample'), # add more classifiers here ] }
Then use GridSearchCV as before to search for best hyperparameters
- User
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=21000,ngram_range=(1,1))), # биграммы max_features=4500, ('tfidf', TfidfTransformer()), #(n_components=60, n_iter=12, random_state=42) ('clf',KMeans()) #RandomForestClassifier(class_weight='balanced')LogisticRegression() ])
clf.fit(X_train,y_train)
y_pred = clf.predict(X_test)
print(classification_report(y_test, y_pred)) ValueError: Mix of label input types (string and number)
- Assistant
The error message indicates that there's a mix of different types of labels, which causes
classification_reportto fail. It's likely that the labels in youry_trainandy_testare different types - one might be strings and the other might be numbers.To fix this, you can ensure that the labels for both training and testing sets have the same type. One way to do this is to convert the labels to the same type using the
astypemethod. For example:X_train, X_test, y_train, y_test = train_test_split(dd['lemma'], dd['category'].astype(str), test_size=0.2)Here,
astype(str)converts all the labels incategoryto strings, ensuring that there's no mix of different types. You may need to adjust the conversion depending on the type of your original labels.Also note that using KMeans as a classifier like this is unusual - KMeans is typically used for unsupervised learning and clustering, not classification. If you're trying to fit a supervised classification model, you may want to consider using a different classifier, such as RandomForestClassifier or LogisticRegression.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.