Numpy学习笔记(一)

来源:互联网 发布:天猫数据直播间 编辑:程序博客网 时间:2024/06/01 08:02

Numpy学习笔记(一)


NumPy’s main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes(轴). The number of axes is rank(秩).

(1)For example, the coordinates of a point in 3D space [1, 2, 1] is an array of rank 1, because it has one axis. That axis has a length of 3.

(2)

[ [1.,0.,0.],
  [0.,1.,2.] ]

 the array has rank 2 (it is 2-dimensional). The first dimension (axis) has a length of 2, the second dimension has a length of 3.

(3)NumPy’s array class is called ndarray. It is also known by the aliasarray. Note thatnumpy.array is not the same as the Standard Python Library classarray.array, which only handles one-dimensional arrays and offers less functionality. The more important attributes of anndarrayobject are:

  • ndarray.ndim
the number of axes (dimensions) of the array. In the Python world, the number of dimensions is referred to as rank.
  • ndarray.shape
the dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with n rows and m columns,shape will be(n,m). The length of theshape tuple is therefore the rank, or number of dimensions,ndim.
  • ndarray.size
the total number of elements of the array. This is equal to the product of the elements of shape.
  • ndarray.dtype
an object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.
  • ndarray.itemsize
the size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.
  • ndarray.data
the buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.

(4)numpy.arange
numpy.arange([start]stop[step]dtype=None)


Return evenly spaced values within a given interval.

Parameters:start : number, optionalStart of interval. The interval includes this value. The default start value is 0.stop : numberEnd of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out.step : number, optionalSpacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified, start must also be given.dtype : dtypeThe type of the output array. If dtype is not given, infer the data type from the other input arguments.
Returns:
arange : ndarrayArray of evenly spaced values.For floating point arguments, the length of the result is ceil((stop - start)/step). Because of floating point overflow, this rule may result in the last element of out being greater than stop.

An example

>>> import numpy as np>>> a = np.arange(15).reshape(3, 5)>>> aarray([[ 0,  1,  2,  3,  4],       [ 5,  6,  7,  8,  9],       [10, 11, 12, 13, 14]])>>> a.shape(3, 5)>>> a.ndim2>>> a.dtype.name'int64'>>> a.itemsize8>>> a.size15>>> type(a)<type 'numpy.ndarray'>>>> b = np.array([6, 7, 8])>>> barray([6, 7, 8])>>> type(b)<type 'numpy.ndarray'>

1、Arrays

(1) initialize numpy arrays from nested Python lists, and access elements using square brackets(方括号[]):

code:

import numpy as npa = np.array([1, 2, 3])  # Create a rank 1 arrayprint type(a)            # Prints "<type 'numpy.ndarray'>"print a.shape            # Prints "(3,)"print a[0], a[1], a[2]   # Prints "1 2 3"a[0] = 5                # Change an element of the arrayprint a                 # Prints "[5, 2, 3]"
b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 arrayprint b.shape             # Prints "(2, 3)"print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"

result



0 0