NumPy- generation ndarray

NumPy ndarray: multi-dimensional array object

One of the core features of NumPy N- dimensional array is an object --ndarray.

ndarray is a fast, flexible container of large data sets in Python. Digital allows you to use syntax similar to the operation of scalar mathematical calculations on a single piece of data.

First of all we want to import NumPy module

import numpy as np

Generated ndarray

Generating an array of objects The easiest way is to use the array function. array function receives any type target sequence (of course, also include other array), to generate a new array containing NumPy transferring data.

Generate a one-dimensional array:

data1 = [6,7.5,8,0,1]
arr1 = np.array(data1)
print(arr1)

[6. 7.5 8. 0. 1. ]

Generate a two-dimensional array:

data2 = [[1,2,3,4],[5,6,7,8]]
arr2 = np.array(data2)
print(arr2)
print(arr2.ndim)
print(arr2.shape)

[[1 2 3 4]
[5 6 7 8]]
2
(2, 4)

You 与 shape

These two properties are used to represent an array.

ndim this array is used to represent several dimensions

This shape is used to represent a few of several multiply array

dtype

Unless explicitly specified otherwise, it np.array automatically infer the data type of array generated. For example, if your array has a floating-point number is the case, then the data will automatically be inferred to float array, and the type of data will be stored in a special metadata dtype in.

print(arr1.dtype)
print(arr2.dtype)

float64
int32

numpy array generating function:

Function Name | Description

Function name description
array The input data (which may be a list of tuples, arrays, and other sequences) into ndarray, if not explicitly specified data type obtained automatically infer; default copy all of the input data
asarray The input into ndarray, but if the input is already ndarray no longer replicate, the difference between him and the array is that when the data source is ndarray object, array will copy a copy, take up a new memory, but not asarray
arange Python built-in function range of the array plate, returning an array such as [1,2,3,4 .....]
ones The whole array generates a given data type and shape
ones_like Generating a different shape according to the full array of arrays 1
zeros According to generate a full array of given shape and 0 data types
zeros_like According to generate an array of the same shape as a whole array 0
empty According to generate a given shape is not empty array initialization values
empty_like The generated array according to a shape of the same but the values ​​are not initialized empty array
full Generating a specified value according to a given shape and array type array np.full (shape, fill value, dtype = data type)
full_like A shape generated according to the same array, but the array contents are specified value, only the shape parameters into the object can be a ndarray
Wherein generating a N * N matrix, all diagonal positions of one and the other are 0, the difference between them is that the former can control the shape of the matrix, which can be a square matrix

Guess you like

Origin www.cnblogs.com/chanyuli/p/11716932.html