- from sklearn.feature_extraction.text import TfidfVectorizer
- from sklearn.model_selection import train_test_split
- from sklearn.naive_bayes import MultinomialNB
- from sklearn.metrics import confusion_matrix, classification_report
- # Load train data (replace with your actual data)
- train_data = pd.read_csv("train.csv")
- # Preprocess text data
- vectorizer = TfidfVectorizer(max_features=1000)
- X_train = vectorizer.fit_transform(train_data["text"])
- y_train = train_data["target"]
- # Split data into train and validation sets
- X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
- # Train Naive Bayes classifier
- clf = MultinomialNB()
- clf.fit(X_train, y_train)
- # Make predictions on validation set
- y_pred = clf.predict(X_val)
- # Create a confusion matrix
- conf_matrix = confusion_matrix(y_val, y_pred)
- # Print the confusion matrix and classification report
- print("Confusion Matrix:")
- print(conf_matrix)
- print("\nClassification Report:")
- print(classification_report(y_val, y_pred))
[text] K
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: