Giter Site home page Giter Site logo

save_fig is not defined about handson-ml HOT 10 CLOSED

ageron avatar ageron commented on May 2, 2024
save_fig is not defined

from handson-ml.

Comments (10)

UlrikeBoehm avatar UlrikeBoehm commented on May 2, 2024 12

I used
plt.savefig("attribute_histogram_plots")
instead of
save_fig("attribute_histogram_plots")
then it works!

from handson-ml.

ageron avatar ageron commented on May 2, 2024 3

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

from handson-ml.

loewenm avatar loewenm commented on May 2, 2024

from handson-ml.

ageron avatar ageron commented on May 2, 2024

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:

# Imports
import os
import matplotlib.pyplot as plt

# 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)

from handson-ml.

ageron avatar ageron commented on May 2, 2024

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?

from handson-ml.

olusegun-omisakin avatar olusegun-omisakin commented on May 2, 2024

Thanks, UlrikeBoehm. It does!

from handson-ml.

tiagobrc avatar tiagobrc commented on May 2, 2024

Thanks! save_fig my ...

from handson-ml.

Neeraj02a avatar Neeraj02a commented on May 2, 2024

I used
plt.savefig("attribute_histogram_plots")
instead of
save_fig("attribute_histogram_plots")
then it works!

That was really very help full, it's working rather then what's described above

from handson-ml.

ageron avatar ageron commented on May 2, 2024

Hi @Neeraj02a ,

Thanks for your feedback. My save_fig() function is just a thin wrapper for Matplotlib's plt.savefig() function. It just handles the file path (so that each image is saved into the appropriate folder, for example in images/fundamentals if you're running the chapter 1 notebook), and by default it also calls plt.tight_layout() to save some space. Lastly, it ensures the output format is PNG and the resolution is 300 dpi. This is the format expected by O'Reilly: they need print-quality images.

If you get an error when calling save_fig(), it might be because you're missing some of the imports? I added the ones you need in the code example above.

Hope this helps.

from handson-ml.

mkayanda avatar mkayanda commented on May 2, 2024

Thank you @ageron for such a well explained resolution. I had the same issue but coming here gave me the best insights.

from handson-ml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.