[text] K

Viewer

  1. from sklearn.feature_extraction.text import TfidfVectorizer
  2. from sklearn.model_selection import train_test_split
  3. from sklearn.naive_bayes import MultinomialNB
  4. from sklearn.metrics import confusion_matrix, classification_report
  5.  
  6. # Load train data (replace with your actual data)
  7. train_data = pd.read_csv("train.csv")
  8.  
  9. # Preprocess text data
  10. vectorizer = TfidfVectorizer(max_features=1000)
  11. X_train = vectorizer.fit_transform(train_data["text"])
  12. y_train = train_data["target"]
  13.  
  14. # Split data into train and validation sets
  15. X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
  16.  
  17. # Train Naive Bayes classifier
  18. clf = MultinomialNB()
  19. clf.fit(X_train, y_train)
  20.  
  21. # Make predictions on validation set
  22. y_pred = clf.predict(X_val)
  23.  
  24. # Create a confusion matrix
  25. conf_matrix = confusion_matrix(y_val, y_pred)
  26.  
  27. # Print the confusion matrix and classification report
  28. print("Confusion Matrix:")
  29. print(conf_matrix)
  30. print("\nClassification Report:")
  31. print(classification_report(y_val, y_pred))
  32.  

Editor

You can edit this paste and save as new:


File Description
  • K
  • Paste Code
  • 27 Apr-2024
  • 1.03 Kb
You can Share it: