<jupyter_start><jupyter_text>Basic ploting Generate coordinate vectors<jupyter_code>nx, ny = 100, 100
x = np.linspace(-5, 5, nx)
y = np.linspace(-5, 5, ny)<jupyter_output><empty_output><jupyter_text>Generate coordinate matrices<jupyter_code>xx, yy = np.meshgrid(x, y, sparse=True)<jupyter_output><empty_output><jupyter_text>Compute function value<jupyter_code>z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)<jupyter_output><empty_output><jupyter_text>Plot contours<jupyter_code># filled contour
plt.contourf(x, y, z)
# unfilled contour
plt.contour(x, y, z, levels=[0.5], cmap='Greys', vmin=0, vmax=1.)<jupyter_output><empty_output><jupyter_text>Plot Decision BoundaryFirst, generate the data and fit the classifier to the training set<jupyter_code>from sklearn.linear_model import LogisticRegression
from sklearn.datasets import make_classification

X, y = make_classification(200, 2, 2, 0, weights=[.5, .5], random_state=15)
clf = LogisticRegression().fit(X[:100], y[:100])<jupyter_output><empty_output><jupyter_text>Next, make a continuous grid of values and evaluate the probability of each (x, y) point in the grid:<jupyter_code>xx, yy = np.mgrid[-5:5:.01, -5:5:.01]
grid = np.c_[xx.ravel(), yy.ravel()]
probs = clf.predict_proba(grid)[:, 1].reshape(xx.shape)<jupyter_output><empty_output><jupyter_text>Now, plot the probability grid as a contour map and additionally show the test set samples on top of it:<jupyter_code>f, ax = plt.subplots(figsize=(8, 6))
contour = ax.contourf(xx, yy, probs, 25, cmap="RdBu",
                      vmin=0, vmax=1)
ax_c = f.colorbar(contour)
ax_c.set_label("$P(y = 1)$")
ax_c.set_ticks([0, .25, .5, .75, 1])

ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=50,
           cmap="RdBu", vmin=-.2, vmax=1.2,
           edgecolor="white", linewidth=1)

ax.set(aspect="equal",
       xlim=(-5, 5), ylim=(-5, 5),
       xlabel="$X_1$", ylabel="$X_2$")<jupyter_output><empty_output><jupyter_text>The logistic regression lets your classify new samples based on any threshold you want, so it doesn't inherently have one "decision boundary." But, of course, a common decision rule to use is $p = .5$. We can also just draw that contour level using the above code:<jupyter_code>f, ax = plt.subplots(figsize=(8, 6))
ax.contour(xx, yy, probs, levels=[.5], cmap="Greys", vmin=0, vmax=.6)

ax.scatter(X[100:,0], X[100:, 1], c=y[100:], s=50,
           cmap="RdBu", vmin=-.2, vmax=1.2,
           edgecolor="white", linewidth=1)

ax.set(aspect="equal",
       xlim=(-5, 5), ylim=(-5, 5),
       xlabel="$X_1$", ylabel="$X_2$")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16822066822066822, 'fraction_numerical': 0.042346542346542344, 'mean_word_length': 5.3580246913580245, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '245227', 'license': 'MIT', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1018, 'n_words': 230, 'path': 'notebooks/Plot.ipynb', 'repo_name': 'Alexoner/skynet', 'size': '2574'}