1 Creating an array
Arange function: Specify initial, final, and step steps to create an array
Import numpy>>> numpy.arange (0,1,0.1) array ([0., 0.1, 0.2, 0.3, 0.4, 0.5 , 0.6, 0.7, 0.8, 0.9])
Linspace function: Create a one-dimensional array by specifying a start value, a final value, and the number of elements, you can specify whether to include the final value by the Endpoint keyword, and the default setting is to include the final value
>>> numpy.linspace (0,1,10) array ([0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444, 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ])
The Logspace function is similar to Linspace, but it creates geometric series, and the following example generates 1 (10^0) to 100 (10^2) and 20 elements of geometric series
>>> numpy.logspace (1,2,20) Array ([ in. , 11.28837892, 12.74274986, 14.38449888, 16.23776739, 18.32980711, 20.69138081, 23.35721469, 26.36650899, 29.76351442, 33.59818286, 37.92690191, 42.81332399, 48.32930239, 54.55594781, 61.58482111, 69.51927962, 78.47599704, 88.58667904 . ])
In addition, functions such as Frombuffer, FromString, FromFile, and so on can be used to create an array from a sequence of bytes, with FromString as an example:
>>> s='abcdefg'>>> numpy.fromstring (s,dtype=97, 98, 101, 102, 103], dtype=int8)
The first parameter of the FromFunction function is the function that evaluates each array element, the second parameter is the size of the group (shape), because it supports multidimensional arrays, so the second argument must be a sequence
In this example, a two-dimensional array is created to represent the 99 multiplication table:
Import NumPy def Func2 (i,j): return (i+1) * (j+1) a=numpy.fromfunction (Func2, (9,9))
Python Create array