Machine learning library NumPy Python Tutorial

0 Numpy brief

Numpy Python is a scientific computing library, a matrix operation function, which is typically used with Scipy, matplotlib. In fact, list already provided similar representation of the matrix, but numpy provides more functions for us. If you come into contact with matlab, scilab, then numpy good start.

1 Installation

pip install numpy

In NumPy, the dimension referred to axis(plural axes), the number of dimensions is called rank.

(A common practice import numpu as np simple input)

2 Multidimensional Arrays

NumPy array class is ndarraythat it has an alias  numpy.array, but with the Python standard library array.arrayis not the same. The latter is only a one-dimensional array. And ndarrayhas the following attributes:

  • ndarray.ndim: The dimension of the array. In the Python world, called the dimensionrank
  • ndarray.shape: The dimension of the array. This is a series of numbers, the length (the dimension of the array ndim) determined. For example: the length of the one-dimensional array of n shapeis n. A matrix of n rows and m columns shapeis n, m
  • ndarray.size: The number of all elements in the array
  • ndarray.dtype: Type of array elements, for example numpy.int32numpy.int16ornumpy.float64
  • ndarray.itemsize: The size of each element of the array in bytes
  • ndarray.data: Buffer storage array elements. Usually we only need to access the elements by index, without the need to access the buffer

In a list or tuple variable parameter generating one-dimensional array:

np.array([1,2,3,4]) 

np.array((1.2,2,3,4))  

To list or tuple variables to produce a two-dimensional array or multidimensional arrays as elements:

np.array(((1,2,3),(4,5,6)))  

np.array([[1,2,3],[4,5,6]])  

When we can create an array of the specified type elements, such as this:

np.array([[1,2],[3,4]], dtype=complex )

You can use astype conversion type, file handling when this will be very useful, attention astype call will return a new array that is a copy of the original data.

numeric_strings2 = np.array(['1.23','2.34','3.45'],dtype=np.string_)
print(numeric_strings2)
[b'1.23' b'2.34' b'3.45']

t=numeric_strings2.astype(float)
print(t)
[ 1.23  2.34  3.45]

3 numpy index (index) and sliced ​​(slicing)

  • index and slicing: a first abscissa value array-like, the second vertical axis;
x = np.array (((1,2,3,4), (5,6,7,8), (9,10,11,12 ))) 
and = x [: 1 ] 
and [0] = 20
 print (and)
 print (x)

result:

 

The above can be found by changing the x y will change, so we can infer, x and y are pointing to the same memory space value, the system does not open up space for the new y the x value is assigned in the past.

arr = np.arange(10)
arr[3:6]=10
print(arr)

result:

 

Think about why such a design? Numpy designed to handle large data, if the data is copied, then slice using will have a great performance and memory consumption issues.

  •  If you say that the need for a copy of the array is not a view you can do the following:
arr = np.arange(10) 
arr_copy = arr[3:6].copy() 
print(arr_copy)
arr_copy[:]=24 
print(arr_copy)
print(arr)

result:

 

  • Look at the list of modified sections ( where the depth of the design to copy the python, which slice belongs shallow copy ):
l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
l[5:8] = 12 #报错 TypeError: can only assign an iterable
print(l)
l1= l[5:8]
print(l1)
l1[0]=12 
print(l1)
print(l)

result:

 

4 multidimensional array index (index), sliced ​​(slicing)

arr2d = np.arange(1,10).reshape(3,3)
print(arr2d)
print(arr2d[2])
print(arr2d[0][2])
print(arr2d[0,2])

result:

 

5 basic matrix calculation

Transpose:

a = np.array([[1,0],[2,3]])
print(a)
print()
print(a.transpose())

result:

 

Eigenvalues, eigenvectors:

 

 

6 Shape and operation

除了生成数组之外,当我们已经持有某个数据之后,我们可能会需要根据已有数组来产生一些新的数据结构,这时候我们可以使用下面这些函数:

  • reshape:根据已有数组和指定的shape,生成一个新的数组
  • vstack:用来将多个数组在垂直(v代表vertical)方向拼接(数组的维度必须匹配)
  • hstack:用来将多个数组在水平(h代表horizontal)方向拼接(数组的维度必须匹配)
  • hsplit:用来将数组在水平方向拆分
  • vsplit:用来将数组在垂直方向拆分

7 特定array的创建

 

在实际上的项目工程中,我们常常会需要一些特定的数据,NumPy中提供了这么一些辅助函数:

  • zeros:用来创建元素全部是0的数组
  • ones:用来创建元素全部是1的数组
  • empty:用来创建未初始化的数据,因此是内容是不确定的
  • arange:通过指定范围和步长来创建数组
  • linespace:通过指定范围和元素数量来创建数组
  • random:用来生成随机数

 

参考:

numpy 基础入门 - 30分钟学会numpy 

Python 机器学习库 NumPy 教程

Guess you like

Origin www.cnblogs.com/hoaprox/p/10968053.html