137 lines
8.9 KiB
Plaintext
137 lines
8.9 KiB
Plaintext
Traceback (most recent call last):
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/site-packages/jupyter_cache/executors/utils.py", line 58, in single_nb_execution
|
|
executenb(
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/site-packages/nbclient/client.py", line 1305, in execute
|
|
return NotebookClient(nb=nb, resources=resources, km=km, **kwargs).execute()
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/site-packages/jupyter_core/utils/__init__.py", line 166, in wrapped
|
|
return loop.run_until_complete(inner)
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
|
|
return future.result()
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/site-packages/nbclient/client.py", line 705, in async_execute
|
|
await self.async_execute_cell(
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/site-packages/nbclient/client.py", line 1058, in async_execute_cell
|
|
await self._check_raise_for_error(cell, cell_index, exec_reply)
|
|
File "/Users/mhjensen/miniforge3/lib/python3.9/site-packages/nbclient/client.py", line 914, in _check_raise_for_error
|
|
raise CellExecutionError.from_cell_and_msg(cell, exec_reply_content)
|
|
nbclient.exceptions.CellExecutionError: An error occurred while executing the following cell:
|
|
------------------
|
|
%matplotlib inline
|
|
|
|
from sklearn import datasets
|
|
from sklearn.svm import SVC, LinearSVC
|
|
from sklearn.linear_model import SGDClassifier
|
|
from sklearn.preprocessing import StandardScaler
|
|
import matplotlib
|
|
import matplotlib.pyplot as plt
|
|
plt.rcParams['axes.labelsize'] = 14
|
|
plt.rcParams['xtick.labelsize'] = 12
|
|
plt.rcParams['ytick.labelsize'] = 12
|
|
|
|
|
|
iris = datasets.load_iris()
|
|
X = iris["data"][:, (2, 3)] # petal length, petal width
|
|
y = iris["target"]
|
|
|
|
setosa_or_versicolor = (y == 0) | (y == 1)
|
|
X = X[setosa_or_versicolor]
|
|
y = y[setosa_or_versicolor]
|
|
|
|
|
|
|
|
C = 5
|
|
alpha = 1 / (C * len(X))
|
|
|
|
lin_clf = LinearSVC(loss="hinge", C=C, random_state=42)
|
|
svm_clf = SVC(kernel="linear", C=C)
|
|
sgd_clf = SGDClassifier(loss="hinge", learning_rate="constant", eta0=0.001, alpha=alpha,
|
|
max_iter=100000, random_state=42)
|
|
|
|
scaler = StandardScaler()
|
|
X_scaled = scaler.fit_transform(X)
|
|
|
|
lin_clf.fit(X_scaled, y)
|
|
svm_clf.fit(X_scaled, y)
|
|
sgd_clf.fit(X_scaled, y)
|
|
|
|
print("LinearSVC: ", lin_clf.intercept_, lin_clf.coef_)
|
|
print("SVC: ", svm_clf.intercept_, svm_clf.coef_)
|
|
print("SGDClassifier(alpha={:.5f}):".format(sgd_clf.alpha), sgd_clf.intercept_, sgd_clf.coef_)
|
|
|
|
# Compute the slope and bias of each decision boundary
|
|
w1 = -lin_clf.coef_[0, 0]/lin_clf.coef_[0, 1]
|
|
b1 = -lin_clf.intercept_[0]/lin_clf.coef_[0, 1]
|
|
w2 = -svm_clf.coef_[0, 0]/svm_clf.coef_[0, 1]
|
|
b2 = -svm_clf.intercept_[0]/svm_clf.coef_[0, 1]
|
|
w3 = -sgd_clf.coef_[0, 0]/sgd_clf.coef_[0, 1]
|
|
b3 = -sgd_clf.intercept_[0]/sgd_clf.coef_[0, 1]
|
|
|
|
# Transform the decision boundary lines back to the original scale
|
|
line1 = scaler.inverse_transform([[-10, -10 * w1 + b1], [10, 10 * w1 + b1]])
|
|
line2 = scaler.inverse_transform([[-10, -10 * w2 + b2], [10, 10 * w2 + b2]])
|
|
line3 = scaler.inverse_transform([[-10, -10 * w3 + b3], [10, 10 * w3 + b3]])
|
|
|
|
# Plot all three decision boundaries
|
|
plt.figure(figsize=(11, 4))
|
|
plt.plot(line1[:, 0], line1[:, 1], "k:", label="LinearSVC")
|
|
plt.plot(line2[:, 0], line2[:, 1], "b--", linewidth=2, label="SVC")
|
|
plt.plot(line3[:, 0], line3[:, 1], "r-", label="SGDClassifier")
|
|
plt.plot(X[:, 0][y==1], X[:, 1][y==1], "bs") # label="Iris-Versicolor"
|
|
plt.plot(X[:, 0][y==0], X[:, 1][y==0], "yo") # label="Iris-Setosa"
|
|
plt.xlabel("Petal length", fontsize=14)
|
|
plt.ylabel("Petal width", fontsize=14)
|
|
plt.legend(loc="upper center", fontsize=14)
|
|
plt.axis([0, 5.5, 0, 2])
|
|
|
|
plt.show()
|
|
------------------
|
|
|
|
|
|
[0;31m---------------------------------------------------------------------------[0m
|
|
[0;31mModuleNotFoundError[0m Traceback (most recent call last)
|
|
Cell [0;32mIn[1], line 1[0m
|
|
[0;32m----> 1[0m [43mget_ipython[49m[43m([49m[43m)[49m[38;5;241;43m.[39;49m[43mrun_line_magic[49m[43m([49m[38;5;124;43m'[39;49m[38;5;124;43mmatplotlib[39;49m[38;5;124;43m'[39;49m[43m,[49m[43m [49m[38;5;124;43m'[39;49m[38;5;124;43minline[39;49m[38;5;124;43m'[39;49m[43m)[49m
|
|
[1;32m 3[0m [38;5;28;01mfrom[39;00m [38;5;21;01msklearn[39;00m [38;5;28;01mimport[39;00m datasets
|
|
[1;32m 4[0m [38;5;28;01mfrom[39;00m [38;5;21;01msklearn[39;00m[38;5;21;01m.[39;00m[38;5;21;01msvm[39;00m [38;5;28;01mimport[39;00m SVC, LinearSVC
|
|
|
|
File [0;32m~/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:2432[0m, in [0;36mInteractiveShell.run_line_magic[0;34m(self, magic_name, line, _stack_depth)[0m
|
|
[1;32m 2430[0m kwargs[[38;5;124m'[39m[38;5;124mlocal_ns[39m[38;5;124m'[39m] [38;5;241m=[39m [38;5;28mself[39m[38;5;241m.[39mget_local_scope(stack_depth)
|
|
[1;32m 2431[0m [38;5;28;01mwith[39;00m [38;5;28mself[39m[38;5;241m.[39mbuiltin_trap:
|
|
[0;32m-> 2432[0m result [38;5;241m=[39m [43mfn[49m[43m([49m[38;5;241;43m*[39;49m[43margs[49m[43m,[49m[43m [49m[38;5;241;43m*[39;49m[38;5;241;43m*[39;49m[43mkwargs[49m[43m)[49m
|
|
[1;32m 2434[0m [38;5;66;03m# The code below prevents the output from being displayed[39;00m
|
|
[1;32m 2435[0m [38;5;66;03m# when using magics with decorator @output_can_be_silenced[39;00m
|
|
[1;32m 2436[0m [38;5;66;03m# when the last Python token in the expression is a ';'.[39;00m
|
|
[1;32m 2437[0m [38;5;28;01mif[39;00m [38;5;28mgetattr[39m(fn, magic[38;5;241m.[39mMAGIC_OUTPUT_CAN_BE_SILENCED, [38;5;28;01mFalse[39;00m):
|
|
|
|
File [0;32m~/miniforge3/lib/python3.9/site-packages/IPython/core/magics/pylab.py:99[0m, in [0;36mPylabMagics.matplotlib[0;34m(self, line)[0m
|
|
[1;32m 97[0m [38;5;28mprint[39m([38;5;124m"[39m[38;5;124mAvailable matplotlib backends: [39m[38;5;132;01m%s[39;00m[38;5;124m"[39m [38;5;241m%[39m backends_list)
|
|
[1;32m 98[0m [38;5;28;01melse[39;00m:
|
|
[0;32m---> 99[0m gui, backend [38;5;241m=[39m [38;5;28;43mself[39;49m[38;5;241;43m.[39;49m[43mshell[49m[38;5;241;43m.[39;49m[43menable_matplotlib[49m[43m([49m[43margs[49m[38;5;241;43m.[39;49m[43mgui[49m[38;5;241;43m.[39;49m[43mlower[49m[43m([49m[43m)[49m[43m [49m[38;5;28;43;01mif[39;49;00m[43m [49m[38;5;28;43misinstance[39;49m[43m([49m[43margs[49m[38;5;241;43m.[39;49m[43mgui[49m[43m,[49m[43m [49m[38;5;28;43mstr[39;49m[43m)[49m[43m [49m[38;5;28;43;01melse[39;49;00m[43m [49m[43margs[49m[38;5;241;43m.[39;49m[43mgui[49m[43m)[49m
|
|
[1;32m 100[0m [38;5;28mself[39m[38;5;241m.[39m_show_matplotlib_backend(args[38;5;241m.[39mgui, backend)
|
|
|
|
File [0;32m~/miniforge3/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3606[0m, in [0;36mInteractiveShell.enable_matplotlib[0;34m(self, gui)[0m
|
|
[1;32m 3585[0m [38;5;28;01mdef[39;00m [38;5;21menable_matplotlib[39m([38;5;28mself[39m, gui[38;5;241m=[39m[38;5;28;01mNone[39;00m):
|
|
[1;32m 3586[0m [38;5;250m [39m[38;5;124;03m"""Enable interactive matplotlib and inline figure support.[39;00m
|
|
[1;32m 3587[0m
|
|
[1;32m 3588[0m [38;5;124;03m This takes the following steps:[39;00m
|
|
[0;32m (...)[0m
|
|
[1;32m 3604[0m [38;5;124;03m display figures inline.[39;00m
|
|
[1;32m 3605[0m [38;5;124;03m """[39;00m
|
|
[0;32m-> 3606[0m [38;5;28;01mfrom[39;00m [38;5;21;01mmatplotlib_inline[39;00m[38;5;21;01m.[39;00m[38;5;21;01mbackend_inline[39;00m [38;5;28;01mimport[39;00m configure_inline_support
|
|
[1;32m 3608[0m [38;5;28;01mfrom[39;00m [38;5;21;01mIPython[39;00m[38;5;21;01m.[39;00m[38;5;21;01mcore[39;00m [38;5;28;01mimport[39;00m pylabtools [38;5;28;01mas[39;00m pt
|
|
[1;32m 3609[0m gui, backend [38;5;241m=[39m pt[38;5;241m.[39mfind_gui_and_backend(gui, [38;5;28mself[39m[38;5;241m.[39mpylab_gui_select)
|
|
|
|
File [0;32m~/miniforge3/lib/python3.9/site-packages/matplotlib_inline/__init__.py:1[0m
|
|
[0;32m----> 1[0m [38;5;28;01mfrom[39;00m [38;5;21;01m.[39;00m [38;5;28;01mimport[39;00m backend_inline, config [38;5;66;03m# noqa[39;00m
|
|
[1;32m 2[0m __version__ [38;5;241m=[39m [38;5;124m"[39m[38;5;124m0.1.6[39m[38;5;124m"[39m [38;5;66;03m# noqa[39;00m
|
|
|
|
File [0;32m~/miniforge3/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:6[0m
|
|
[1;32m 1[0m [38;5;124;03m"""A matplotlib backend for publishing figures via display_data"""[39;00m
|
|
[1;32m 3[0m [38;5;66;03m# Copyright (c) IPython Development Team.[39;00m
|
|
[1;32m 4[0m [38;5;66;03m# Distributed under the terms of the BSD 3-Clause License.[39;00m
|
|
[0;32m----> 6[0m [38;5;28;01mimport[39;00m [38;5;21;01mmatplotlib[39;00m
|
|
[1;32m 7[0m [38;5;28;01mfrom[39;00m [38;5;21;01mmatplotlib[39;00m [38;5;28;01mimport[39;00m colors
|
|
[1;32m 8[0m [38;5;28;01mfrom[39;00m [38;5;21;01mmatplotlib[39;00m[38;5;21;01m.[39;00m[38;5;21;01mbackends[39;00m [38;5;28;01mimport[39;00m backend_agg
|
|
|
|
[0;31mModuleNotFoundError[0m: No module named 'matplotlib'
|
|
|