Python's NumPy tutorial (i): Basic Concepts __python

Source: Internet
Author: User

First to install the NumPy package, specific installation please find another tutorial


1. Loading :

Import NumPy as NP

2. EstablishA simple data

data = [[0.9526, -0.246, -0.8856],[0.5639,  0.2379,  0.9104]]

Enter data to run the result:

[[0.9526,-0.246,-0.8856], [0.5639, 0.2379, 0.9104]]

3. convert data to an array object (Ndarray)

data = Np.array (data)

Run the data again and see what it's become.

Array ([[0.9526, -0.246, -0.8856],
       [0.5639,  0.2379,  0.9104]])


4. Yes, this is a multi-dimensional array pattern that can be easily computed , such as

Data*10
Output:

Array ([[0.9526, -0.246, -0.8856],
       [0.5639,  0.2379,  0.9104]])

Another example

Data + data
Output:

Array ([[1.9052, -0.492, -1.7712],
       [1.1278,  0.4758,  1.8208]])

5. Understand what is Ndarray, and then look at its basic syntax, and then build two data

Data1 = [6,7.5,8,0,1]
arr1 = Np.array (data1)
data2 = [[1,2,3,4],[5,6,7,8]]
arr2 = Np.array (data2)

For large data, it is not possible to go one line to count the attributes of the data, such as rows, columns, and so on, so you need a simple syntax to give the result directly.

Look at the dimensions of the array, that is, the number of axes of the array, mathematics called the rank of the matrix, with the Ndim statement

Arr2.ndim
Output:

2


6. View the row and column information of the array, using the shape statement

Arr2.shape
Output:

(2, 4)

7.np.array will attempt to infer a more appropriate data type for the newly created array, which will be stored in a special Dtype object. Like the two data just now:

Arr1.dtype
Output:

Dtype (' float64 ')

Arr2.dtype
Output:

Dtype (' int32 ')

8. Thezeros and ones functions can create an array of all 0 or all 1 of the specified length or shape,empty can create an array without any specific values.

Np.zeros (10)
Output:

Array ([0.,  0.,  0.,  0., 0., 0., 0., 0., 0.,  0.)

Np.ones ((3,6))
Output:

Array ([[1.,  1.,  1.,  1.,  1.,  1.],
       [1  ., 1., 1., 1., 1., 1.],
       [1.,  1.,  1.,  1.,  1.,  1.]]
Np.empty ((2,3,2))
Output:
Array ([[  1.01283457e-321,   0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000]
        , [  0.00000000e+000,   0.00000000e+000]], [

       [  0.00000000e+000,   0.00000000e+000],  [ 0.00000000e+000,   0.00000000e+000],
        [  0.00000000e+000,   0.00000000e+000]]]


9.arrange is an array version of the Python built-in function range

Np.arange (15)
Output:

Array ([0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14])

Summarize 1~9:


10.ndarray Data type dtype:

arr1 = Np.array ([1,2,3],dtype=np.float64)
arr2 = Np.array ([1,2,3],dtype=np.int32)
Arr1.dtype
Arr2.dtype

Output:

Dtype (' float64 ')
Dtype (' int32 ')

The data type of the NumPy is shown in figure:




11. Data type conversion using astype

arr = Np.array ([1,2,3,4,5])
arr.dtype
Float_arr = Arr.astype (np.float64)
Float_arr.dtype

Output:

Dtype (' int32 ')
Dtype (' float64 ')

This example converts an integer to a floating-point number, and if the floating-point numbers are converted to integers, the decimal branch is truncated . Look at the following example:


arr = Np.array ([3.7,-1.2,-2.6,0.5,12.9,10.1])
arr
Output:

Array ([  3.7,  -1.2,  -2.6,   0.5,  12.9,  10.1])

Arr.astype (Np.int32)
Output:

Array ([3,-1,-2,  0, 12, 10])

can also put converts a string to a numeric type(Note that _ behind the character string):

Numeric_strings = Np.array ([' 1.25 ', ' -9.6 ', ' numeric_strings '],dtype=np.string_)
Output:

Array ([B ' 1.25 ', b ' -9.6 ', B ' []], 
      dtype= ' | S4 ')
Numeric_strings.astype (float)
Output:

Array ([  1.25,  -9.6, the  .  ])

12. Assigning the type of a set of data to another set of data

Int_array = Np.arange (10)
calibers = Np.array ([. 22,.270,.357,.380,.44,.50],dtype=np.float)
Int_array.astype (Calibers.dtype)
Output:

Array ([0.,  1.,  2.,  3., 4., 5., 6., 7., 8.,  9.)


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.