Hi Aurelien - Great book... I've caught a recurring error that you may be able to help with. In the available code you've provided to supplement the book, you frequently use "save_fig()" after generating a plot. I'd like to use this feature, but it always errors out for me and I've seen quite a few discussion threads on it as well.
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-38-299395e3626e> in <module>()
1 housing.plot(kind="scatter", x="median_income", y="median_house_value", alpha=0.1)
----> 2 save_fig("income_vs_house_value_scatterplot")
NameError: name 'save_fig' is not defined
Just wondering if we've missed a step somewhere...
As you may be able to surmise, I'm quite new to machine learning, but am thoroughly enjoying this book.
Hi @loewenm,
Thanks for your kind words, I'm glad you are enjoying my book. :)
The save_fig() function is defined in the first cell of each notebook: just run that cell and the error should go away. You would typically get the NameError: name 'save_fig' is not defined error if you restart Jupyter (or just the Python kernel) and then go back to the cell you were at without running the previous cells again. When you want to continue running a notebook after an interruption, you need to run the previous cells again, because the Python kernel has lost all the context.
I hope this helps. Can you please point me to the discussion threads about this, so I can help them out? Thanks!
Cheers,
Aurélien
Hi Aurelien,
Thank you for the above. I'm still having this issue though...
I fully realize that I need to restart the kernel and re-run the initial
portion of the section (i.e. all the library imports, etc.) to ensure that
everything is running smoothly as I progress through a chapter... However,
I get this error in every single chapter. Every one!
I am using Spyder instead of Jupyter as I like the interface a little more
and the Variable Explorer window is really helpful. If you would indulge me
for a minute and wouldn't mind running the following code in Spyder please?
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
iris = datasets.load_iris()
list(iris.keys())
print(iris.DESCR)
X = iris["data"][:, 3:] # petal width
y = (iris["target"] == 2).astype(np.int) # 1 if Iris-Virginica, else 0
from sklearn.linear_model import LogisticRegression
log_reg = LogisticRegression(random_state=42)
log_reg.fit(X, y)
X_new = np.linspace(0, 3, 1000).reshape(-1, 1)
y_proba = log_reg.predict_proba(X_new)
plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not
Iris-Virginica")
decision_boundary = X_new[y_proba[:, 1] >= 0.5][0]
plt.figure(figsize=(8, 3))
plt.plot(X[y==0], y[y==0], "bs")
plt.plot(X[y==1], y[y==1], "g^")
plt.plot([decision_boundary, decision_boundary], [-1, 2], "k:", linewidth=2)
plt.plot(X_new, y_proba[:, 1], "g-", linewidth=2, label="Iris-Virginica")
plt.plot(X_new, y_proba[:, 0], "b--", linewidth=2, label="Not
Iris-Virginica")
plt.text(decision_boundary+0.02, 0.15, "Decision boundary", fontsize=14,
color="k", ha="center")
plt.arrow(decision_boundary, 0.08, -0.3, 0, head_width=0.05,
head_length=0.1, fc='b', ec='b')
plt.arrow(decision_boundary, 0.92, 0.3, 0, head_width=0.05,
head_length=0.1, fc='g', ec='g')
plt.xlabel("Petal width (cm)", fontsize=14)
plt.ylabel("Probability", fontsize=14)
plt.legend(loc="center left", fontsize=14)
plt.axis([0, 3, -0.02, 1.02])
save_fig("logistic_regression_plot")
plt.show()
Same error as before:
Traceback (most recent call last):
File "
save_fig("logistic_regression_plot")
NameError: name 'save_fig' is not defined
Kind regards,
Michael
On Thu, Jul 6, 2017 at 12:55 PM, Aurélien Geron notifications@github.com
wrote:
Hi @loewenm https://github.com/loewenm,
Thanks for your kind words, I'm glad you are enjoying my book. :)
The save_fig() function is defined in the first cell of each notebook:
just run that cell and the error should go away. You would typically get
the NameError: name 'save_fig' is not defined error if you restart
Jupyter (or just the Python kernel) and then go back to the cell you were
at without running the previous cells again. When you want to continue
running a notebook after an interruption, you need to run the previous
cells again, because the Python kernel has lost all the context.I hope this helps. Can you please point me to the discussion threads about
this, so I can help them out? Thanks!Cheers,
Aurélien—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/ageron/handson-ml/issues/50#issuecomment-313455693,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ASl5NoptZrjsgjhxYlVE-i4msCcdVGE_ks5sLRGZgaJpZM4OOhod
.
--
Signed,
Michael Loewen
michael.[email protected]
Hi @loewenm ,
Sorry for the delay, I missed your message as the issue was closed. Did you manage to get the code to run in Spyder? The piece of code you asked me to run does not include the definition of the save_fig() function, so it's normal to get an error saying that the function is undefined. You need to make sure you have the following function definition at the beginning of your code:
# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "fundamentals"
def save_fig(fig_id, tight_layout=True):
path = os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id + ".png")
print("Saving figure", fig_id)
if tight_layout:
plt.tight_layout()
plt.savefig(path, format='png', dpi=300)
I'm glad this helped! Regarding QML, it's definitely fascinating, but I'm afraid I really don't know much about this topic (yet?). I read an introductory book about Quantum Physics about 10 years ago, and it blew my mind. I was so motivated to learn that I bought an actual university textbook, but I was lost after reading only about 10 pages. I should try again, perhaps it would be easier now that I've practiced a lot of Linear Algebra.
I'd love to read your thoughts about the topic: what do you think will be the main applications? Is it mostly about faster training time? Exploring huge hyperparameter spaces? Training on quantum data?
I used
plt.savefig("attribute_histogram_plots")
instead of
save_fig("attribute_histogram_plots")
then it works!
Thanks, UlrikeBoehm. It does!
Thanks! save_fig my ...
Most helpful comment
I used
plt.savefig("attribute_histogram_plots")
instead of
save_fig("attribute_histogram_plots")
then it works!