Skip to content

Commit dbc6bb8

Browse files
committed
Pushing the docs to dev/ for branch: master, commit 9927982c4eb5773a33f8d0ef5ca22cfc7806e926
1 parent 3afec18 commit dbc6bb8

File tree

971 files changed

+3116
-3094
lines changed

Some content is hidden

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

971 files changed

+3116
-3094
lines changed
486 Bytes
Binary file not shown.
474 Bytes
Binary file not shown.

dev/_downloads/plot_separating_hyperplane.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\nfrom sklearn import svm\n\n# we create 40 separable points\nnp.random.seed(0)\nX = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]\nY = [0] * 20 + [1] * 20\n\n# fit the model\nclf = svm.SVC(kernel='linear')\nclf.fit(X, Y)\n\n# get the separating hyperplane\nw = clf.coef_[0]\na = -w[0] / w[1]\nxx = np.linspace(-5, 5)\nyy = a * xx - (clf.intercept_[0]) / w[1]\n\n# plot the parallels to the separating hyperplane that pass through the\n# support vectors\nb = clf.support_vectors_[0]\nyy_down = a * xx + (b[1] - a * b[0])\nb = clf.support_vectors_[-1]\nyy_up = a * xx + (b[1] - a * b[0])\n\n# plot the line, the points, and the nearest vectors to the plane\nplt.plot(xx, yy, 'k-')\nplt.plot(xx, yy_down, 'k--')\nplt.plot(xx, yy_up, 'k--')\n\nplt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],\n s=80, facecolors='none')\nplt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)\n\nplt.axis('tight')\nplt.show()"
29+
"print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import svm\nfrom sklearn.datasets import make_blobs\n\n\n# we create 40 separable points\nX, y = make_blobs(n_samples=40, centers=2, random_state=12, cluster_std=0.35)\n\n# fit the model\nclf = svm.SVC(kernel='linear')\nclf.fit(X, y)\n\nplt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)\n\n# plot the decision function\nax = plt.gca()\nxlim = ax.get_xlim()\nylim = ax.get_ylim()\n\n# create grid to evaluate model\nxx = np.linspace(xlim[0], xlim[1], 30)\nyy = np.linspace(ylim[0], ylim[1], 30)\nYY, XX = np.meshgrid(yy, xx)\nxy = np.vstack([XX.ravel(), YY.ravel()]).T\nZ = clf.decision_function(xy).reshape(XX.shape)\n\n# plot decision boundary and margins\nax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,\n linestyles=['--', '-', '--'])\n# plot support vectors\nax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,\n linewidth=1, facecolors='none')"
3030
]
3131
}
3232
],

dev/_downloads/plot_separating_hyperplane.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,33 @@
1212
import numpy as np
1313
import matplotlib.pyplot as plt
1414
from sklearn import svm
15+
from sklearn.datasets import make_blobs
16+
1517

1618
# we create 40 separable points
17-
np.random.seed(0)
18-
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
19-
Y = [0] * 20 + [1] * 20
19+
X, y = make_blobs(n_samples=40, centers=2, random_state=12, cluster_std=0.35)
2020

2121
# fit the model
2222
clf = svm.SVC(kernel='linear')
23-
clf.fit(X, Y)
24-
25-
# get the separating hyperplane
26-
w = clf.coef_[0]
27-
a = -w[0] / w[1]
28-
xx = np.linspace(-5, 5)
29-
yy = a * xx - (clf.intercept_[0]) / w[1]
30-
31-
# plot the parallels to the separating hyperplane that pass through the
32-
# support vectors
33-
b = clf.support_vectors_[0]
34-
yy_down = a * xx + (b[1] - a * b[0])
35-
b = clf.support_vectors_[-1]
36-
yy_up = a * xx + (b[1] - a * b[0])
37-
38-
# plot the line, the points, and the nearest vectors to the plane
39-
plt.plot(xx, yy, 'k-')
40-
plt.plot(xx, yy_down, 'k--')
41-
plt.plot(xx, yy_up, 'k--')
42-
43-
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
44-
s=80, facecolors='none')
45-
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
46-
47-
plt.axis('tight')
48-
plt.show()
23+
clf.fit(X, y)
24+
25+
plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)
26+
27+
# plot the decision function
28+
ax = plt.gca()
29+
xlim = ax.get_xlim()
30+
ylim = ax.get_ylim()
31+
32+
# create grid to evaluate model
33+
xx = np.linspace(xlim[0], xlim[1], 30)
34+
yy = np.linspace(ylim[0], ylim[1], 30)
35+
YY, XX = np.meshgrid(yy, xx)
36+
xy = np.vstack([XX.ravel(), YY.ravel()]).T
37+
Z = clf.decision_function(xy).reshape(XX.shape)
38+
39+
# plot decision boundary and margins
40+
ax.contour(XX, YY, Z, colors='k', levels=[-1, 0, 1], alpha=0.5,
41+
linestyles=['--', '-', '--'])
42+
# plot support vectors
43+
ax.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=100,
44+
linewidth=1, facecolors='none')

dev/_downloads/plot_separating_hyperplane_unbalanced.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\nfrom sklearn import svm\n#from sklearn.linear_model import SGDClassifier\n\n# we create 40 separable points\nrng = np.random.RandomState(0)\nn_samples_1 = 1000\nn_samples_2 = 100\nX = np.r_[1.5 * rng.randn(n_samples_1, 2),\n 0.5 * rng.randn(n_samples_2, 2) + [2, 2]]\ny = [0] * (n_samples_1) + [1] * (n_samples_2)\n\n# fit the model and get the separating hyperplane\nclf = svm.SVC(kernel='linear', C=1.0)\nclf.fit(X, y)\n\nw = clf.coef_[0]\na = -w[0] / w[1]\nxx = np.linspace(-5, 5)\nyy = a * xx - clf.intercept_[0] / w[1]\n\n\n# get the separating hyperplane using weighted classes\nwclf = svm.SVC(kernel='linear', class_weight={1: 10})\nwclf.fit(X, y)\n\nww = wclf.coef_[0]\nwa = -ww[0] / ww[1]\nwyy = wa * xx - wclf.intercept_[0] / ww[1]\n\n# plot separating hyperplanes and samples\nh0 = plt.plot(xx, yy, 'k-', label='no weights')\nh1 = plt.plot(xx, wyy, 'k--', label='with weights')\nplt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, edgecolors='k')\nplt.legend()\n\nplt.axis('tight')\nplt.show()"
29+
"print(__doc__)\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn import svm\n\n# we create 40 separable points\nrng = np.random.RandomState(0)\nn_samples_1 = 1000\nn_samples_2 = 100\nX = np.r_[1.5 * rng.randn(n_samples_1, 2),\n 0.5 * rng.randn(n_samples_2, 2) + [2, 2]]\ny = [0] * (n_samples_1) + [1] * (n_samples_2)\n\n# fit the model and get the separating hyperplane\nclf = svm.SVC(kernel='linear', C=1.0)\nclf.fit(X, y)\n\n# fit the model and get the separating hyperplane using weighted classes\nwclf = svm.SVC(kernel='linear', class_weight={1: 10})\nwclf.fit(X, y)\n\n# plot separating hyperplanes and samples\nplt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, edgecolors='k')\nplt.legend()\n\n# plot the decision functions for both classifiers\nax = plt.gca()\nxlim = ax.get_xlim()\nylim = ax.get_ylim()\n\n# create grid to evaluate model\nxx = np.linspace(xlim[0], xlim[1], 30)\nyy = np.linspace(ylim[0], ylim[1], 30)\nYY, XX = np.meshgrid(yy, xx)\nxy = np.vstack([XX.ravel(), YY.ravel()]).T\n\n# get the separating hyperplane\nZ = clf.decision_function(xy).reshape(XX.shape)\n\n# plot decision boundary and margins\na = ax.contour(XX, YY, Z, colors='k', levels=[0], alpha=0.5, linestyles=['-'])\n\n# get the separating hyperplane for weighted classes\nZ = wclf.decision_function(xy).reshape(XX.shape)\n\n# plot decision boundary and margins for weighted classes\nb = ax.contour(XX, YY, Z, colors='r', levels=[0], alpha=0.5, linestyles=['-'])\n\nplt.legend([a.collections[0], b.collections[0]], [\"non weighted\", \"weighted\"],\n loc=\"upper right\")"
3030
]
3131
}
3232
],

dev/_downloads/plot_separating_hyperplane_unbalanced.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import numpy as np
3030
import matplotlib.pyplot as plt
3131
from sklearn import svm
32-
#from sklearn.linear_model import SGDClassifier
3332

3433
# we create 40 separable points
3534
rng = np.random.RandomState(0)
@@ -43,25 +42,36 @@
4342
clf = svm.SVC(kernel='linear', C=1.0)
4443
clf.fit(X, y)
4544

46-
w = clf.coef_[0]
47-
a = -w[0] / w[1]
48-
xx = np.linspace(-5, 5)
49-
yy = a * xx - clf.intercept_[0] / w[1]
50-
51-
52-
# get the separating hyperplane using weighted classes
45+
# fit the model and get the separating hyperplane using weighted classes
5346
wclf = svm.SVC(kernel='linear', class_weight={1: 10})
5447
wclf.fit(X, y)
5548

56-
ww = wclf.coef_[0]
57-
wa = -ww[0] / ww[1]
58-
wyy = wa * xx - wclf.intercept_[0] / ww[1]
59-
6049
# plot separating hyperplanes and samples
61-
h0 = plt.plot(xx, yy, 'k-', label='no weights')
62-
h1 = plt.plot(xx, wyy, 'k--', label='with weights')
6350
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Paired, edgecolors='k')
6451
plt.legend()
6552

66-
plt.axis('tight')
67-
plt.show()
53+
# plot the decision functions for both classifiers
54+
ax = plt.gca()
55+
xlim = ax.get_xlim()
56+
ylim = ax.get_ylim()
57+
58+
# create grid to evaluate model
59+
xx = np.linspace(xlim[0], xlim[1], 30)
60+
yy = np.linspace(ylim[0], ylim[1], 30)
61+
YY, XX = np.meshgrid(yy, xx)
62+
xy = np.vstack([XX.ravel(), YY.ravel()]).T
63+
64+
# get the separating hyperplane
65+
Z = clf.decision_function(xy).reshape(XX.shape)
66+
67+
# plot decision boundary and margins
68+
a = ax.contour(XX, YY, Z, colors='k', levels=[0], alpha=0.5, linestyles=['-'])
69+
70+
# get the separating hyperplane for weighted classes
71+
Z = wclf.decision_function(xy).reshape(XX.shape)
72+
73+
# plot decision boundary and margins for weighted classes
74+
b = ax.contour(XX, YY, Z, colors='r', levels=[0], alpha=0.5, linestyles=['-'])
75+
76+
plt.legend([a.collections[0], b.collections[0]], ["non weighted", "weighted"],
77+
loc="upper right")

dev/_downloads/scikit-learn-docs.pdf

30.2 KB
Binary file not shown.
-85 Bytes
-85 Bytes
39 Bytes

0 commit comments

Comments
 (0)