Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learning NumPy Array
Learning NumPy Array

Learning NumPy Array: Supercharge your scientific Python computations by understanding how to use the NumPy library effectively

By Ivan Idris
$28.99
Book Jun 2014 164 pages 1st Edition
eBook
$18.99 $12.99
Print
$28.99
Subscription
$15.99 Monthly
eBook
$18.99 $12.99
Print
$28.99
Subscription
$15.99 Monthly

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Black & white paperback book shipped to your address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Jun 13, 2014
Length 164 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783983902
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Learning NumPy Array

Chapter 1. Getting Started with NumPy

Let's get started. We will install NumPy and related software on different operating sytems and have a look at some simple code that uses NumPy. As mentioned in the Preface, SciPy is closely related to NumPy, so you will see the name SciPy appearing throughout the chapter. At the end of this chapter, you will find pointers on how to find additional information online if you get stuck or are uncertain about the best way to solve problems.

In this chapter, we shall learn the following skills:

  • Installing Python, SciPy, Matplotlib, IPython, and NumPy on Windows, Linux, and Macintosh

  • Writing simple NumPy code

  • Adding arrays

  • Making use of online resources and help

Python


NumPy is based on Python, so it is required to have Python installed. On some operating systems, Python is already installed. You, however, need to check whether the Python version is compatible with the NumPy version you want to install. There are many implementations of Python, including commercial implementations and distributions. In this book, we will focus on the standard CPython implementation, which is guaranteed to be compatible with NumPy.

NumPy has binary installers for Windows, various Linux distributions, and Mac OS X. There is also a source distribution, if you prefer that. You need to have Python 2.4.x or above installed on your system. Python 2.7.6 is currently the best Python version to have because most scientific Python libraries support it.

Installing NumPy, Matplotlib, SciPy, and IPython on Windows


Installing NumPy on Windows is a necessary but, fortunately, straightforward task that we will cover in detail. You only need to download an installer, and a wizard will guide you through the installation steps. It is recommended that Matplotlib, SciPy, and IPython be installed. However, this is not required to enjoy this book. The actions we will take are as follows:

  1. Download a NumPy installer for Windows from the SourceForge website at http://sourceforge.net/projects/numpy/files/.

  2. Choose the appropriate version. In this example, we chose numpy-1.8.0-win32-superpack-python2.7.exe.

  3. Open the EXE installer by double-clicking on it.

  4. Now, we can see a description of NumPy and its features, as shown in the previous screenshot. Click on the Next button.

  5. If you have Python installed, it should automatically be detected. If it is not detected, maybe your path settings are wrong. At the end of this chapter, resources are listed in case you have problems installing NumPy.

  6. In this example, Python 2.7 was found. Click on the Next button if Python is found, otherwise, click on the Cancel button and install Python (NumPy cannot be installed without Python). Click on the Next button. This is the point of no return. Well, kind of, but it is best to make sure that you are installing to the proper directory and so on and so forth. Now the real installation starts. This may take a while.

  7. Install SciPy and Matplotlib with the Enthought distribution at http://www.enthought.com/products/epd.php.

    Note

    The situation around installers is rapidly evolving. Other alternatives exist in various stage of maturity (see http://www.scipy.org/install.html). It might be necessary to put the msvcp71.dll file in your C:\Windows\system32 directory. You can get it at http://www.dll-files.com/dllindex/dll-files.shtml?msvcp71. A Windows IPython installer is available on the IPython website (see http://ipython.scipy.org/Wiki/IpythonOnWindows).

Installing NumPy, Matplotlib, SciPy, and IPython on Linux


Installing NumPy and related recommended software on Linux depends on the distribution you have. We will discuss how you would install NumPy from the command line although you could probably use graphical installers; it depends on your distribution (distro). The commands to install Matplotlib, SciPy, and IPython are the same—only the package names are different. Installing Matplotlib, SciPy, and IPython is recommended, but optional.

Most Linux distributions have NumPy packages. We will go through the necessary steps for some of the popular Linux distros:

  • Run the following instructions from the command line for installing NumPy on Red Hat:

    yum install python-numpy
    
  • To install NumPy on Mandriva, run the following command-line instruction:

    urpmi python-numpy
    
  • To install NumPy on Gentoo, run the following command-line instruction:

    sudo emerge numpy
    
  • To install NumPy on Debian or Ubuntu, we need to type the following:

    sudo apt-get install python-numpy
    

The following table gives an overview of the Linux distributions and corresponding package names for NumPy, SciPy, Matplotlib, and IPython:

Linux distribution

NumPy

SciPy

Matplotlib

IPython

Arch Linux

python-numpy

python-scipy

python-matplotlib

ipython

Debian

python-numpy

python-scipy

python-matplotlib

ipython

Fedora

numpy

python-scipy

python-matplotlib

ipython

Gentoo

dev-python/numpy

scipy

matplotlib

ipython

OpenSUSE

python-numpy, python-numpy-devel

python-scipy

python-matplotlib

ipython

Slackware

numpy

scipy

matplotlib

ipython

Installing NumPy, Matplotlib, and SciPy on Mac OS X


You can install NumPy, Matplotlib, and SciPy on the Mac with a graphical installer or from the command line with a port manager, such as MacPorts or Fink, depending on your preference.

Note

We can get a NumPy installer from the SourceForge website at http://sourceforge.net/projects/numpy/files/. Similar files exist for Matplotlib and SciPy. Just change numpy in the previous URL to scipy or matplotlib. IPython didn't have a GUI installer at the time of writing. Download the appropriate DMG file as shown in the following screenshot; usually the latest one is the best. Another alternative is the SciPy Superpack (https://github.com/fonnesbeck/ScipySuperpack). Whichever option you choose, it is important to make sure that updates which impact the system Python library don't negatively influence the already installed software by not building against the Python library provided by Apple.

We will install NumPy with a GUI installer using the following steps:

  1. Open the DMG file as shown in the following screenshot (in this example, numpy-1.8.0-py2.7-python.org-macosx10.6.dmg):

  2. Double-click on the icon of the opened box, that is, the one having a subscript that ends with .mpkg. We will be presented with the welcome screen of the installer.

  3. Click on the Continue button to go to the Read Me screen, where we will be presented with a short description of NumPy, as shown in the following screenshot:

  4. Click on the Continue button to go to the License screen.

  5. Read the license, click on the Continue button, and then on the Accept button, when prompted to accept the license. Continue through the next screens and click on the Finish button at the end.

Alternatively, we can install NumPy, SciPy, Matplotlib, and IPython through the MacPorts route or with Fink. The following installation steps install all these packages. We only need NumPy for the tutorials in this book, so please omit the packages you are not interested in.

  • To install with MacPorts, type the following command:

    sudo port install py-numpy py-scipy py-matplotlib py-ipython
    
  • Fink also has packages for NumPy: scipy-core-py24, scipy-core-py25, and scipy-core-py26. The SciPy packages are: scipy-py24, scipy-py25, and scipy-py26. We can install NumPy and the other recommended packages that we will be using in this book for Python 2.6 with the following command:

    fink install scipy-core-py26 scipy-py26 matplotlib-py26
    

Building from source


As a last resort or if we want to have the latest code, we can build from source. In practice it shouldn't be that hard although, depending on your operating system, you might run into problems. As operating systems and related software are rapidly evolving, the best you can do is search online or ask for help. In this chapter, we give pointers on good places to look for help.

The steps to install NumPy from source are straightforward and given here. We can retrieve the source code for NumPy with .git as follows:

git clone git://github.com/numpy/numpy.git numpy

Install on /usr/local with the following command:

python setup.py build
sudo python setup.py install --prefix=/usr/local

To build, we need a C compiler such as GCC and the Python header files in the python-dev or python-devel package.

NumPy arrays


After going through the installation of NumPy, it's time to have a look at NumPy arrays. NumPy arrays are more efficient than Python lists when it comes to numerical operations. NumPy arrays are in fact specialized objects with extensive optimizations. NumPy code requires less explicit loops than the equivalent Python code. This is based on vectorization.

If we go back to high school mathematics, then we should remember the concepts of scalars and vectors. The number 2 for instance is a scalar. When we add 2 and 2, we are performing scalar addition. We can form a vector out of a group of scalars. In Python programming terms, we will then have a one-dimensional array. This concept can of course be extended to higher dimensions. Performing an operation on two arrays such as addition can be reduced to a group of scalar operations. In straight Python, we will do that with loops going through each element in the first array and adding it to the corresponding element in the second array. However, this is more verbose than the way it is done in mathematics. In mathematics, we treat the addition of two vectors as a single operation. That's the way NumPy arrays do it too and there are certain optimizations using low-level C routines, which make these basic operations more efficient. We will cover NumPy arrays in more detail in the next chapter.

Adding arrays

Imagine that we want to add two vectors called a and b. A vector is used here in the mathematical sense, which means a one-dimensional array. We will learn in Chapter 4, Simple Predictive Analytics with NumPy, about specialized NumPy arrays that represent matrices. The vector a holds the squares of integers 0 to n, for instance. If n is equal to 3, then a contains 0, 1, or 4. The vector b holds the cubes of integers 0 to n, so if n is equal to 3, then the vector b is equal to 0, 1, or 8. How would you do that using plain Python? After we come up with a solution, we will compare it with the NumPy equivalent.

The following function solves the vector addition problem using pure Python without NumPy:

def pythonsum(n):
   a = range(n)
   b = range(n)
   c = []

   for i in range(len(a)):
       a[i] = i ** 2
       b[i] = i ** 3
       c.append(a[i] + b[i])

   return c

The following is a function that achieves the same with NumPy:

def numpysum(n):
  a = numpy.arange(n) ** 2
  b = numpy.arange(n) ** 3
  c = a + b
  return c

Notice that numpysum() does not need a for loop. Also, we used the arange function from NumPy, which creates a NumPy array for us with integers 0 to n. The arange function was imported; that is why it is prefixed with numpy.

Now comes the fun part. Remember that it is mentioned in the Preface that NumPy is faster when it comes to array operations. How much faster is Numpy, though? The following program will show us by measuring the elapsed time in microseconds, for the numpysum and pythonsum functions. It also prints the last two elements of the vector sum. Let's check that we get the same answers when using Python and NumPy:

#!/usr/bin/env/python

import sys
from datetime import datetime
import numpy as np

"""
 This program demonstrates vector addition the Python way.
 Run from the command line as follows
     
  python vectorsum.py n
 
 where n is an integer that specifies the size of the vectors.

 The first vector to be added contains the squares of 0 up to n.
 The second vector contains the cubes of 0 up to n.
 The program prints the last 2 elements of the sum and the elapsed time.
"""

def numpysum(n):
   a = np.arange(n) ** 2
   b = np.arange(n) ** 3
   c = a + b

   return c

def pythonsum(n):
   a = range(n)
   b = range(n)
   c = []

   for i in range(len(a)):
       a[i] = i ** 2
       b[i] = i ** 3
       c.append(a[i] + b[i])

   return c
   
size = int(sys.argv[1])

start = datetime.now()
c = pythonsum(size)
delta = datetime.now() - start
print "The last 2 elements of the sum", c[-2:]
print "PythonSum elapsed time in microseconds", delta.microseconds

start = datetime.now()
c = numpysum(size)
delta = datetime.now() - start
print "The last 2 elements of the sum", c[-2:]
print "NumPySum elapsed time in microseconds", delta.microseconds

The output of the program for the 1000, 2000, and 3000 vector elements is as follows:

$ python vectorsum.py 1000
The last 2 elements of the sum [995007996, 998001000]
PythonSum elapsed time in microseconds 707
The last 2 elements of the sum [995007996 998001000]
NumPySum elapsed time in microseconds 171
$ python vectorsum.py 2000
The last 2 elements of the sum [7980015996, 7992002000]
PythonSum elapsed time in microseconds 1420
The last 2 elements of the sum [7980015996 7992002000]
NumPySum elapsed time in microseconds 168
$ python vectorsum.py 4000
The last 2 elements of the sum [63920031996, 63968004000]
PythonSum elapsed time in microseconds 2829
The last 2 elements of the sum [63920031996 63968004000]
NumPySum elapsed time in microseconds 274

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Clearly, NumPy is much faster than the equivalent normal Python code. One thing is certain: we get the same results whether we are using NumPy or not. However, the result that is printed differs in representation. Notice that the result from the numpysum function does not have any commas. How come? Obviously we are not dealing with a Python list, but with a NumPy array. It was mentioned in the Preface that NumPy arrays are specialized data structures for numerical data. We will learn more about NumPy arrays in Chapter 2, NumPy Basics.

Online resources and help


The main documentation website for NumPy and SciPy is at http://docs.scipy.org/doc/. On this web page, we can browse the NumPy reference at http://docs.scipy.org/doc/numpy/reference/ and the user guide, as well as several tutorials.

NumPy has a wiki with lots of documentation at http://docs.scipy.org/numpy/Front%20Page/.

The NumPy and SciPy forum can be found at http://ask.scipy.org/en.

The popular Stack Overflow software development forum has hundreds of questions tagged as numpy. To view them, go to http://stackoverflow.com/questions/tagged/numpy.

If you are really stuck with a problem or you want to be kept informed of NumPy's development, you can subscribe to the NumPy discussion mailing list. The e-mail address is . The number of e-mails per day is not too high, and there is almost no spam to speak of. Most importantly, developers actively involved with NumPy also answer questions asked on the discussion group. The complete list can be found at http://www.scipy.org/Mailing_Lists.

For IRC users, there is an IRC channel on irc://irc.freenode.net. The channel is called #scipy, but you can also ask NumPy questions since SciPy users also have knowledge of NumPy, as SciPy is based on NumPy. There are at least 50 members on the SciPy channel at all times.

Summary


In this chapter, we installed NumPy and other recommended software that we will be using in some tutorials. We got a vector addition program working and convinced ourselves that NumPy has superior performance. In addition, we explored the available NumPy documentation and online resources.

In the next chapter, we will take a look under the hood and explore some fundamental concepts, including arrays and data types.

Left arrow icon Right arrow icon

Key benefits

What you will learn

Install NumPy and discover its arrays and features Perform data analysis and complex array operations with NumPy Analyze time series and perform signal processing Understand NumPy modules and explore the scientific Python ecosystem Improve the performance of calculations with clean and efficient NumPy code Analyze large data sets using statistical functions and execute complex linear algebra and mathematical computations

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Black & white paperback book shipped to your address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Jun 13, 2014
Length 164 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783983902
Category :
Concepts :

Table of Contents

14 Chapters
Learning NumPy Array Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.PacktPub.com Chevron down icon Chevron up icon
Preface Chevron down icon Chevron up icon
Getting Started with NumPy Chevron down icon Chevron up icon
NumPy Basics Chevron down icon Chevron up icon
Basic Data Analysis with NumPy Chevron down icon Chevron up icon
Simple Predictive Analytics with NumPy Chevron down icon Chevron up icon
Signal Processing Techniques Chevron down icon Chevron up icon
Profiling, Debugging, and Testing Chevron down icon Chevron up icon
The Scientific Python Ecosystem Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela