Excercise: Intent classification with sklearn

Intent classification with sklearn

Intent classification with sklearn An array X containing vectors describing each of the sentences in the ATIS dataset

has been created for you, along with a 1D array y containing the labels. The labels are integers corresponding to the

intents in the dataset. For example, label 0 corresponds to the intent atis_flight.

Now, you'll use the scikit-learn library to train a classifier on this same dataset. Specifically, you will fit and evaluate a support vector classifier.

Instructions

Import the SVC class from sklearn.svm. Instantiate a classifier clf by calling SVC with a single keyword argument C with value 1.

Fit the classifier to the training data X_train and y_train. Predict the labels of the test set, X_test.

import pandas as pd
import numpy as np
import spacy
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

np.random.seed(42)


# importing heart_disease dataset
heart_disease = pd.read_csv("heart-disease.csv")
X = heart_disease.drop("target", axis=1)
y = heart_disease["target"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Create a support vector classifier
clf = SVC()
# Fit the classifier using the training data
clf.fit(X_train, y_train)

# Predict the labels of the test set
y_preds = clf.predict(X_test)

# converting y_test into an array to compare with y_preds
y_test = np.array(y_test)

# Count the number of correct predictions
n_correct = 0
for i in range(len(y_test)):
    if y_preds[i] == np.array(y_test[i]):
        n_correct += 1

print("Predicted {0} correctly out of {1} test examples".format(n_correct, len(y_test)))

 

Last modified: Friday, 17 February 2023, 4:56 PM