Basics of NumPy Arrays

import numpy as np
# from list to array

list = [1,2,3,4]

arr = np.array(list)
arr

OUTPUT:

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

create numpy array with tuple

x = ("red","yellow","blue")
colors = np.array(x)
colors

OUTPUT:

array(['red', 'yellow', 'blue'], dtype='<U6')

create numpy array directly

x = np.array([1,2,3,4])
x

OUTPUT:

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

Type of ndarray

type(x)

OUTPUT:

numpy.ndarray

We can change the data type of the elements using dtype

np_heights = np.array([74, 75, 72, 72, 71])

np_heights.dtype

OUTPUT:

dtype('int32')

Change the data type to float

l_heights= (74, 75, 72, 72, 71)

np_heights = np.array(l_heights,dtype='float')

np_heights

OUTPUT:

array([74., 75., 72., 72., 71.])
np_heights.dtype

OUTPUT:

dtype('<U2')

Change the data type to strings

l_heights=(74, 75, 72, 72, 71)

np_heights = np.array(l_heights,dtype='str')

np_heights

OUTPUT:

array(['74', '75', '72', '72', '71'], dtype='<U2')

Notice that if you give different data types , it will convert it to string

import numpy as np
l_heights=(74.6, 75, "72", 72, 71)

np_heights = np.array(l_heights)

np_heights

OUTPUT:

array(['74.6', '75', '72', '72', '71'], dtype='<U32')

Multiple height (NumPy array) with a scalar.

np_heights = np.array([74, 75, 72, 72, 71])

np_heights + 20

OUTPUT:

array([94, 95, 92, 92, 91])

The following ways are commonly used when you know the size of the array beforehand:

np.ones(): Create array of 1s
np.zeros(): Create array of 0s

Creating a 1 D array of ones

[1,2,3,4,5,6,7,8]

[1,2,3
4,5,6
7,8,9]

arr = np.ones(10)
arr

OUTPUT:

array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
arr.shape ## 1 D array with 9 elements

OUTPUT:

(10,)

Notice that by default it creates float data type we can provide dtype explicitly using dtype

arr = np.ones(15,dtype='int')
arr

OUTPUT:

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
arr.dtype

OUTPUT:

dtype('int32')

Creating a 5 x 3 array of ones

arr1 = np.ones((5,3))
arr1

OUTPUT:

array([[1., 1., 1.],
      [1., 1., 1.],
      [1., 1., 1.],
      [1., 1., 1.],
      [1., 1., 1.]])

arr1.shape

OUTPUT:

(5,3)
np.zeros((5,6),dtype='float')

OUTPUT:

array([[0., 0., 0., 0., 0., 0.],
      [0., 0., 0.,0., 0., 0.],
      [0., 0., 0.,0., 0., 0.],
      [0., 0., 0.,0., 0., 0.],
      [0., 0., 0.,0., 0., 0.]])

 

Creating array of zeros

arr1 = np.zeros(5,dtype='int')

OUTPUT:

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

NumPy arange() is one of the array creation routines based on numerical ranges, it takes 3 arguments ,start, stop and step

arr=np.arange(2,20,3)
arr

OUTPUT:

array([ 2,  5,  8, 11, 14, 17])
np.arange(5.0,dtype='int')

OUTPUT:

array([0, 1, 2, 3, 4])
np.arange(15,1,-2)

OUTPUT:

array([15, 13, 11,  9,  7,  5,  3])

Creating numpy array using linspace, it returns number spaces evenly w.r.t interval.

arr=np.linspace(4,100,20)
arr

OUTPUT:

array([  4.        ,  9.05263158,  14.10526316,  19.15789474,
      24.21052632,  29.26315789,  34.31578947, 39.36842105,
      44.42105263,  49.47368421,  54.52631579, 59.57894737,
       64.63157895,  69.68421053, 74.73684211,  79.78947368,
      84.84210526,  89.89473684,  94.94736842, 100.        ])


NumPy comes with its own set of methods and operations

Let's define two lists and perform '+' operation on that.

list_1 = [1,2,3]
list_2 = [4,5,6]

list_1 + list_2

OUTPUT:

[1, 2, 3, 4, 5, 6]

Let's define two NumPy array and perform '+' operation on that.

np1 = np.array([1,2,3])
np2 = np.array([4,5,6])

np1 + np2

OUTPUT:

array([5, 7, 9])
Lesson Assignment
Challenge yourself with our lab assignment and put your skills to test.
# Python Program to find the area of triangle

a = 5
b = 6
c = 7

# Uncomment below to take inputs from the user
# a = float(input('Enter first side: '))
# b = float(input('Enter second side: '))
# c = float(input('Enter third side: '))

# calculate the semi-perimeter
s = (a + b + c) / 2

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is %0.2f' %area)
Sign up to get access to our code lab and run this code.