Created
August 22, 2022 11:17
-
-
Save andrea-dagostino/a0b59182bbf1c68c1dd4c6853de8537c to your computer and use it in GitHub Desktop.
overfitting_example_ita
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| train_accs = [] | |
| test_accs = [] | |
| cols = [ | |
| 'fixed.acidity', 'volatile.acidity', 'citric.acid','residual.sugar', 'chlorides', 'free.sulfur.dioxide', | |
| 'total.sulfur.dioxide', 'density', 'pH', 'sulphates', 'alcohol', | |
| ] | |
| # inizializziamo un loop dove cambieremo il valore di max depth, partendo da 1 a 25 | |
| for depth in range(1, 25): | |
| clf = tree.DecisionTreeClassifier(max_depth=depth) | |
| clf.fit(df_train[cols], df_train.quality) | |
| train_predictions = clf.predict(df_train[cols]) | |
| test_predictions = clf.predict(df_test[cols]) | |
| train_acc = metrics.accuracy_score(df_train.quality, train_predictions) | |
| test_acc = metrics.accuracy_score(df_test.quality, test_predictions) | |
| # inseriamo in liste vuote le nostre accuracies | |
| train_accs.append(train_acc) | |
| test_accs.append(test_acc) | |
| # visualizziamo i dati | |
| plt.figure(figsize=(10, 5)) | |
| sns.set_style('whitegrid') | |
| plt.plot(train_accs, label='train accuracy') | |
| plt.plot(test_accs, label='test accuracy') | |
| plt.legend(loc='upper left', prop={'size': 15}) | |
| plt.xticks(range(0, 26, 5)) | |
| plt.xlabel('max_depth', size=20) | |
| plt.ylabel('accuracy', size=20) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment