Skip to content

Commit 8cf78cb

Browse files
committed
Pushing the docs to dev/ for branch: master, commit e650a207efc9dd33556b1b9678b043f73a18aecb
1 parent 6fa58ff commit 8cf78cb

File tree

1,231 files changed

+4707
-4030
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,231 files changed

+4707
-4030
lines changed
Binary file not shown.

dev/_downloads/3a6b0f407d8829a616b0eff2bf71828a/plot_confusion_matrix.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn import svm, datasets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nfrom sklearn.utils.multiclass import unique_labels\n\n# import some data to play with\niris = datasets.load_iris()\nX = iris.data\ny = iris.target\nclass_names = iris.target_names\n\n# Split the data into a training set and a test set\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n\n# Run classifier, using a model that is too regularized (C too low) to see\n# the impact on the results\nclassifier = svm.SVC(kernel='linear', C=0.01)\ny_pred = classifier.fit(X_train, y_train).predict(X_test)\n\n\ndef plot_confusion_matrix(y_true, y_pred, classes,\n normalize=False,\n title=None,\n cmap=plt.cm.Blues):\n \"\"\"\n This function prints and plots the confusion matrix.\n Normalization can be applied by setting `normalize=True`.\n \"\"\"\n if not title:\n if normalize:\n title = 'Normalized confusion matrix'\n else:\n title = 'Confusion matrix, without normalization'\n\n # Compute confusion matrix\n cm = confusion_matrix(y_true, y_pred)\n # Only use the labels that appear in the data\n classes = classes[unique_labels(y_true, y_pred)]\n if normalize:\n cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]\n print(\"Normalized confusion matrix\")\n else:\n print('Confusion matrix, without normalization')\n\n print(cm)\n\n fig, ax = plt.subplots()\n im = ax.imshow(cm, interpolation='nearest', cmap=cmap)\n ax.figure.colorbar(im, ax=ax)\n # We want to show all ticks...\n ax.set(xticks=np.arange(cm.shape[1]),\n yticks=np.arange(cm.shape[0]),\n # ... and label them with the respective list entries\n xticklabels=classes, yticklabels=classes,\n title=title,\n ylabel='True label',\n xlabel='Predicted label')\n\n # Rotate the tick labels and set their alignment.\n plt.setp(ax.get_xticklabels(), rotation=45, ha=\"right\",\n rotation_mode=\"anchor\")\n\n # Loop over data dimensions and create text annotations.\n fmt = '.2f' if normalize else 'd'\n thresh = cm.max() / 2.\n for i in range(cm.shape[0]):\n for j in range(cm.shape[1]):\n ax.text(j, i, format(cm[i, j], fmt),\n ha=\"center\", va=\"center\",\n color=\"white\" if cm[i, j] > thresh else \"black\")\n fig.tight_layout()\n return ax\n\n\nnp.set_printoptions(precision=2)\n\n# Plot non-normalized confusion matrix\nplot_confusion_matrix(y_test, y_pred, classes=class_names,\n title='Confusion matrix, without normalization')\n\n# Plot normalized confusion matrix\nplot_confusion_matrix(y_test, y_pred, classes=class_names, normalize=True,\n title='Normalized confusion matrix')\n\nplt.show()"
29+
"print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nfrom sklearn import svm, datasets\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import plot_confusion_matrix\n\n# import some data to play with\niris = datasets.load_iris()\nX = iris.data\ny = iris.target\nclass_names = iris.target_names\n\n# Split the data into a training set and a test set\nX_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)\n\n# Run classifier, using a model that is too regularized (C too low) to see\n# the impact on the results\nclassifier = svm.SVC(kernel='linear', C=0.01).fit(X_train, y_train)\n\nnp.set_printoptions(precision=2)\n\n# Plot non-normalized confusion matrix\ntitles_options = [(\"Confusion matrix, without normalization\", None),\n (\"Normalized confusion matrix\", 'true')]\nfor title, normalize in titles_options:\n disp = plot_confusion_matrix(classifier, X_test, y_test,\n display_labels=class_names,\n cmap=plt.cm.Blues,\n normalize=normalize)\n disp.ax_.set_title(title)\n\n print(title)\n print(disp.confusion_matrix)\n\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/622fb50f5e367eda84eb7c32d306f659/plot_digits_classification.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"print(__doc__)\n\n# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>\n# License: BSD 3 clause\n\n# Standard scientific Python imports\nimport matplotlib.pyplot as plt\n\n# Import datasets, classifiers and performance metrics\nfrom sklearn import datasets, svm, metrics\nfrom sklearn.model_selection import train_test_split\n\n# The digits dataset\ndigits = datasets.load_digits()\n\n# The data that we are interested in is made of 8x8 images of digits, let's\n# have a look at the first 4 images, stored in the `images` attribute of the\n# dataset. If we were working from image files, we could load them using\n# matplotlib.pyplot.imread. Note that each image must have the same size. For these\n# images, we know which digit they represent: it is given in the 'target' of\n# the dataset.\nimages_and_labels = list(zip(digits.images, digits.target))\nfor index, (image, label) in enumerate(images_and_labels[:4]):\n plt.subplot(2, 4, index + 1)\n plt.axis('off')\n plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n plt.title('Training: %i' % label)\n\n# To apply a classifier on this data, we need to flatten the image, to\n# turn the data in a (samples, feature) matrix:\nn_samples = len(digits.images)\ndata = digits.images.reshape((n_samples, -1))\n\n# Create a classifier: a support vector classifier\nclassifier = svm.SVC(gamma=0.001)\n\n# Split data into train and test subsets\nX_train, X_test, y_train, y_test = train_test_split(\n data, digits.target, test_size=0.5, shuffle=False)\n\n# We learn the digits on the first half of the digits\nclassifier.fit(X_train, y_train)\n\n# Now predict the value of the digit on the second half:\npredicted = classifier.predict(X_test)\n\nprint(\"Classification report for classifier %s:\\n%s\\n\"\n % (classifier, metrics.classification_report(y_test, predicted)))\nprint(\"Confusion matrix:\\n%s\" % metrics.confusion_matrix(y_test, predicted))\n\nimages_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))\nfor index, (image, prediction) in enumerate(images_and_predictions[:4]):\n plt.subplot(2, 4, index + 5)\n plt.axis('off')\n plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n plt.title('Prediction: %i' % prediction)\n\nplt.show()"
29+
"print(__doc__)\n\n# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>\n# License: BSD 3 clause\n\n# Standard scientific Python imports\nimport matplotlib.pyplot as plt\n\n# Import datasets, classifiers and performance metrics\nfrom sklearn import datasets, svm, metrics\nfrom sklearn.model_selection import train_test_split\n\n# The digits dataset\ndigits = datasets.load_digits()\n\n# The data that we are interested in is made of 8x8 images of digits, let's\n# have a look at the first 4 images, stored in the `images` attribute of the\n# dataset. If we were working from image files, we could load them using\n# matplotlib.pyplot.imread. Note that each image must have the same size. For these\n# images, we know which digit they represent: it is given in the 'target' of\n# the dataset.\n_, axes = plt.subplots(2, 4)\nimages_and_labels = list(zip(digits.images, digits.target))\nfor ax, (image, label) in zip(axes[0, :], images_and_labels[:4]):\n ax.set_axis_off()\n ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n ax.set_title('Training: %i' % label)\n\n# To apply a classifier on this data, we need to flatten the image, to\n# turn the data in a (samples, feature) matrix:\nn_samples = len(digits.images)\ndata = digits.images.reshape((n_samples, -1))\n\n# Create a classifier: a support vector classifier\nclassifier = svm.SVC(gamma=0.001)\n\n# Split data into train and test subsets\nX_train, X_test, y_train, y_test = train_test_split(\n data, digits.target, test_size=0.5, shuffle=False)\n\n# We learn the digits on the first half of the digits\nclassifier.fit(X_train, y_train)\n\n# Now predict the value of the digit on the second half:\npredicted = classifier.predict(X_test)\n\nimages_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))\nfor ax, (image, prediction) in zip(axes[1, :], images_and_predictions[:4]):\n ax.set_axis_off()\n ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n ax.set_title('Prediction: %i' % prediction)\n\nprint(\"Classification report for classifier %s:\\n%s\\n\"\n % (classifier, metrics.classification_report(y_test, predicted)))\ndisp = metrics.plot_confusion_matrix(classifier, X_test, y_test)\ndisp.figure_.suptitle(\"Confusion Matrix\")\nprint(\"Confusion matrix:\\n%s\" % disp.confusion_matrix)\n\nplt.show()"
3030
]
3131
}
3232
],

dev/_downloads/7e0e90df87894c2fadaf3004c5316545/plot_confusion_matrix.py

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131

3232
from sklearn import svm, datasets
3333
from sklearn.model_selection import train_test_split
34-
from sklearn.metrics import confusion_matrix
35-
from sklearn.utils.multiclass import unique_labels
34+
from sklearn.metrics import plot_confusion_matrix
3635

3736
# import some data to play with
3837
iris = datasets.load_iris()
@@ -45,72 +44,21 @@
4544

4645
# Run classifier, using a model that is too regularized (C too low) to see
4746
# the impact on the results
48-
classifier = svm.SVC(kernel='linear', C=0.01)
49-
y_pred = classifier.fit(X_train, y_train).predict(X_test)
50-
51-
52-
def plot_confusion_matrix(y_true, y_pred, classes,
53-
normalize=False,
54-
title=None,
55-
cmap=plt.cm.Blues):
56-
"""
57-
This function prints and plots the confusion matrix.
58-
Normalization can be applied by setting `normalize=True`.
59-
"""
60-
if not title:
61-
if normalize:
62-
title = 'Normalized confusion matrix'
63-
else:
64-
title = 'Confusion matrix, without normalization'
65-
66-
# Compute confusion matrix
67-
cm = confusion_matrix(y_true, y_pred)
68-
# Only use the labels that appear in the data
69-
classes = classes[unique_labels(y_true, y_pred)]
70-
if normalize:
71-
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
72-
print("Normalized confusion matrix")
73-
else:
74-
print('Confusion matrix, without normalization')
75-
76-
print(cm)
77-
78-
fig, ax = plt.subplots()
79-
im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
80-
ax.figure.colorbar(im, ax=ax)
81-
# We want to show all ticks...
82-
ax.set(xticks=np.arange(cm.shape[1]),
83-
yticks=np.arange(cm.shape[0]),
84-
# ... and label them with the respective list entries
85-
xticklabels=classes, yticklabels=classes,
86-
title=title,
87-
ylabel='True label',
88-
xlabel='Predicted label')
89-
90-
# Rotate the tick labels and set their alignment.
91-
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
92-
rotation_mode="anchor")
93-
94-
# Loop over data dimensions and create text annotations.
95-
fmt = '.2f' if normalize else 'd'
96-
thresh = cm.max() / 2.
97-
for i in range(cm.shape[0]):
98-
for j in range(cm.shape[1]):
99-
ax.text(j, i, format(cm[i, j], fmt),
100-
ha="center", va="center",
101-
color="white" if cm[i, j] > thresh else "black")
102-
fig.tight_layout()
103-
return ax
104-
47+
classifier = svm.SVC(kernel='linear', C=0.01).fit(X_train, y_train)
10548

10649
np.set_printoptions(precision=2)
10750

10851
# Plot non-normalized confusion matrix
109-
plot_confusion_matrix(y_test, y_pred, classes=class_names,
110-
title='Confusion matrix, without normalization')
111-
112-
# Plot normalized confusion matrix
113-
plot_confusion_matrix(y_test, y_pred, classes=class_names, normalize=True,
114-
title='Normalized confusion matrix')
52+
titles_options = [("Confusion matrix, without normalization", None),
53+
("Normalized confusion matrix", 'true')]
54+
for title, normalize in titles_options:
55+
disp = plot_confusion_matrix(classifier, X_test, y_test,
56+
display_labels=class_names,
57+
cmap=plt.cm.Blues,
58+
normalize=normalize)
59+
disp.ax_.set_title(title)
60+
61+
print(title)
62+
print(disp.confusion_matrix)
11563

11664
plt.show()

dev/_downloads/b1e3674706d6abde2dae4b6cfa71be67/plot_digits_classification.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
# matplotlib.pyplot.imread. Note that each image must have the same size. For these
3232
# images, we know which digit they represent: it is given in the 'target' of
3333
# the dataset.
34+
_, axes = plt.subplots(2, 4)
3435
images_and_labels = list(zip(digits.images, digits.target))
35-
for index, (image, label) in enumerate(images_and_labels[:4]):
36-
plt.subplot(2, 4, index + 1)
37-
plt.axis('off')
38-
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
39-
plt.title('Training: %i' % label)
36+
for ax, (image, label) in zip(axes[0, :], images_and_labels[:4]):
37+
ax.set_axis_off()
38+
ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
39+
ax.set_title('Training: %i' % label)
4040

4141
# To apply a classifier on this data, we need to flatten the image, to
4242
# turn the data in a (samples, feature) matrix:
@@ -56,15 +56,16 @@
5656
# Now predict the value of the digit on the second half:
5757
predicted = classifier.predict(X_test)
5858

59+
images_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))
60+
for ax, (image, prediction) in zip(axes[1, :], images_and_predictions[:4]):
61+
ax.set_axis_off()
62+
ax.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
63+
ax.set_title('Prediction: %i' % prediction)
64+
5965
print("Classification report for classifier %s:\n%s\n"
6066
% (classifier, metrics.classification_report(y_test, predicted)))
61-
print("Confusion matrix:\n%s" % metrics.confusion_matrix(y_test, predicted))
62-
63-
images_and_predictions = list(zip(digits.images[n_samples // 2:], predicted))
64-
for index, (image, prediction) in enumerate(images_and_predictions[:4]):
65-
plt.subplot(2, 4, index + 5)
66-
plt.axis('off')
67-
plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')
68-
plt.title('Prediction: %i' % prediction)
67+
disp = metrics.plot_confusion_matrix(classifier, X_test, y_test)
68+
disp.figure_.suptitle("Confusion Matrix")
69+
print("Confusion matrix:\n%s" % disp.confusion_matrix)
6970

7071
plt.show()
Binary file not shown.

dev/_downloads/scikit-learn-docs.pdf

25.2 KB
Binary file not shown.

dev/_images/iris.png

0 Bytes

0 commit comments

Comments
 (0)