Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

NumPy Create Arrays from Numeric Ranges

NumPy Create Arrays from Numeric Ranges

In this chapter we will learn how to create arrays from numeric ranges.

numpy.arange

The arange function in the numpy package is used to create numeric ranges and return an ndarray object. The function format is as follows:

numpy.arange(start, stop, step, dtype)

Generate an ndarray according to the range specified by start and stop and the step set by step.

Parameter Description:

Parameters Description
start Start value, defaults to0
stop End value (not included)
step Step, defaults to1
dtype Returnndarraydata type; if not provided, the type of the input data will be used.

Example

Generate an array from 0 to 4 with length 5:

Example

import

numpy

as

np

x

=

np

.

arange

(

5

)

print

(

x

)

The output is as follows:

[0  1  2  3  4]

Set the return type to float:

Example

import

numpy

as

np

Set dtype

x

=

np

.

arange

(

5

,

dtype

=

float

)

print

(

x

)

The output is as follows:

[0.  1.  2.  3.  4.]

Set the start value, end value, and step:

Example

import

numpy

as

np

x

=

np

.

arange

(

10

,

20

,

2

)

print

(

x

)

The output is as follows:

[10  12  14  16  18]

numpy.linspace

The numpy.linspace function is used to create a one-dimensional array consisting of an arithmetic sequence. The format is as follows:

np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

Parameter Description:

Parameters Description
start The start value of the sequence
stop The end value of the sequence; ifendpointistrue, this value is included in the sequence
num The number of equally spaced samples to generate, defaults to50
endpoint When this value is true , the sequence includes thestopvalue; otherwise it is not included. The default is True.
retstep If True, the spacing is displayed in the generated array; otherwise it is not.
dtype ndarray data type

The following example uses three parameters, setting the start point to 1, the end point to 10, and the number of elements to 10.

Example

import

numpy

as

np

a

=

np

.

linspace

(

1

,

10

,

10

)

print

(

a

)

The output is:

[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

Set an arithmetic sequence where all elements are 1:

Example

import

numpy

as

np

a

=

np

.

linspace

(

1

,

1

,

10

)

print

(

a

)

The output is:

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Set endpoint to false, not including the end value:

Example

import

numpy

as

np

a

=

np

.

linspace

(

10

,

20

,

5

,

endpoint

=

False

)

print

(

a

)

The output is:

[10. 12. 14. 16. 18.]

If endpoint is set to true, 20 will be included.

The following example sets the spacing.

Example

import

numpy

as

np

a

=

np

.

linspace

(

1

,

10

,

10

,

retstep

=

True

)

print

(

a

)

Extended example

b

=

np

.

linspace

(

1

,

10

,

10

)

.

reshape

(

[

10

,

1

]

)

print

(

b

)

The output is:

(array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]), 1.0)

[[ 1.]

 [ 2.]

 [ 3.]

 [ 4.]

 [ 5.]

 [ 6.]

 [ 7.]

 [ 8.]

 [ 9.]

 [10.]]

numpy.logspace

The numpy.logspace function is used to create a geometric sequence. The format is as follows:

np.logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None)

The base parameter means the subscript of log when taking the logarithm.

Parameters Description
start The start value of the sequence is: base ** start
stop The end value of the sequence is: base ** stop. Ifendpointistrue, this value is included in the sequence
num The number of equally spaced samples to generate, defaults to50
endpoint When this value is true , the sequence includes thestopvalue; otherwise it is not included. The default is True.
base The base of the logarithm log.
dtype ndarray data type

Example

import

numpy

as

np

The default base is 10

a

=

np

.

logspace

(

1.0

,

2.0

,

num

=

10

)

print

(

a

)

The output is:

[ 10.           12.91549665     16.68100537      21.5443469  27.82559402      

  35.93813664   46.41588834     59.94842503      77.42636827    100.    ]

Set the base of the logarithm to 2:

Example

import

numpy

as

np

a

=

np

.

logspace

(

0

,

9

,

10

,

base

=

2

)

print

(

a

)

The output is as follows:

[  1.   2.   4.   8.  16.  32.  64. 128. 256. 512.]