Why the result of score() and accuracy_score() are different?

27 Views Asked by At

I was doing classification with Support Vector Machine (SVM)

And

df=pd.read_csv('mobile.csv')
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=42)

model = SVC()
model.fit(x_train, y_train)
model.score(x_test, y_test)
accuracy = accuracy_score(y_test, y_pred)
accuracy

model.score(x_test, y_test) result was 0.9533333333333334 And accuracy score was also 0.9533333333333334

And then I tried to make better with hyperparameter.

y_pred = model.predict(x_test)

C_values = [0.001, 0.01, 0.1, 1, 10, 100]

train_scores = []
test_scores = []

for C in C_values:
    model = SVC(C=C)
    model.fit(x_train, y_train)
    train_scores.append(model.score(x_train, y_train))
    test_scores.append(model.score(x_test, y_test))

model.score(x_test, y_test)

And then I could find model.score(x_test, y_test) result became 0.9716666666666667

accuracy

However, the accuracy score hasn't change, it was still 0.9533333333333334

I'm not sure this is normal or not.

Do you have idea why this happened?

I thought the result of score() and accuracy_score() are always have same value.

But it was not for now

I'm not sure if there is something wrong or not.

I checked the time when score() value change was after using hyperparameter. So I expected that something is wrong of the processing of hyperparameter.

1

There are 1 best solutions below

5
Federicofkt On

The method accuracy_score() takes as input the test and the predictions. The problem here is that you are computing the predictions before the loop with the tuning.

Also, model.score() outside the for loop doesn't make much sense, as it will use the last tuned model (that could not be the best one).

You can nest it inside the loop in order to print each result, together with computing the predictions and print also the accuracy, that will be the same as the model.score()