1. CREATING NUMPY ARRAYS
Dr. B. Subashini
Assistant Professor of Computer Applications,
V.V.Vanniaperumal College for Women,
Virudhunagar.
2. CREATING NUMPY ARRAYS
np.array is a function that allow users to create an array of element
supplied as an argument.
Numpy array() function takes a list of elements as argument and
returns a one-dimensional array
EX:
import numpy as np
array1 = np.array([1, 3, 5])
print("np.array():n", array1)
OUTPUT:
np.array():
[1 3 5]
3. NUMPY.ARRANGE
The arange([start,] stop[, step,][, dtype]) : Returns an array with evenly
spaced elements as per the interval.
EX:
import numpy as np
print("An", np.arange(4, 20, 3), "n")
OUTPUT:
A
[ 4 7 10 13 16 19]
4. NUMPY.LINSPACE
The numpy.linspace() function returns number spaces evenly w.r.t
interval. Similar to numpy.arange() function but instead of step it uses
sample number.
EX:
import numpy as np
a=np.linspace(1,2/3,4)
print(a)
OUTPUT:
[1 0.916666 0.83333333 0.75]
5. ARRAY CRATION USING LIST
Arrays are used to store multiple values in one single variable.Python
does not have built-in support for Arrays, but Python lists can be used
instead.
Example :
import numpy as np
l= [1, 2, 3, 4, 5]
arr=np.array(l)
Print(arr)
OUTPUT:
[1 2 3 4 5]
6. ARRAY CRATION USING TUPLE
Array values are stored in rounded brackets
Example :
import numpy as np
l= (1, 2, 3, 4, 5)
arr=np.array(l)
Print(arr)
OUTPUT:
[1 2 3 4 5]
7. ARRAY FUNCTIONS
NP.ONES:
creates an array filled with ones of the specified shape
Example:
import numpy as np
array3 = np.ones((2, 4))
print("nnp.ones():n", array3)
Output:
np.ones():
[[1. 1. 1. 1.]
[1. 1. 1. 1.]]
8. ARRAY FUNCTIONS
NP.ZEROS:
creates an array filled with zeros of the specified shape
Example:
import numpy as np
array3 = np.zeros((2, 4))
print("nnp.zeros():n", array3)
Output:
np.zeros():
[[0. 0. 0. 0.]
[0. 0. 0. 0.]]
9. ARRAY FUNCTIONS
NP.FULL:
creates an array filled with specified value of the specified shape
Example:
import numpy as np
array3 = np.full((2, 4),2)
print("nnp.full():n", array3)
Output:
np.full():
[[2. 2. 2. 2.]
[2. 2. 2. 2.]]
10. ARRAY FUNCTIONS
NUMPY.EMPTY:
The numpy.empty() function is used to create a new array of given
shape and type, without initializing entries. It is typically used for
large arrays when performance is critical, and the values will be
filled in later
Example:
import numpy as np
np.empty(2)
Output:
array([ 6.95033087e-310, 1.69970835e-316])
11. ARRAY FUNCTIONS
NP.RANDOM.RAND:
creates an array filled with random value of the specified shape
Example:
from numpy import random
import numpy as np
x = np.random.rand(3,2)
print(x)
Output:
[[0.19488027 0.27812108]
[0.89937813 0.20219224]
[0.21488034 0.77359958]]