Notes
Notes - notes.io |
"""Train the classifier on the provided data"""
# Preprocess texts
preprocessed_texts = [self.preprocess_text(text) for text in texts]
# Split data for training and evaluation
X_train, X_test, y_train, y_test = train_test_split(
preprocessed_texts, labels, test_size=0.2, random_state=42
)
# Create and fit vectorizer ONLY on training data
self.vectorizer = TfidfVectorizer(
max_features=5000,
min_df=2,
max_df=0.85,
ngram_range=(1, 2)
)
X_train_vec = self.vectorizer.fit_transform(X_train)
# Transform test data with the SAME vectorizer (no fitting)
X_test_vec = self.vectorizer.transform(X_test)
# Topic modeling features (optional)
if use_topic_modeling:
print("Building topic model...")
# Important: Build topic model on ALL data to get consistent topics
self.build_topic_model(texts)
# Create consistent indices for extracting topic features
train_indices = [i for i, _ in enumerate(preprocessed_texts) if _ in X_train]
test_indices = [i for i, _ in enumerate(preprocessed_texts) if _ in X_test]
# Extract topic features using the SAME topic model
all_topic_features = self.extract_topic_features(texts)
train_topic_features = np.array([all_topic_features[i] for i in train_indices])
test_topic_features = np.array([all_topic_features[i] for i in test_indices])
# Combine with TF-IDF features
X_train_combined = np.hstack((X_train_vec.toarray(), train_topic_features))
X_test_combined = np.hstack((X_test_vec.toarray(), test_topic_features))
else:
X_train_combined = X_train_vec
X_test_combined = X_test_vec
# Train models
print("Training machine learning models...")
models = {
'Naive Bayes': MultinomialNB(),
'SVM': LinearSVC(C=1.0, class_weight='balanced', max_iter=10000),
'Random Forest': RandomForestClassifier(n_estimators=100, random_state=42)
}
best_model = None
best_score = 0
for name, model in models.items():
print(f"Training {name}...")
model.fit(X_train_combined, y_train)
# Always use transform() (not fit_transform()) on test data
y_pred = model.predict(X_test_combined)
accuracy = accuracy_score(y_test, y_pred)
print(f"{name} - Accuracy: {accuracy:.4f}")
if accuracy > best_score:
best_score = accuracy
best_model = model
# Save the best model
self.ml_model = best_model
print(f"Best model selected: {type(best_model).__name__} with accuracy {best_score:.4f}")
# Evaluate on test set - use the built models for prediction
self.evaluate(X_test, y_test)
return self
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
