Movatterモバイル変換


[0]ホーム

URL:


SciPy

Array creation

Introduction

There are 5 general mechanisms for creating arrays:

  1. Conversion from other Python structures (e.g., lists, tuples)
  2. Intrinsic numpy array creation objects (e.g., arange, ones, zeros,etc.)
  3. Reading arrays from disk, either from standard or custom formats
  4. Creating arrays from raw bytes through the use of strings or buffers
  5. Use of special library functions (e.g., random)

This section will not cover means of replicating, joining, or otherwiseexpanding or mutating existing arrays. Nor will it cover creating objectarrays or structured arrays. Both of those are covered in their own sections.

Converting Python array_like Objects to NumPy Arrays

In general, numerical data arranged in an array-like structure in Python canbe converted to arrays through the use of the array() function. The mostobvious examples are lists and tuples. See the documentation for array() fordetails for its use. Some objects may support the array-protocol and allowconversion to arrays this way. A simple way to find out if the object can beconverted to a numpy array using array() is simply to try it interactively andsee if it works! (The Python Way).

Examples:

>>>x=np.array([2,3,1,0])>>>x=np.array([2,3,1,0])>>>x=np.array([[1,2.0],[0,0],(1+1j,3.)])# note mix of tuple and lists,    and types>>>x=np.array([[1.+0.j,2.+0.j],[0.+0.j,0.+0.j],[1.+1.j,3.+0.j]])

Intrinsic NumPy Array Creation

NumPy has built-in functions for creating arrays from scratch:

zeros(shape) will create an array filled with 0 values with the specifiedshape. The default dtype is float64.

>>>np.zeros((2,3))array([[ 0., 0., 0.], [ 0., 0., 0.]])

ones(shape) will create an array filled with 1 values. It is identical tozeros in all other respects.

arange() will create arrays with regularly incrementing values. Check thedocstring for complete information on the various ways it can be used. A fewexamples will be given here:

>>>np.arange(10)array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>>np.arange(2,10,dtype=float)array([ 2., 3., 4., 5., 6., 7., 8., 9.])>>>np.arange(2,3,0.1)array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])

Note that there are some subtleties regarding the last usage that the usershould be aware of that are described in the arange docstring.

linspace() will create arrays with a specified number of elements, andspaced equally between the specified beginning and end values. Forexample:

>>>np.linspace(1.,4.,6)array([ 1. ,  1.6,  2.2,  2.8,  3.4,  4. ])

The advantage of this creation function is that one can guarantee thenumber of elements and the starting and end point, which arange()generally will not do for arbitrary start, stop, and step values.

indices() will create a set of arrays (stacked as a one-higher dimensionedarray), one per dimension with each representing variation in that dimension.An example illustrates much better than a verbal description:

>>>np.indices((3,3))array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]])

This is particularly useful for evaluating functions of multiple dimensions ona regular grid.

Reading Arrays From Disk

This is presumably the most common case of large array creation. The details,of course, depend greatly on the format of data on disk and so this sectioncan only give general pointers on how to handle various formats.

Standard Binary Formats

Various fields have standard formats for array data. The following lists theones with known python libraries to read them and return numpy arrays (theremay be others for which it is possible to read and convert to numpy arrays socheck the last section as well)

HDF5:h5pyFITS:Astropy

Examples of formats that cannot be read directly but for which it is not hard toconvert are those formats supported by libraries like PIL (able to read andwrite many image formats such as jpg, png, etc).

Common ASCII Formats

Comma Separated Value files (CSV) are widely used (and an export and importoption for programs like Excel). There are a number of ways of reading thesefiles in Python. There are CSV functions in Python and functions in pylab(part of matplotlib).

More generic ascii files can be read using the io package in scipy.

Custom Binary Formats

There are a variety of approaches one can use. If the file has a relativelysimple format then one can write a simple I/O library and use the numpyfromfile() function and .tofile() method to read and write numpy arraysdirectly (mind your byteorder though!) If a good C or C++ library exists thatread the data, one can wrap that library with a variety of techniques thoughthat certainly is much more work and requires significantly more advancedknowledge to interface with C or C++.

Use of Special Libraries

There are libraries that can be used to generate arrays for special purposesand it isn’t possible to enumerate all of them. The most common uses are useof the many array generation functions in random that can generate arrays ofrandom values, and some utility functions to generate special matrices (e.g.diagonal).

Table Of Contents

Previous topic

Data types

Next topic

I/O with NumPy

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp