Machine Learning Practice, Quiz 2
You are working with a dataset containing 1000 samples, aiming to classify them using the KNeighborsClassifier from scikit-learn. After trying an initial configuration, you observe that the model seems to be overfitting, with the following accuracies:
from sklearn.neighbors import KNeighborsClassifierfrom sklearn.metrics import accuracy_score
# Initial Configurationknn = KNeighborsClassifier(n_neighbors=3)knn.fit(X_train, y_train)train_acc = accuracy_score(y_train, knn.predict(X_train))val_acc = accuracy_score(y_val, knn.predict(X_val))After observing such performance of the model, Which of the following values for n_neighbors would be more suitable to try next?
You are working with a dataset containing 1000 samples, aiming to classify them using the `KNeighborsClassifier` from `scikit-learn`. After trying an initial configuration, you observe that the model seems to be overfitting, with the following accuracies: from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score # Initial Configuration knn = KNeighborsClassifier(n_neighbors=3) knn.fit(X_train, y_train) train_acc = accuracy_score(y_train, knn.predict(X_train)) val_acc = accuracy_score(y_val, knn.predict(X_val)) - Training accuracy: 98% - Validation accuracy: 65% After observing such performance of the model, Which of the following values for `n_neighbors` would be more suitable to try next? Consider the following code segment which uses `CountVectorizer` on a set of documents: from sklearn.feature_extraction.text import CountVectorizer documents = [ 'apple orange banana', 'apple apple', 'banana orange', 'apple banana orange orange' ] vectorizer = CountVectorizer() X = vectorizer.fit_transform(documents) After executing the code, what will be the shape of matrix `X`? Assume train data (X_train, y_train) and test data (X_test) is given as numpy array and you build and train a LogisticRegression model. Which of the following options might possibly be the predicted class of first two samples(rows) of the test data according to the code given below? >>> from sklearn.linear_model import LogisticRegression >>> log_reg = LogisticRegression() >>> log_reg.fit(X_train,y_train) >>> print(log_reg.classes_) [0,1,2] #output of above code >>> print(log_reg.predict_proba(X_test[[0]])) [[2.73e-45, 1.21e-51, 1.00e+00]] #output of above code >>> print(log_reg.predict_proba(X_test[[1]])) [[7.09e-29, 1.00e+00, 2.02e-36]] #output of above code >>> print(log_reg.predict(X_test[0:2]))