Numpy learning path (1) -- array creation, numpy path

Source: Internet
Author: User

Numpy learning path (1) -- array creation, numpy path

Array is the main object for Numpy operations and the main object for python data analysis. This series of articles is my note in Numpy learning.

The following numpy import methods are used in this article:

import numpy as npfrom numpy import *

1. Create a normal array -- np. arange (), np. array (),

(1) arange () is a sequential array. The function prototype is arange ([start,] stop [, step], dtype = None)

If the start parameter is omitted, it indicates that it starts from 0. The default dtype is float32.

# Create a one-dimensional array ar_1ar_1 = np from 0 to 19. arange (20) # output: ar_1 = array ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]) # create a one-dimensional array of 11-20, step = 2, ar_2ar_2 = np. arange (11,21, 2) # output: ar_2 = array ([11, 13, 15, 17, 19])

(2) array () is mainly used to create multi-dimensional arrays. The prototype is array (object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)

  • Object: it is an array_like object, including list, tuple, etc. The _ array _ custom class can return the object of the class array.
  • Dtype: Specifies the data type. The default value is the minimum data type that can be saved.
  • You can use help to view other parameters.
# Create a 2*3 array with the Data Type int32ary_1 = np. array ([[2, 2, 3], [5, 7, 3], dtype = int32) # output: ary_1 = ([2, 2, 3], # [5, 7, 3], dtype = int32) # create an array of 2*3*3. The default data type is ary_2 = np. array ([[[3.3, 5], [4.2, 4.2, 1.3], [2.2, 5.8, 2.3], [[1.9, 5.7, 4.5, 6.7, 9.7], [2.2, 1.2, 7.99]) # output: # array ([[3 ., 4 ., 5.], # [3.3, 4.2, 4.2], # [1.3, 2.2, 5.8], # [2.3, 1.9, 5.7], # [4.5, 6.7, 9.7], # [2.2, 1.2, 7.99]) ary_2.dtype # output: dtype ('float64 '),
# Although it is saved by int and float, array is the minimum data type that can save the data, so it is float32
# Of course, you can also change the array dimension through reshape and other methods to obtain the array you need
Ary_3 = np. arange (20, 30). reshape (2, 5)
# Output: ary_3
# Array ([20, 21, 22, 23, 24],
# [25, 26, 27, 28, 29])

2. Create a special array:

(1) empty array: empty (), empty_like ()

  • Empty (shape [, dtype = None, order =]), create an empty array of shape, dtype is data type, order is sequential form: C (C) -row-major; F (Fortran) column-major.
  • Empty_like (array) returns a new empty array based on the shape and type of the given array (.
# Create an empty 3*3 array: e_1 = np. empty ([3, 3]) # In [89]: print e_1 #[2017-077 2.68678134e + 154 4.44659081e-323] # [0.20.0000e + 000 0.20.0000e + 000 0.20.0000e + 000] # [0.20.0000e + 000 0.20.0000e + 000 0.20.0000e + 000] # The filled values are random # Use empty_like to create an empty array e_2 = np in the same shape as e_1. empty_like (e_1) # In [93]: print e_2 #[[1.72723371e-077 1.72723371e-077 2.00299617e-313] #[1.72723371e-077 bytes-323 bytes-313] # [0.20.0000e + 000 bytes-309 0.20.0000e + 000]

(2) create other special Arrays: eye, ones, and zeros have similar structures. In addition, an identity function is used to create square arrays.

  • Eye [N, [, M, k, dtype]), N is the number of rows, M is the number of columns (if not set to N by default), diagonal serial number: 0 corresponds to the primary diagonal ;, the integer corresponds to upper diagonal, and the negative number corresponds to lower diagonal;
  • Eye_like (array): creates an array with a diagonal line of 1 in the same shape as array.
# Create a matrix of 3x3 with the primary diagonal line 1: ey_1 = np. eye (3, 3, k = 0) print ey_1 #[1. 0. 0.] #[0. 1. 0.] #[0. 0. 1.] # The diagonal line of ey_2 uper a position (ROW) ey_2 = np. eye (3, 3, k = 1) pringt ey_2 #[0. 1. 0.] #[0. 0. 1.] #[0. 0. 0.]
  • Ones (shape [, dtpe =, order]): returns an array of elements 1 in shape according to the given shape.
  • Ones_like (a): returns an array of 1 elements in the same shape as.
one_1=np.ones([5,9])print one_1#In [99]: print one_1#[[ 1.  1.  1.  1.  1.  1.  1.  1.  1.]#[ 1.  1.  1.  1.  1.  1.  1.  1.  1.]#[ 1.  1.  1.  1.  1.  1.  1.  1.  1.]#[ 1.  1.  1.  1.  1.  1.  1.  1.  1.]#[ 1.  1.  1.  1.  1.  1.  1.  1.  1.]]
  • Zeros (shape [, dtype, order]): returns an array of 0 elements in shape according to the given shape.
  • Zeros_like (a): returns an array with 0 elements in the same shape as.
zero_1=np.zeros([2,3])print zero_1#[[ 0.  0.  0.]#[ 0.  0.  0.]]
  • Identity (n [, dtype =]) returns an n-dimensional square matrix.
In [103]: idenOut[103]:array([[ 1.,  0.,  0.,  0.,  0.,  0.,  0.],       [ 0.,  1.,  0.,  0.,  0.,  0.,  0.],       [ 0.,  0.,  1.,  0.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  1.,  0.,  0.,  0.],       [ 0.,  0.,  0.,  0.,  1.,  0.,  0.],       [ 0.,  0.,  0.,  0.,  0.,  1.,  0.],       [ 0.,  0.,  0.,  0.,  0.,  0.,  1.]])

3. struct Array

Generally, the elements stored in an array are of the same source, that is, all elements in the array must be of the same type. In actual data analysis, especially in two-dimensional spreadsheet format, data in the same row is composed of different types. This requires defining a unique dtype for such data. Dtype is actually a class. You can assign a parameter to define a special struct array type. (I think it is a bit similar to the sort in SAS)

# Define a dtype named "person". dtype # a peson consists of name, age, and weight to form a person = np. dtype ([('name', str, 20), ('age', int32), ('weight', float32)]) print person # [('name ', 's20'), ('age', '<i4'), ('weight',' <f4 ')] # You can create an array whose dtype is person. student = array ([('cnblog ', 10, 12.2), ('myblog',)], dtype = person) # Because the type specifies the number of parameters, you need to use tuple to create the number of rows of the array (because it cannot be changed). Otherwise, there may be a readable predictionprint student # [('cnblog ', 10, 12.199999809265137) ('myblog', 40, 30.0)]

4. Create a file (this will be introduced later)

 

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.