Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Pylab Instances with Matplotlib's pyplot #407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
68 changes: 33 additions & 35 deletions lessons/3-NeuralNetworks/03-Perceptron/Perceptron.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"metadata": {},
"outputs": [],
"source": [
"import pylab\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib import gridspec\n",
"from sklearn.datasets import make_classification\n",
"import numpy as np\n",
Expand Down Expand Up @@ -131,8 +131,8 @@
"source": [
"def plot_dataset(suptitle, features, labels):\n",
" # prepare the plot\n",
" fig, ax = pylab.subplots(1, 1)\n",
" #pylab.subplots_adjust(bottom=0.2, wspace=0.4)\n",
" fig, ax = plt.subplots(1, 1)\n",
" #plt.subplots_adjust(bottom=0.2, wspace=0.4)\n",
" fig.suptitle(suptitle, fontsize = 16)\n",
" ax.set_xlabel('$x_i[0]$ -- (feature 1)')\n",
" ax.set_ylabel('$x_i[1]$ -- (feature 2)')\n",
Expand Down Expand Up @@ -337,12 +337,12 @@
" x = np.array([-6, 6], dtype='float32')\n",
" y = -(weights[0] * x + weights[2])/weights[1]\n",
"\n",
" pylab.xlim(-6, 6)\n",
" pylab.ylim(-6, 6) \n",
" pylab.plot(positive_examples[:,0], positive_examples[:,1], 'bo')\n",
" pylab.plot(negative_examples[:,0], negative_examples[:,1], 'ro')\n",
" pylab.plot(x, y, 'g', linewidth=2.0)\n",
" pylab.show()"
" plt.xlim(-6, 6)\n",
" plt.ylim(-6, 6) \n",
" plt.plot(positive_examples[:,0], positive_examples[:,1], 'bo')\n",
" plt.plot(negative_examples[:,0], negative_examples[:,1], 'ro')\n",
" plt.plot(x, y, 'g', linewidth=2.0)\n",
" plt.show()"
]
},
{
Expand Down Expand Up @@ -470,15 +470,15 @@
"snapshots = train_graph(pos_examples,neg_examples)\n",
"\n",
"def plotit(pos_examples,neg_examples,snapshots,step):\n",
" fig = pylab.figure(figsize=(10,4))\n",
" fig = plt.figure(figsize=(10,4))\n",
" fig.add_subplot(1, 2, 1)\n",
" plot_boundary(pos_examples, neg_examples, snapshots[step][0])\n",
" fig.add_subplot(1, 2, 2)\n",
" pylab.plot(np.arange(len(snapshots[:,1])), snapshots[:,1])\n",
" pylab.ylabel('Accuracy')\n",
" pylab.xlabel('Iteration')\n",
" pylab.plot(step, snapshots[step,1][0], \"bo\")\n",
" pylab.show()\n",
" plt.plot(np.arange(len(snapshots[:,1])), snapshots[:,1])\n",
" plt.ylabel('Accuracy')\n",
" plt.xlabel('Iteration')\n",
" plt.plot(step, snapshots[step,1][0], \"bo\")\n",
" plt.show()\n",
"def pl1(step): plotit(pos_examples,neg_examples,snapshots,step)"
]
},
Expand Down Expand Up @@ -685,11 +685,11 @@
"print(MNIST['Train']['Labels'][0])\n",
"features = MNIST['Train']['Features'].astype(np.float32) / 256.0\n",
"labels = MNIST['Train']['Labels']\n",
"fig = pylab.figure(figsize=(10,5))\n",
"fig = plt.figure(figsize=(10,5))\n",
"for i in range(10):\n",
" ax = fig.add_subplot(1,10,i+1)\n",
" pylab.imshow(features[i].reshape(28,28))\n",
"pylab.show()"
" plt.imshow(features[i].reshape(28,28))\n",
"plt.show()"
]
},
{
Expand Down Expand Up @@ -718,16 +718,16 @@
" positive_images = MNIST['Train']['Features'][positive_indices]\n",
" negative_images = MNIST['Train']['Features'][negative_indices]\n",
"\n",
" fig = pylab.figure()\n",
" fig = plt.figure()\n",
" ax = fig.add_subplot(1, 2, 1)\n",
" pylab.imshow(positive_images[0].reshape(28,28), cmap='gray', interpolation='nearest')\n",
" plt.imshow(positive_images[0].reshape(28,28), cmap='gray', interpolation='nearest')\n",
" ax.set_xticks([])\n",
" ax.set_yticks([])\n",
" ax = fig.add_subplot(1, 2, 2)\n",
" pylab.imshow(negative_images[0].reshape(28,28), cmap='gray', interpolation='nearest')\n",
" plt.imshow(negative_images[0].reshape(28,28), cmap='gray', interpolation='nearest')\n",
" ax.set_xticks([])\n",
" ax.set_yticks([])\n",
" pylab.show()\n",
" plt.show()\n",
" \n",
" return positive_images, negative_images"
]
Expand Down Expand Up @@ -774,17 +774,17 @@
"outputs": [],
"source": [
"def plotit2(snapshots_mn,step):\n",
" fig = pylab.figure(figsize=(10,4))\n",
" fig = plt.figure(figsize=(10,4))\n",
" ax = fig.add_subplot(1, 2, 1)\n",
" pylab.imshow(snapshots_mn[step][0].reshape(28, 28), interpolation='nearest')\n",
" plt.imshow(snapshots_mn[step][0].reshape(28, 28), interpolation='nearest')\n",
" ax.set_xticks([])\n",
" ax.set_yticks([])\n",
" pylab.colorbar()\n",
" plt.colorbar()\n",
" ax = fig.add_subplot(1, 2, 2)\n",
" ax.set_ylim([0,1])\n",
" pylab.plot(np.arange(len(snapshots_mn[:,1])), snapshots_mn[:,1])\n",
" pylab.plot(step, snapshots_mn[step,1], \"bo\")\n",
" pylab.show()\n",
" plt.plot(np.arange(len(snapshots_mn[:,1])), snapshots_mn[:,1])\n",
" plt.plot(step, snapshots_mn[step,1], \"bo\")\n",
" plt.show()\n",
"def pl3(step): plotit2(snapshots_mn,step)\n",
"def pl4(step): plotit2(snapshots_mn2,step) "
]
Expand Down Expand Up @@ -943,8 +943,8 @@
" pos_points = mypca.transform(positive_images[:200])\n",
" neg_points = mypca.transform(negative_images[:200])\n",
"\n",
" pylab.plot(pos_points[:,0], pos_points[:,1], 'bo')\n",
" pylab.plot(neg_points[:,0], neg_points[:,1], 'ro')"
" plt.plot(pos_points[:,0], pos_points[:,1], 'bo')\n",
" plt.plot(neg_points[:,0], neg_points[:,1], 'ro')"
]
},
{
Expand Down Expand Up @@ -1053,11 +1053,9 @@
],
"metadata": {
"celltoolbar": "Slideshow",
"interpreter": {
"hash": "16aeaa504b544176258e5caf576fc030dfd6fff62d0c15825e7863ff13e121ff"
},
"kernelspec": {
"display_name": "Python 3.8.0 64-bit (conda)",
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
Expand All @@ -1070,7 +1068,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.12.3"
},
"livereveal": {
"start_slideshow_at": "selected"
Expand Down
163 changes: 163 additions & 0 deletions myenv/lib/python3.12/site-packages/IPython/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# PYTHON_ARGCOMPLETE_OK
"""
IPython: tools for interactive and parallel computing in Python.

https://ipython.org
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2008-2011, IPython Development Team.
# Copyright (c) 2001-2007, Fernando Perez <[email protected]>
# Copyright (c) 2001, Janko Hauser <[email protected]>
# Copyright (c) 2001, Nathaniel Gray <[email protected]>
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import sys

#-----------------------------------------------------------------------------
# Setup everything
#-----------------------------------------------------------------------------

# Don't forget to also update setup.py when this changes!
if sys.version_info < (3, 10):
raise ImportError(
"""
IPython 8.19+ supports Python 3.10 and above, following SPEC0.
IPython 8.13+ supports Python 3.9 and above, following NEP 29.
IPython 8.0-8.12 supports Python 3.8 and above, following NEP 29.
When using Python 2.7, please install IPython 5.x LTS Long Term Support version.
Python 3.3 and 3.4 were supported up to IPython 6.x.
Python 3.5 was supported with IPython 7.0 to 7.9.
Python 3.6 was supported with IPython up to 7.16.
Python 3.7 was still supported with the 7.x branch.

See IPython `README.rst` file for more information:

https://github.com/ipython/ipython/blob/main/README.rst

"""
)

#-----------------------------------------------------------------------------
# Setup the top level names
#-----------------------------------------------------------------------------

from .core.getipython import get_ipython
from .core import release
from .core.application import Application
from .terminal.embed import embed

from .core.interactiveshell import InteractiveShell
from .utils.sysinfo import sys_info
from .utils.frame import extract_module_locals

__all__ = ["start_ipython", "embed", "start_kernel", "embed_kernel"]

# Release data
__author__ = '%s <%s>' % (release.author, release.author_email)
__license__ = release.license
__version__ = release.version
version_info = release.version_info
# list of CVEs that should have been patched in this release.
# this is informational and should not be relied upon.
__patched_cves__ = {"CVE-2022-21699", "CVE-2023-24816"}


def embed_kernel(module=None, local_ns=None, **kwargs):
"""Embed and start an IPython kernel in a given scope.

If you don't want the kernel to initialize the namespace
from the scope of the surrounding function,
and/or you want to load full IPython configuration,
you probably want `IPython.start_kernel()` instead.

Parameters
----------
module : types.ModuleType, optional
The module to load into IPython globals (default: caller)
local_ns : dict, optional
The namespace to load into IPython user namespace (default: caller)
**kwargs : various, optional
Further keyword args are relayed to the IPKernelApp constructor,
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
allowing configuration of the kernel (see :ref:`kernel_options`). Will only have an effect
on the first embed_kernel call for a given process.
"""

(caller_module, caller_locals) = extract_module_locals(1)
if module is None:
module = caller_module
if local_ns is None:
local_ns = caller_locals

# Only import .zmq when we really need it
from ipykernel.embed import embed_kernel as real_embed_kernel
real_embed_kernel(module=module, local_ns=local_ns, **kwargs)

def start_ipython(argv=None, **kwargs):
"""Launch a normal IPython instance (as opposed to embedded)

`IPython.embed()` puts a shell in a particular calling scope,
such as a function or method for debugging purposes,
which is often not desirable.

`start_ipython()` does full, regular IPython initialization,
including loading startup files, configuration, etc.
much of which is skipped by `embed()`.

This is a public API method, and will survive implementation changes.

Parameters
----------
argv : list or None, optional
If unspecified or None, IPython will parse command-line options from sys.argv.
To prevent any command-line parsing, pass an empty list: `argv=[]`.
user_ns : dict, optional
specify this dictionary to initialize the IPython user namespace with particular values.
**kwargs : various, optional
Any other kwargs will be passed to the Application constructor,
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
allowing configuration of the instance (see :ref:`terminal_options`).
"""
from IPython.terminal.ipapp import launch_new_instance
return launch_new_instance(argv=argv, **kwargs)

def start_kernel(argv=None, **kwargs):
"""Launch a normal IPython kernel instance (as opposed to embedded)

`IPython.embed_kernel()` puts a shell in a particular calling scope,
such as a function or method for debugging purposes,
which is often not desirable.

`start_kernel()` does full, regular IPython initialization,
including loading startup files, configuration, etc.
much of which is skipped by `embed_kernel()`.

Parameters
----------
argv : list or None, optional
If unspecified or None, IPython will parse command-line options from sys.argv.
To prevent any command-line parsing, pass an empty list: `argv=[]`.
user_ns : dict, optional
specify this dictionary to initialize the IPython user namespace with particular values.
**kwargs : various, optional
Any other kwargs will be passed to the Application constructor,
such as `config`, a traitlets :class:`Config` object (see :ref:`configure_start_ipython`),
allowing configuration of the kernel (see :ref:`kernel_options`).
"""
import warnings

warnings.warn(
"start_kernel is deprecated since IPython 8.0, use from `ipykernel.kernelapp.launch_new_instance`",
DeprecationWarning,
stacklevel=2,
)
from ipykernel.kernelapp import launch_new_instance
return launch_new_instance(argv=argv, **kwargs)
15 changes: 15 additions & 0 deletions myenv/lib/python3.12/site-packages/IPython/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# PYTHON_ARGCOMPLETE_OK
# encoding: utf-8
"""Terminal-based IPython entry point.
"""
# -----------------------------------------------------------------------------
# Copyright (c) 2012, IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
# -----------------------------------------------------------------------------

from IPython import start_ipython

start_ipython()
Loading