0% found this document useful (0 votes)
5 views7 pages

NumPy Array Creation Methods 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)
5 views7 pages

NumPy Array Creation Methods 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

6.

NUMPY − ARRAY CREATION ROUTINES NumPy

A new ndarray object can be constructed by any of the following array creation routines or
using a low-level ndarray constructor.

[Link]
It creates an uninitialized array of specified shape and dtype. It uses the following constructor:

[Link](shape, dtype=float, order='C')

The constructor takes the following parameters.

Shape Shape of an empty array in int or tuple of int

Dtype Desired output data type. Optional

Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 1
The following code shows an example of an empty array.

import numpy as np
x = [Link]([3,2], dtype=int)
print x

The output is as follows:

[[22649312 1701344351]
[1818321759 1885959276]
[16779776 156368896]]

Note: The elements in an array show random values as they are not initialized.

22
NumPy

[Link]
Returns a new array of specified size, filled with zeros.

[Link](shape, dtype=float, order='C')

The constructor takes the following parameters.

Shape Shape of an empty array in int or sequence of int

Dtype Desired output data type. Optional

Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 2
# array of five zeros. Default dtype is float
import numpy as np
x = [Link](5)
print x

The output is as follows:

[ 0. 0. 0. 0. 0.]

Example 3
import numpy as np
x = [Link]((5,), dtype=[Link])
print x

Now, the output would be as follows:

[0 0 0 0 0]

23
NumPy

Example 4
# custom type
import numpy as np
x = [Link]((2,2), dtype=[('x', 'i4'), ('y', 'i4')])
print x

It should produce the following output:

[[(0, 0) (0, 0)]


[(0, 0) (0, 0)]]

[Link]
Returns a new array of specified size and type, filled with ones.

[Link](shape, dtype=None, order='C')

The constructor takes the following parameters.

Shape Shape of an empty array in int or tuple of int

Dtype Desired output data type. Optional

Order 'C' for C-style row-major array, 'F' for FORTRAN style column-major array

Example 5
# array of five ones. Default dtype is float
import numpy as np
x = [Link](5)
print x

The output is as follows:


24
NumPy

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

Example 6
import numpy as np
x = [Link]([2,2], dtype=int)
print x

Now, the output would be as follows:

[[1 1]
[1 1]]

25
7. NUMPY − ARRAY FROM EXISTING DATA NumPy

In this chapter, we will discuss how to create an array from existing data.

[Link]
This function is similar to [Link] except for the fact that it has fewer parameters. This
routine is useful for converting Python sequence into ndarray.

[Link](a, dtype=None, order=None)

The constructor takes the following parameters.

Input data in any form such as list, list of tuples, tuples, tuple of tuples or tuple
a
of lists

dtype By default, the data type of input data is applied to the resultant ndarray

order C (row major) or F (column major). C is default

The following examples show how you can use the asarray function.

Example 1
# convert list to ndarray
import numpy as np
x = [1,2,3]
a = [Link](x)
print a

Its output would be as follows:

[1 2 3]

26
NumPy

Example 2
# dtype is set
import numpy as np
x = [1,2,3]
a = [Link](x, dtype=float)
print a

Now, the output would be as follows:

[ 1. 2. 3.]

Example 3
# ndarray from tuple
import numpy as np
x = (1,2,3)
a = [Link](x)
print a

Its output would be:

[1 2 3]

Example 4
# ndarray from list of tuples
import numpy as np
x = [(1,2,3),(4,5)]
a = [Link](x)
print a

Here, the output would be as follows:

[(1, 2, 3) (4, 5)]

27
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

You might also like