Numpy学习笔记(一)

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 alias array. Note that numpy.array is not the same as the Standard Python Library class array.array, which only handles one-dimensional arrays and offers less functionality. The more important attributes of an ndarray object 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 the shape 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, optional
		Start of interval. The interval includes this value. The default start value is 0.
	stop : number
		End 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, optional
		Spacing 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 : dtype
		The type of the output array. If dtype is not given, infer the data type from the other input arguments.
Returns:
	arange : ndarray
		Array 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)
>>> a
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> a.shape
(3, 5)
>>> a.ndim
2
>>> a.dtype.name
'int64'
>>> a.itemsize
8
>>> a.size
15
>>> type(a)
<type 'numpy.ndarray'>
>>> b = np.array([6, 7, 8])
>>> b
array([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 np
	a = np.array([1, 2, 3])  	# Create a rank 1 array
	print 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 array
	print a                 # Prints "[5, 2, 3]"

	b = np.array([[1,2,3],[4,5,6]]) # Create a rank 2 array
	print b.shape             # Prints "(2, 3)"
	print b[0, 0], b[0, 1], b[1, 0] # Prints "1 2 4"
 

result



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值