class_docstr: Intent classifier using the sklearn framework.
class_docstr_tok: ["Intent","classifier","using","the","sklearn","framework","."]
docstr: Train the intent classifier on a data set.
docstr_tok: ["Train","the","intent","classifier","on","a","data","set","."]
code: def train(self, training_data: TrainingData) -> Resource:
      """Train the intent classifier on a data set."""
      num_threads = self.component_config["num_threads"]
  
      labels = [e.get("intent") for e in training_data.intent_examples]
  
      if len(set(labels)) < 2:
          rasa.shared.utils.io.raise_warning(
              "Can not train an intent classifier as there are not "
              "enough intents. Need at least 2 different intents. "
              "Skipping training of intent classifier.",
              docs=DOCS_URL_TRAINING_DATA_NLU,
          )
          return self._resource
  
      y = self.transform_labels_str2num(labels)
      training_examples = [
          message
          for message in training_data.intent_examples
          if message.features_present(
              attribute=TEXT,
              featurizers=self.component_config.get(FEATURIZERS),
          )
      ]
      X = np.stack(
          [
              self._get_sentence_features(example)
              for example in training_examples
          ]
      )
      # reduce dimensionality
      X = np.reshape(X, (len(X), -1))
  
      self.clf = self._create_classifier(num_threads, y)
  
      with warnings.catch_warnings():
          # sklearn raises lots of
          # "UndefinedMetricWarning: F - score is ill - defined"
          # if there are few intent examples, this is needed to prevent it
          warnings.simplefilter("ignore")
          self.clf.fit(X, y)
  
      self.persist()
      return self._resource
  
imports: from __future__ import annotations
  import logging
  from rasa.nlu.featurizers.dense_featurizer.dense_featurizer import DenseFeaturizer
  import typing
  import warnings
  from typing import Any, Dict, List, Optional, Text, Tuple, Type
  import numpy
  import rasa.shared.utils.io
  import rasa.utils.io
  from rasa.engine.graph import GraphComponent, ExecutionContext
  from rasa.engine.recipes.default_recipe import DefaultV1Recipe
  from rasa.engine.storage.resource import Resource
  from rasa.engine.storage.storage import ModelStorage
  from rasa.shared.constants import DOCS_URL_TRAINING_DATA_NLU
  from rasa.nlu.classifiers import LABEL_RANKING_LENGTH
  from rasa.shared.exceptions import RasaException
  from rasa.shared.nlu.constants import TEXT
  from rasa.nlu.classifiers.classifier import IntentClassifier
  from rasa.shared.nlu.training_data.training_data import TrainingData
  from rasa.shared.nlu.training_data.message import Message
  from rasa.utils.tensorflow.constants import FEATURIZERS
label_desc: Use image node_id 8 for calling the SklearnIntentClassifier obj's underlying member method code with example usage: obj.train(training_data) and returns: self, self