Sunday, March 9, 2014

Python Study Note: NumPy array - 1

Array is the key concept in NumPy, and its creations & operations are quite useful to remember.

First, there are two new types: ndarray, and dtype. ndarray represents n-dimension array, and dtype means data-type for NumPy.

1. Array creation

    One comprehensive list is given Here. I will only write down some important ones.

    1.1 ones and zeros

        eye(N[, M, k, dtype])   ---   Return a 2-D array with ones on the diagonal and zeros elsewhere.
        zeros(shape[, dtype, order])   ---   Return a new array of given shape and type, filled with zeros.
        zeros_like(a[, dtype, order, subok])   ---   Return an array of zeros with the same shape and type as a given array.
        There are also ones() and ones_like() function, which usage is same as zeros.

    1.2 from existing data

        numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)  --- convert other object into np.array

    1.3 numeric range

        arange([start,] stop[, step,][, dtype])   ---   Return evenly spaced values within a given interval.
        linspace(start, stop[, num, endpoint, retstep])   ---   Return evenly spaced numbers over a specified interval.
        logspace(start, stop[, num, endpoint, base])   ---   Return numbers spaced evenly on a log scale.

2. Array attributes

    It is always helpful to understand what is the size/shape of the current array; they are the array's attributes. 
    ndarray.shape   ---   Tuple of array dimensions.
    ndarray.ndim   ---   Number of array dimensions.
    ndarray.size   ---   Number of elements in the array.
    ndarray.nbytes   ---   Total bytes consumed by the elements of the array.
    ndarray.dtype   ---   Data-type of the array’s elements.
    ndarray.T   ---   Same as self.transpose(), except that self is returned if self.ndim < 2.
    ndarray.real   ---   The real part of the array.
    ndarray.imag   ---   The imaginary part of the array.
    ndarray.flat   ---   A 1-D iterator over the array.

No comments:

Post a Comment