ageron / handson-ml

⛔️ DEPRECATED – See https://github.com/ageron/handson-ml3 instead.
Apache License 2.0
25.19k stars 12.92k forks source link

Chapter 10 - FileNotFoundError: [Errno 2] No such file or directory: './images/ann/perceptron_iris_plot.png' #407

Closed Rahul954 closed 5 years ago

Rahul954 commented 5 years ago

Hello,

I am running the code for Chapter 10:

from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron

iris = load_iris()
X = iris.data[:,(2,3)]
y=(iris.target==0).astype(np.int)

per_clf = Perceptron(max_iter=100, tol=-np.infty, random_state=42)
per_clf.fit(X,y)
y_pred = per_clf.predict([[2,0.5]])

print("\n\n Value of y_pred", y_pred)

a = -per_clf.coef_[0][0] / per_clf.coef_[0][1]
b = -per_clf.intercept_ / per_clf.coef_[0][1]

axes = [0, 5, 0, 2]

x0, x1 = np.meshgrid(
        np.linspace(axes[0], axes[1], 500).reshape(-1, 1),
        np.linspace(axes[2], axes[3], 200).reshape(-1, 1),
    )
X_new = np.c_[x0.ravel(), x1.ravel()]
y_predict = per_clf.predict(X_new)
zz = y_predict.reshape(x0.shape)

plt.figure(figsize=(10, 4))
plt.plot(X[y==0, 0], X[y==0, 1], "bs", label="Not Iris-Setosa")
plt.plot(X[y==1, 0], X[y==1, 1], "yo", label="Iris-Setosa")

plt.plot([axes[0], axes[1]], [a * axes[0] + b, a * axes[1] + b], "k-", linewidth=3)
from matplotlib.colors import ListedColormap
custom_cmap = ListedColormap(['#9898ff', '#fafab0'])

plt.contourf(x0, x1, zz, cmap=custom_cmap)
plt.xlabel("Petal length", fontsize=14)
plt.ylabel("Petal width", fontsize=14)
plt.legend(loc="lower right", fontsize=14)
plt.axis(axes)

save_fig("perceptron_iris_plot")
plt.show()

The trace getting generated is like:

runfile('/root/Machine_Learning_Scikit_Tensorflow/Chapter10.py', wdir='/root/Machine_Learning_Scikit_Tensorflow')

 Value of y_pred [1]
Saving figure perceptron_iris_plot
Traceback (most recent call last):

  File "<ipython-input-10-ec8c1f97086d>", line 1, in <module>
    runfile('/root/Machine_Learning_Scikit_Tensorflow/Chapter10.py', wdir='/root/Machine_Learning_Scikit_Tensorflow')

  File "/root/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "/root/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/root/Machine_Learning_Scikit_Tensorflow/Chapter10.py", line 83, in <module>
    save_fig("perceptron_iris_plot")

  File "/root/Machine_Learning_Scikit_Tensorflow/Chapter10.py", line 34, in save_fig
    plt.savefig(path, format='png', dpi=300)

  File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 689, in savefig
    res = fig.savefig(*args, **kwargs)

  File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/figure.py", line 2094, in savefig
    self.canvas.print_figure(fname, **kwargs)

  File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backend_bases.py", line 2075, in print_figure
    **kwargs)

  File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_agg.py", line 521, in print_png
    cbook.open_file_cm(filename_or_obj, "wb") as fh:

  File "/root/anaconda3/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)

  File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 407, in open_file_cm
    fh, opened = to_filehandle(path_or_file, mode, True, encoding)

  File "/root/anaconda3/lib/python3.6/site-packages/matplotlib/cbook/__init__.py", line 392, in to_filehandle
    fh = open(fname, flag, encoding=encoding)

FileNotFoundError: [Errno 2] No such file or directory: './images/ann/perceptron_iris_plot.png'

Though I have directory /images/ann/ in my working directory. Still I am getting the error. FileNotFoundError: [Errno 2] No such file or directory: './images/ann/perceptron_iris_plot.png'

Regards, Rahul Rathore

ageron commented 5 years ago

Hi @Rahul954 ,

A quick fix is to comment out the save_fig() lines, they are only used to save the figures to disk, but I guess nobody needs that expect me (to include these images in the book).

I'm not sure what environment you are running in, because you seem to be running as root, which is usually not a good idea, except perhaps if it's a virtual machine or a docker? The first line of the output is:

runfile('/root/Machine_Learning_Scikit_Tensorflow/Chapter10.py',
        wdir='/root/Machine_Learning_Scikit_Tensorflow')

So apparently the working directory is /root/Machine_Learning_Scikit_Tensorflow. This would mean that the directory it will try to save the image to is located at /root/Machine_Learning_Scikit_Tensorflow/images/ann/.

If it does not exist, then simply create it. If it does exists, then I'm guessing this is an access rights issue: perhaps the Jupyter server is running as some user X, and that user does not have access rights to the directory /root/Machine_Learning_Scikit_Tensorflow/images/ann/.

Hope this helps, Aurélien

Rahul954 commented 5 years ago

Hello Aurélien,

Thanks for your help.

A quick fix is to comment out the save_fig() lines, they are only used to save the figures to disk, but I guess nobody needs that expect me (to include these images in the book).

I will try that out.

I'm not sure what environment you are running in, because you seem to be running as root, which is usually not a good idea, except perhaps if it's a virtual machine or a docker?

I will keep this in mind in future. However, I have already done the whole setup on Spyder. So I will follow your advise religiously once I have finished this book and some other books by uninstalling it.

If it does not exist, then simply create it.

Actually I have tried that.

Last but not the least I want some help regarding suggestion of some book for python. I know other languages and understand python too but sometimes get stuck with syntax. For example:

for epoch in range(n_epochs):
        for batch_index in range(n_batches):
            X_batch, y_batch = fetch_batch(epoch, batch_index, batch_size)
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})

    best_theta = theta.eval()

I am unable to understand meaning of {X: X_batch, y: y_batch}.

YOUR HELP AND A GREAT ATTITUDE TOWARDS MAKING OTHER PEOPLE LEARN IS A BOON FOR US AND ALLOWING US TO EXPRESS OUR DIFFICULTIES / CHALLENGES HONESTLY.

KEEP UP THE GOOD WORK.

Regards, Rahul Rathore

ageron commented 5 years ago

Hi Rahul,

Thanks for your detailed answer and your encouraging words. :)

I personally learned Python entirely online, I don't have any Python book (actually, I bought one for my children: Python for Kids. I particularly liked the official tutorial, but there are plenty of other online resources. Not sure what book to recommend though, I'm sure there are plenty of excellent ones.

The core data structures in Python include lists (e.g., [1, 2, 3]), tuples (e.g., (1, 2, 3)), dictionaries (e.g., {"john": 12, "martha": 43, "robert": 17.5}) and sets (e.g., {2, 3, 5, 7, 11, 13}). So {X: X_batch, y: y_batch} is a dictionary that maps the object X to the object X_batch, and the object y to the object y_batch. Dictionaries are implemented using hash tables, so the keys (X and y) must be hashable, meaning they must implement a __hash__() method. TensorFlow placeholders are hashable, so you can use them as keys. But you could also use their name instead: {"X:0": X_batch, "y:0": y_batch}. The name of a tensor is the name of the operation that creates it (in this case, a tf.placeholder() operation) followed by :0 if it is the first output of the operation, or :1 if it is the second, and so one. Most operations have a single output, so most tensors are named "something:0".

Hope this helps.

Rahul954 commented 5 years ago

Hello,

Thanks for the advice. I will try to use online material and try to make most of it.

Regards, Rahul Rathore