All different ways of Creating Numpy Arrays!
All you need to know About Numpy
import numpy as np
Hello Guys👋
In this article, we will see all the different ways of creating Numpy Arrays.
So, Without much ado, Let's get started with the article👍.
- Numpy Arrays vs Python Lists
- Creating Numpy Arrays from Lists
- Creating Numpy Constant Arrays
- Creating Numpy Sequence Arrays
- Creating ndarrays and understanding their properties
- Conclusion
☝Numpy Arrays vs Python Lists 🤨:
The main difference difference is the time taken by them to perform operations. To measure this, we use %timeit function. N loops indicate that the function has been called N times to compute the average time to execute the function. Best of K indicates that the %timeit has been run K times to take the best average. These constants can also be modified and given as argument to the %timeit function.
Let's use %timeit and see the difference:
vec_size = 1000
def add_python_lists():
X = range(vec_size)
Y = range(vec_size)
Z = [X[i] + Y[i] for i in range(len(X))]
return Z
%timeit add_python_lists()
The output of the above %timeit function is : 1000 loops, best of 5: 260 µs per loop
vec_size = 1000
def add_numpy_arrays():
X = np.arange(vec_size)
Y = np.arange(vec_size)
Z = X + Y
return Z
%timeit add_numpy_arrays()
The output of the above %timeit function is : The slowest run took 1698.30 times longer than the fastest. This could mean that an intermediate result is being cached. 100000 loops, best of 5: 3.85 µs per loop
So, we can clearly see that to add two lists of same size, Python takes 260µs per loop whereas Numpy takes only 3.85µs per loop🙂.
☝☝Creating Numpy Arrays from Lists🙃
Numpy arrays can be created using a list with numpy.array
All elements of a numpy array should be of the same type
numpy.array
chooses a data type which fits all of the elements in the array- We can also specify the data type with the
dtype
parameter
Let's create a numpy array.
int_array = np.array([1, 5, 7, 9, -2], dtype=np.int16)
print("int_array:", int_array)
typecasted_int_array = np.array([1, 5, 7, 9, -2.54], dtype=np.int64)
print("typecasted_int_array:", typecasted_int_array)
The above code prints
int_array: [ 1 5 7 9 -2]
typecasted_int_array: [ 1 5 7 9 -2]
Here, int_array is a numpy array of type int16. The typecasted_int_array is the numpy array of type int64. Here what we need to observe is, when we specified the dtype as int, 2.54 is typecasted as 2✨.
☝☝☝Creating Numpy Constant Arrays
➡ np.zeros(shape, dtype=float) - Returns a new array of given shape and type, filled with zeros.
zeros_5 = np.zeros(5)
print("zeros_5:", zeros_5)
zeros_7 = np.zeros(7, dtype=int)
print("zeros_7:", zeros_7)
zeros_2_3 = np.zeros((2,3), dtype=int)
print("zeros_2_3:", zeros_2_3)
The above code prints
zeros_5: [0. 0. 0. 0. 0.]
zeros_7: [0 0 0 0 0 0 0]
zeros_2_3: [[0 0 0]
[0 0 0]]
➡ np.ones(shape, dtype=float) - Returns a new array of given shape and type, filled with ones.
This functions similar to np.zeros. As the name suggests, the only difference is it returns an array filled with ones instead of zeroes.
➡ np.full(shape, fill_value, dtype=None) - Returns a new array of given shape and type, filled with fill_value. The default data type is derived from the fill_value
fill_int_array = np.full(10, fill_value=7.5, dtype=int)
print("fill_int_array:",fill_int_array)
fill_bool_array = np.full(6, fill_value=False)
print("fill_bool_array:",fill_bool_array)
The above code prints
fill_int_array: [7 7 7 7 7 7 7 7 7 7]
fill_bool_array: [False False False False False False]
So, here fill_int_array is an array of size 10, type int and filled with 7.5. There is no need to specify the dtype as it automatically derive that from the fill_value as shown in example 2. There we haven't specified any dtype, but the type of fill_bool_array is boolean.
☝☝☝☝Creating Numpy Sequence Arrays
➡ np.arange(start, stop, step, dtype=None) :
- Equivalent to the Python built-in range, but returns an ndarray rather than a list
- Values are generated within the half-open interval [start, stop)
- Much more efficient than np.array(range(_))
first_10_numbers = np.arange(10)
print("first_10_numbers:",first_10_numbers)
numbers_with_step = np.arange(5, 13, 2, dtype=float)
print("numbers_with_step:",numbers_with_step)
And the above code prints
first_10_numbers: [0 1 2 3 4 5 6 7 8 9]
numbers_with_step: [5. 7. 9. 11.]
Here numbers_with_step is an array containing numbers starting from 5 till 13 (excluding) with a step of 2, and of type float. We need not specify the start, step value, by default they are 0 as shown in first_10_numbers. It is an array of consecutive numbers from 0 to 10.
➡ np.linspace(start, stop, num, endpoint, retstep, dtype) :
Returns evenly spaced numbers, calculated over the interval [start, stop]. The number of evenly spaced numbers returned is equal to num
We Can control whether the last number stop can be included or excluded with the endpoint parameter
a = 4.0
b = 5.0
n = 6
sequential_array = np.linspace(a, b, num=n)
print("sequential_array:",sequential_array)
This code prints
sequential_array: [4. 4.2 4.4 4.6 4.8 5. ]
This returns an array of 6 numbers between 4 and 5 that are evenly spaced.
🖐Some more Methods of Creating Numpy Arrays
➡ np.empty(shape, dtype) - Creates an uninitialized empty array of the given shape. It might contain garbage values.
empty_array = np.empty(4, dtype=int)
print("empty_array:",empty_array)
The above code gives
empty_array: [4632796641680687104 4630263366890291200 4645547458125679821 4643121143826433966]
This may not give the same values always because they are garbage and will differ everytime we run.
➡ np.random.random(shape) - Creates an array of the given shape with random values in the interval [0,1]
random_valued_array = np.random.random(4)
print("random_valued_array:",random_valued_array)
This code prints
random_valued_array: [0.71697819 0.70451885 0.18953959 0.587543 ]
The values are not always same as the name suggests they are randomly generated.
➡ np.eye(num) - Returns the identity matrix of size num*num. Default data type is float.
identity_matrix = np.eye(7)
print(identity_matrix)
This prints an identity matrix of size 7*7.
[[1. 0. 0. 0. 0. 0. 0.]
[0. 1. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 1. 0.]
[0. 0. 0. 0. 0. 0. 1.]]
➡ np.diag(list of elements) - Returns a diagonal matrix with the list of elements as diagonal elements. The size is automatically derived from the list.
diagonal_matrix = np.diag((1, 2, 3, 5, 8, 10))
print(diagonal_matrix)
This code prints a diagonal matrix of size 6*6 with these elements as diagonal elements.
[[ 1 0 0 0 0 0]
[ 0 2 0 0 0 0]
[ 0 0 3 0 0 0]
[ 0 0 0 5 0 0]
[ 0 0 0 0 8 0]
[ 0 0 0 0 0 10]]
🖐☝Creating ndarrays & know its Properties
Lets create a 3darray and understand its properties.
array_2d = np.array([[1, -2, 0],
[3, 4, 2],
[3, 8, 9]])
print(array_2d)
The above code prints a 3*3 matrix as below.
[[ 1 -2 0]
[ 3 4 2]
[ 3 8 9]]
Properties of ndarray 😮😮
- size : Returns the total number of elements in the array
- ndim : Returns the no.of dimensions of the array
- shape : Returns the shape of array
Lets see an example.
array_2d = [[1 -2 0]
[3 4 2]
[3 8 9]]
print("size: ", array_2d.size)
print("ndim: ", array_2d.ndim)
print("shape: ", array_2d.shape)
The above code prints
size: 9
ndim: 2
shape: (3, 3)
🖐☝☝Conclusion
Finally, The methods we discussed in this article are
- np.array
- np.zeros, np.ones, np.full
- np.arrange, np.linespace
- np.empty, np.eye, np.diag
- np.random.random
- nd Arrays
What's Next ⏭?
Thank you for reading so far🙏. I hope you enjoyed following it and understood different ways of creating Numpy Arrays. I highly recommend you to play around with these methods.
In the next part, we will discuss the Indexing and Slicing of Numpy Arrays in detail !! So Stay Tuned👀👀!!!!
Do share your valuable suggestions and your honest feedback, also Constructive Criticism. If you have any comments, questions, or concerns, Do comment them and I am more than happy to take them💬.
Happy Learning 😊👍!!