NumPy - Array Creation using Existing Data

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

1. numpy.asarray

It Convert the input to an array. It is useful for converting Python sequence into ndarray..

Syntax: 
numpy.asarray(a, dtype = None, order = None)

Where,
a -  Input data, in any form that can be converted to an array. This includes lists,
     lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.
dtype  - By default, the data-type is inferred from the input data.
order - C (row major) or F (column major). C is default  

Example 1:

# convert list to ndarray 
import numpy as np 

lst = [1,2,3,4,5] 
print(np.asarray(lst))

Output:
[1 2 3 4 5]

Example 2:

# convert tuple to ndarray
import numpy as np 

tple = (1,2,3,4,5) 
print(np.asarray(tple))

Output:
[1 2 3 4 5]

Example 3:

# ndarray from list of tuples 
import numpy as np 

x = [(1,2,3),(4,5,6)] 
print(np.asarray(x))

Output:
[[1 2 3]
 [4 5 6]]

Example 4:

# dtype is set as float
import numpy as np 

x = [(1,2,3),(4,5,6)] 
print(np.asarray(x, dtype = float))

Output:
[[1. 2. 3.]
 [4. 5. 6.]]

2. numpy.copy

Return an array copy of the given object.

Syntax:
numpy.copy(a)

Where,
a - Input data

Example 1:

#Create an array a and copy to b:
import numpy as np 

a = np.array([1, 2, 3, 4, 5])
b = np.copy(a)

print("Original Array",a)
print("Copy of an Array",b)

Output:
Original Array [1 2 3 4 5]
Copy of an Array [1 2 3 4 5]

3. numpy.frombuffer

Interpret a buffer as a 1-dimensional array.

Syntax: 
numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)

Where,
buffer - An object that exposes the buffer interface.
dtype  - Data-type of the returned array; default: float.
count  - Number of items to read. -1 means all data in the buffer
offset - Start re.ding the buffer from this offset (in bytes); default: 0.

Example 1:

import numpy as np 
s =  b'Hello' 
print(np.frombuffer(s, dtype = 'S1'))

Output:
[b'H' b'e' b'l' b'l' b'o']

4. numpy.fromiter

Create a new 1-dimensional array from an iterable object.

Syntax:
numpy.fromiter(iterable, dtype, count=-1)

Where,
iterable - An iterable object providing data for the array.
dtype - The data-type of the returned array.
count - The number of items to read from iterable. 
        The default is -1, which means all data is read

Example 1:

# code to create array using list 
import numpy as np 

iterable = (range(10))
print(np.fromiter(iterable, int))

Output:
[0 1 2 3 4 5 6 7 8 9]

Example 2:

# code to create array for square of mentioned range
import numpy as np 

iterable = (x*x for x in range(10))
print(np.fromiter(iterable, float))

Output:
[ 0.  1.  4.  9. 16. 25. 36. 49. 64. 81.]

5. numpy.fromstring

A new 1-D array initialized from text data in a string

Syntax:
numpy.fromstring(string, dtype=float, count=-1, sep='')

Where,
string  - A string containing the data
dtype   - The data type of the array; default: float
count   - Read this number of dtype elements from the data. 
          If this is negative (the default), the count will be determined from 
          the length of the data.
sep     - The string separating numbers in the data; 
          extra whitespace between elements is also ignored

Example 1:

# Code to create array from string 
import numpy as np 
strVal =  '1 2 3 4 5'
print(np.fromstring(strVal, dtype=int, sep=' '))

Output:
[1 2 3 4 5]

Example 2:

# Code to create array from string 
import numpy as np 
strVal =  '1234 2343 3223'
print(np.fromstring(strVal, dtype=int, sep=' '))

Output:
[1234 2343 3223]
Total Website Visits: 40436
© Copyright 2018 - 2020. All Rights Reserved.

Developed by Vinoth Rathinam