0% found this document useful (0 votes)
6 views6 pages

NumPy Array Creation Functions Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

NumPy Array Creation Functions Explained

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NumPy

[Link]
This function interprets a buffer as one-dimensional array. Any object that exposes the buffer
interface is used as parameter to return an ndarray.

[Link](buffer, dtype=float, count=-1, offset=0)

The constructor takes the following parameters.

buffer Any object that exposes buffer interface


dtype Data type of returned ndarray. Defaults to float
count The number of items to read, default -1 means all data
offset The starting position to read from. Default is 0

Example 5
The following examples demonstrate the use of frombuffer function.

import numpy as np
s = 'Hello World'
a = [Link](s, dtype='S1')
print a

Here is its output:

['H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd']

[Link]
This function builds an ndarray object from any iterable object. A new one-dimensional array
is returned by this function.

[Link](iterable, dtype, count=-1)

Here, the constructor takes the following parameters.

iterable Any iterable object

dtype
Data type of resultant array

28
NumPy

The number of items to be read from iterator. Default is -1 which means all data
count
to be read

The following examples show how to use the built-in range() function to return a list object.
An iterator of this list is used to form an ndarray object.

Example 6
# create list object using range function
import numpy as np
list = range(5)
print list

Its output is as follows:

[0, 1, 2, 3, 4]

Example 7
# obtain iterator object from list
import numpy as np
list = range(5)
it = iter(list)

# use iterator to create ndarray


x = [Link](it, dtype=float)
print x

Now, the output would be as follows:

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

29
8. NUMPY − ARRAY FROM NUMERICAL RANGES NumPy

In this chapter, we will see how to create an array from numerical ranges.

[Link]
This function returns an ndarray object containing evenly spaced values within a given range.
The format of the function is as follows:

[Link](start, stop, step, dtype)

The constructor takes the following parameters.

start The start of an interval. If omitted, defaults to 0

stop The end of an interval (not including this number)

step Spacing between values, default is 1

dtype Data type of resulting ndarray. If not given, data type of input is used

The following examples show how you can use this function.

Example 1
import numpy as np
x = [Link](5)
print x

Its output would be as follows:

[0 1 2 3 4]

30
NumPy

Example 2
import numpy as np
# dtype set
x = [Link](5, dtype=float)
print x

Here, the output would be:

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

Example 3
# start and stop parameters set
import numpy as np
x = [Link](10,20,2)
print x

Its output is as follows:

[10 12 14 16 18]

[Link]
This function is similar to arange() function. In this function, instead of step size, the number
of evenly spaced values between the interval is specified. The usage of this function is as
follows:

[Link](start, stop, num, endpoint, retstep, dtype)

The constructor takes the following parameters.

start The starting value of the sequence

stop The end value of the sequence, included in the sequence if endpoint set to true

31
NumPy

num The number of evenly spaced samples to be generated. Default is 50

True by default, hence the stop value is included in the sequence. If false, it is not
endpoint
included

retstep If true, returns samples and step between the consecutive numbers

dtype Data type of output ndarray

32
NumPy

End of ebook preview


If you liked what you saw…
Buy it from our store @ [Link]

33

You might also like