Learning numpy

preface

I have been using MATLAB to deal with matrix and determinant operation in university. Matlab is powerful and really useful, but I’ve forgotten it all these years. At present, recommendation algorithm, data processing, machine learning, artificial intelligence, data analysis and processing are indeed needed, and currently there are some deficiencies in many aspects, so we need to improve the knowledge in this area, which also requires us to pick up matrix, determinant and other related things. It has recently been found that NUMpy, MatplotLab, PANDAS, scipy can completely replace MATLAB, and the modules are free, and there are quite a lot of online tutorials. Since all the conditions are met, I decided to settle down and learn these things well, hoping to finally apply what I have learned. Learn Numpy first and figure out all the concepts and uses in a week or so, which should be 4-6 articles.


Numpy

NumPy is a very fast math library for array calculations. Support a large number of dimensional array and matrix operations, in addition to array operations for a large number of mathematical functions, including:

  1. A powerful N – dimensional array object nDARray
  2. Broadcast function
  3. Tools to integrate C/C++/Fortran code
  4. Linear algebra, Fourier transform, random number generation, etc
Numpy is installed using PIP
1. The latest version has built-in PIP module
python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sympy nose
   
Copy the code
2. Check whether the installation is successful
>>> import numpy as np
>>> np.__version__
'1.16.3'
Copy the code
NumPy Ndarray object

Ndarray is an N-dimensional combination of the same type of data, each element occupies the same type of memory space, and the index starts at 0. Ndarray consists of the following contents:

  1. A pointer to data (memory or a chunk of data in a memory-mapped file).
  2. A data type, or Dtype, that describes a grid of fixed-size values in an array.
  3. A tuple representing array shapes, representing tuples of dimensions.
  4. A stride where the integer is the number of bytes that need to be “crossed” in order to advance to the next element in the current dimension.
4. Create an NdarRay object

The following is an example:

  • Create a one-dimensional NDARray object from a list:
>>> a = [1,2,3,4]
>>> dt = np.array(a)
>>> print(dt)
[1 2 3 4]
Copy the code
  • Create a 2d Ndarray object from a list:
> > > a = [[1, 2, 3], [4 and 6]] > > > dt = np, array (a) > > >print(dt)
[[1 2 3]
 [4 5 6]]
Copy the code
  • Create multi-dimensional Ndarray objects from lists:
> > > a = [[[1, 2, 3]], [[4 and 6]]] > > > dt = np, array (a) > > >print(dt)
[[[1 2 3]]

 [[4 5 6]]]
Copy the code
Ndarray object with type
  • Create an ndarray object with dType =float from a list:
> > > a = [[1, 2, 3], [4 and 6]] > > > dt = np, array (a, dtype ='float') > > > > > >print(dt)
[[1. 2. 3.]
 [4. 5. 6.]]
Copy the code
  • Create an ndarray object with dType =float from a list:
> > > a = [[1, 2, 3], [4 and 6]] > > > dt = np, array (a, dtype ='complex') > > >print(dt)
[[1.+0.j 2.+0.j 3.+0.j]
 [4.+0.j 5.+0.j 6.+0.j]]
>>> 

Copy the code
Here’s a look at the dType

The dtype type is used to identify variable types, just like Python data types, but numpy data types are far more complex than Python types, which we’ll cover briefly in this chapter and in a separate section later.

  • Integer types :(divided into signed and unsigned types)
Type name instructions
int_ The default type is int32 or int64
int8 Signed integers (-128 to 127)
int16 Signed integers (-32768 to 32767)
int32 Signed integers (-2147483648 to 2147483647)
int64 Signed integer (-9223372036854775808 to 9223372036854775807)
uint8 Unsigned integers (0 to 255)
uint16 Unsigned integers (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
  • Floating point types
Type name instructions
float_ Short type of float64
float16 A semi-precision floating point number, including 1 sign bit, 5 exponent bits, and 10 mantissa bits
float32 A semi-precision floating point number containing 1 sign bit, 8 exponent bits, and 32 mantissa bits
float64 A half-precision floating point number, including 1 sign bit, 11 exponent bits, and 52 mantissa bits
  • The plural types
Type name instructions
complex_ A shorthand type of complex128, 128-bit complex numbers
complex64 Representation of double 32-bit floating point numbers (real and imaginary parts)
complex128 Representation of double 64-bit floating point numbers (real and imaginary parts)

Simple to understand:


Copy the code

To be continued…