Git Product home page Git Product logo

cookbook-code's Introduction

IPython Cookbook

This repository contains the recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python, by Dr. Cyrille Rossant, Packt Publishing, 500 pages, September 2014.

Follow me on Twitter to get all updates

Featured recipes

A selection of free recipes from the book:

  1. Getting the best performance out of NumPy
  2. Simulating a physical system by minimizing an energy
  3. Creating a route planner for road network
  4. Introduction to machine learning in Python with scikit-learn
  5. Simulating a partial differential equation: reaction-diffusion systems and Turing patterns
  6. Getting started with Vispy
  • more coming soon...

Table of contents

Errata

You can report inaccuracies or errors in the GitHub issue tracker. Even better, propose your own corrections by submitting a pull request!

Example data

You will find the data used in the recipes here.

Structure of the repository

The structure of the repo is the following:

notebooks/                      all notebooks with the code of all examples
    chapter01_tour/             
    chapter02_best_practices/   
    ...
    extra/                      extra code example that didn't make it in the book
    guests/                     guest recipes
featured/                       a selection of complete recipes with all text, figures and code
references/                     a curated list of references about scientific Python programming
tools/                          various building Python scripts

Installation

You need Python 3 (or 2) and a bunch of scientific modules for the code examples, mainly IPython 2.0+, NumPy, SciPy, Pandas, and matplotlib. Many recipes that require other modules come with the appropriate installation instructions.

We highly recommend that you use an all-in-one Python distribution like Anaconda. This distribution comes with an excellent package manager named conda. It lets you install easily many modules on most platforms (Windows, Linux, Mac OS X), in 64-bit (recommended if you have a 64-bit OS) or 32-bit.

The recipes are written for Python 3 first, but they also work with Python 2. Please favor Python 3 over Python 2 if you can.

Cloning the repository

You need git, a distributed versioning system, to download a local copy of this repository. Open a terminal and type:

git clone https://github.com/ipython-books/cookbook-code.git

This will copy the repository in a local folder named cookbook-code.

Running the examples

Launch the IPython notebook server with:

ipython notebook

In your browser, go to 127.0.0.1:8888. You can navigate in the repository and open the notebooks.

Contribute

You are welcome to contribute to this repository. You can use the issue tracker to report any problem. You can also propose a pull request (PR) to fix an error, to add some information, or even propose a brand new recipe in the guests/ folder!

cookbook-code's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cookbook-code's Issues

Failed to start the Kernel

when i start notebook, there's a error in my console as follow
image

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/notebook/base/handlers.py", line 437, in wrapper
    result = yield gen.maybe_future(method(self, *args, **kwargs))
  File "/Library/Python/2.7/site-packages/notebook/services/sessions/handlers.py", line 56, in post
    model = sm.create_session(path=path, kernel_name=kernel_name)
  File "/Library/Python/2.7/site-packages/notebook/services/sessions/sessionmanager.py", line 66, in create_session
    kernel_name=kernel_name)
  File "/Library/Python/2.7/site-packages/notebook/services/kernels/kernelmanager.py", line 84, in start_kernel
    **kwargs)
  File "/Library/Python/2.7/site-packages/jupyter_client/multikernelmanager.py", line 109, in start_kernel
    km.start_kernel(**kwargs)
  File "/Library/Python/2.7/site-packages/jupyter_client/manager.py", line 244, in start_kernel
    **kw)
  File "/Library/Python/2.7/site-packages/jupyter_client/manager.py", line 190, in _launch_kernel
    return launch_kernel(kernel_cmd, **kw)
  File "/Library/Python/2.7/site-packages/jupyter_client/launcher.py", line 124, in launch_kernel
    proc = Popen(cmd, **kwargs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

how can I fix it ?

my mac's info:

image

Depreciated warning in Section 1 > Recipe 2

Run pd.rolling_mean(df['Berri1'], n).dropna().plot(); and you get


/usr/local/lib/python2.7/dist-packages/ipykernel/__main__.py:6: FutureWarning: pd.rolling_mean is deprecated for Series and will be removed in a future version, replace with 
    Series.rolling(window=15,center=False).mean()

How can you do the replacement with Series.rolling(...).mean()?

Change in widgets in IPython 3.0

As reported by one user:

Page: 19 & 20

In the example that begins:

"10. Now, we illustrate the latest interactive features in IPython 2.0+,
namely JavaScript widgets."

I had to change the world 'values' to 'options' in

dw = DropdownWidget(values=OrderedDict([

and

dw.value = dw.values['SciPy 2013'].

Using IPython 3.1.0, Python 2.7.9.

AttributeError: 'VispyWidget' object has no attribute 'on_msg'


AttributeError Traceback (most recent call last)
in ()
----> 1 c.show()
2 app.run();

f:\anaconda3\vispy-master\vispy\app\canvas.py in show(self, visible, run)
427 Run the backend event loop.
428 """
--> 429 self._backend._vispy_set_visible(visible)
430 if run:
431 self.app.run()

f:\anaconda3\vispy-master\vispy\app\backends_ipynb_webgl.py in _vispy_set_visible(self, visible)
206 return
207 if self._widget is None:
--> 208 self._widget = VispyWidget()
209 self._widget.set_canvas(self._vispy_canvas)
210 display(self._widget)

f:\anaconda3\vispy-master\vispy\app\backends\ipython_widget.py in init(self, **kwargs)
60 def init(self, **kwargs):
61 super(VispyWidget, self).init(**kwargs)
---> 62 self.on_msg(self.events_received)
63 self.canvas = None
64 self.canvas_backend = None

AttributeError: 'VispyWidget' object has no attribute 'on_msg'

re notebook: 07_webcam_py3 ShimWarning

error on running the first cell:
ShimWarning: The IPython.html package has been deprecated. You should import from notebook instead. IPython.html.widgets has moved to ipywidgets.
"IPython.html.widgets has moved to ipywidgets.", ShimWarning)
/home/dgd/.local/lib/python3.5/site-packages/IPython/utils/traitlets.py:5: UserWarning: IPython.utils.traitlets has moved to a top-level traitlets package.
warn("IPython.utils.traitlets has moved to a top-level traitlets package.")

bokeh API changed

I've just installed bokeh 0.8.2 (the version pip finds). The 'line' and 'scatter' functions are now methods of a figure object, so in notebook chapter06/03_bokeh I had to change the first example to:

p = bkh.figure(title="random data", x_axis_label='x', y_axis_label='y')
p.line(x, y, line_width=5)
bkh.show(p)

and the second to:

p = bkh.figure(title="flower petal shapes by color", x_axis_label='petal_length', y_axis_label='petal_width')
p.scatter(flowers["petal_length"],
flowers["petal_width"],
color=flowers["color"],
fill_alpha=0.25, size=10,)
bkh.show(p)

Errors in http://ipython-books.github.io/featured-03/

A few issues:
Step 2: make sure to actually use "g = nx.read_shp("data/tl_2013_06_prisecroads.shp", simplify =False)". Without setting simplify to False, grid-like road structures become completely broken graphs. This is extremely important when using open street map data for example.

Step 3: to get the maximally connected subgraph, use the command 'max(nx.connected_component_subgraphs(g.to_undirected()), key=len)'

Step 7: Since the "JSON" coordinates are in the form (lon,lat), you are actually passing the wrong set to the geocalc function. Change to 'return np.sum(geocalc(path[1:,1],path[1:,0],path[:-1,1],path[:-1,0]))'. Surprisingly, the path it finds is nearly the same, and the distance is only a bit different.

Vispy Code (6.6) Error

In the 6th python cell:

program['a_position'] = np.c_[
        np.linspace(-1.0, +1.0, 1000),
        np.random.uniform(-0.5, +0.5, 1000)]

results in:

TypeError: data must be 32-bit not float64

I may mess with this later, but I know next to nothing about vispy so I'm not sure how well I can troubleshoot the issue.

Update book release info

Every recipe ends with a footer:

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

Since the book has been already published, I suggest changing it to a link to the book description/publication info.

Issue with explanation of shared memory in 01_numpy_performance

Thanks for writing the cookbook and sharing it online. This is certain to become a great resource for our users.

In cookbook 01_numpy_performance, it is recommended to use x.__array_interface__['data'][0] to determine if an array is sharing data with another array. This is only useful if the offset of the arrays are the same, not if one array is a subarray/slice/view of another.

For example, here, two arrays are sharing the same data but they have different starting pointers.

In [1]: import numpy as np

In [2]: x = np.arange(10)

In [3]: y = x[1::2]

In [4]: x.__array_interface__['data'][0]
Out[4]: 46090608

In [5]: y.__array_interface__['data'][0]                                                                                                                                                                                            
Out[5]: 46090616

You could probably figure out that their data areas are overlapping, but that’s kind of expensive/complex.

The best way I’ve found to find out if two arrays share the same data:

def get_data_base(arr):
    base = arr.base
    while base is not None:
        base = base.base
    return base

>>> get_data_base(x) is get_data_base(y)
True

Chopped values in the energy-minimization example

One of the featured recipes is this nice energy-minimization problem. But I think the final plot is missing some really interesting features. In particular, the function that plots the spring bar contains the line

color=plt.cm.copper(c*150))

This means that negative values (corresponding to compression of the spring, rather than extension) don't get any special coloring -- they get chopped. If you allow for negative values, you can see those compressed springs:

image

You can see that the lower springs near the wall are very compressed, and the diagonal ones get some compression as well. I think that's too interesting to ignore! :)

The simplest way to deal with this would just be

color=plt.cm.copper(abs(c*150)))

But that wouldn't show you which springs are compressed and which are extended. To get the plot above, I defined a new color function

def spring_color_map(c):
    min_c, max_c = -0.00635369422326, 0.00836362559722
    ratio = (max_c-c) / (max_c-min_c)
    color = plt.cm.coolwarm(ratio)
    shading = np.sqrt(abs(ratio-0.5)*2)
    return (shading*color[0], shading*color[1], shading*color[2], color[3])

and then in the plotting function, I did

color=spring_color_map(c)

IPython 3.0 updates

  • Update widget instructions
  • Check other potential IPython changes
  • Test with the latest versions of the other dependencies

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.