Python for Data analysis--NumPy

Source: Internet
Author: User

NumPy as the basis for the scientific calculation of Python, why Python is suitable for mathematical calculations, in addition to being easy to understand and easy to learn

Python can simply invoke a large number of legacy libraries written in C and Fortran.

The NumPy ndarray:a multidimensional Array Object

Ndarray, which can be understood as an n-dimensional array, used for abstract matrices and vectors

Creating ndarrays

The simplest is to initialize from list,

Of course there are other ways, like,

Summary

Data Types for Ndarrays

First, for Ndarray, you can only store the same type of data

And because it encapsulates the C and Fortran libraries, the type must be uniform, so ndarrays supports the following types,
Then the types in each language will be uniformly aligned to these types

Ndarray support for explicit type conversion (copy)

int turn to float:

String to float:

This is useful, and you can see that the type written here Float,numpy automatically converts the Python type to the Ndarray supported type

Operations between Arrays and scalars

For Ndarray inside the elems operation, do not need to write for the
By default, the operation between Ndarray of the same ndarray or shape will traverse each element, called vectorization, to quantify

Basic indexing and slicing (View)

Take out some of the data in the matrix, or cut out the sub-matrices

For a one-dimensional vector, and the Python list operation is basically consistent, the biggest difference is that the Ndarray slicing will not copy, but the view, that is, you change the slicing, is equivalent to change the original data


You can see that the change Arr_slice also affects arr, because copy is inefficient due to the large matrix that is often manipulated, so the default is no copy
Of course you can explicitly copy,arr[5:8].copy ()

Two-dimensional, reference,

Boolean indexing

This comparison has characteristics,
For normal index,arr[2], here is the second specified index
What if I want to index multiple, and not continuous?

You can actually use a Boolean indexing to one by one to specify if you need to remove

For example, Arr[[true, False, True,false]]

A Boolean indexing that does not normally have manual parameters can be judged by some conditions

Then you take the Boolean indexing into arr and you can remove the dimension data labeled True.


Therefore, it is important to ensure that the dimensions of the Boolean indexing are the same as the corresponding dimensions in the matrix.
Like what
Arr[[true, False, True,false]]
Arr must be guaranteed to be 4 rows.

In fact, each dimension can be combined with conditional filtering, for example, row selection names==bobs, column 3rd column

In addition to selecting at the dimension level, each elem can be filtered
For example, the data, all less than 0, are set to 0

Fancy indexing

Fancy indexing is a term adopted by NumPy to describe indexing using integer arrays.
Unlike normal indexing, arr[3], the difference is that multiple can be specified and returned in the order specified

Select Line 4,3,0,6

Notice the difference between the two below,
Equivalent, fetch (1, 0), (5, 3), (7, 1), and (2, 2)


Row, select 1,5,7,2
Columns, select all, Change Order

Transposing Arrays and swapping Axes

Transpose, transposing

Arr. T

In fact, the transpose is a special version of Swapaxes, which can specify the two dimensions of swap

Arr.swapaxes (0, 1)

Universal functions:fast element-wise Array Functions

This has been introduced in the basic Ndarray calculation, here a summary of
This vectorized operation is divided into one yuan and two yuan,

Data processing Using Arrays

Expressing Conditional Logic as Array Operations

Vectorized can be used to simplify a for loop, can if-else be simplified?

Numpy.where function is a vectorized version of the ternary expression x if condition else y

Np.where (Cond, Xarr, Yarr) equivalent to cond? Xarr:yarr

And, this can also be nested, that is, if if...elseif.....elseif......else ...

Mathematical and statistical Methods

Methods for Boolean Arrays

Any and all

Unique and other Set Logic

File Input and Output with Arrays

Storing Arrays on Disk in Binary Format (. npy)

Compress the store and specify the alias (. Npz)

Saving and Loading Text Files

Linear Algebra

Random number Generation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.