Python Libraries For Data Science

NumPy Array Functions

Trignometric functions
Enroll

Trignometric functions

import numpy as np

np.sin(45)

OUTPUT:

0.8509035245341184
arr= np.array([0,45,90])

np.sin(arr)

OUTPUT:

array([0.        , 0.85090352, 0.89399666])
np.cos(arr)

OUTPUT:

array([ 1.        ,  0.52532199, -0.44807362])
np.tan(arr)

np.pi

OUTPUT:

3.141592653589793

Exponential and logarithmic functions

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

OUTPUT:

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

np.exp(x)   
# e=2.718...

OUTPUT:

array([2.71828183e+00, 7.38905610e+00, 2.00855369e+01, 2.20264658e+04])
###     2^1, 2^2, 2^3, 2^4
np.exp2(x)

OUTPUT:

array([ 2.,  4.,  8., 16.])

The logarithmic function is an inverse function to exponentiation.

log 5 to base 10

np.log10(5)

OUTPUT:

0.6989700043360189

log 2 with base 2

np.log2(2)

OUTPUT:

1.0

log of 3 with base 10

np.log10(3)

OUTPUT:

0.47712125471966244

Aggregates

x = np.arange(1,100)
x

OUTPUT:

array([ 1,  2,  3, 4,  5,  6, 7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
      18, 19, 20, 21,22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34,
      35, 36, 37, 38,39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
      52, 53, 54, 55,56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68,
      69, 70, 71, 72,73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85,
      86, 87, 88, 89,90, 91, 92, 93, 94, 95, 96, 97, 98, 99])


np.sum(x)

OUTPUT:

4950

Accumulate stores the intermediate results in an array and returns that.

x = np.arange(1,6)

print("Array is:",x)

print("Accumlate add function:",np.add.accumulate(x))

OUTPUT:

Array is: [1 2 3 4 5]Accumlate add function: [ 1  3  6 10 15]
x = np.arange(1,6)
print("Array is:",x)

print("Accumlate multiply function:",np.multiply.accumulate(x))

OUTPUT:

Array is: [1 2 3 4 5]
Accumlate multiply function: [  1  2   6  24 120]

import numpy as np
a=np.array([[1,7],[2,4]])


b=np.array([[3,3],[5,2]])
print(a)

print(b)

OUTPUT:

[[1 7]
[2 4]]
[[3 3]
[5 2]]

b.shape

OUTPUT:

(2,2)

print(np.matmul(a,b))

OUTPUT:

[[38 17]
[26 14]]

print(np.dot(a,b))
[[38 17]
[26 14]]


The numpy.matmul function performs matrix multiplication. It is similar to numpy.dot for 2-D arrays, but there are differences in behavior for other dimensional arrays:
For 2-D arrays, numpy.matmul and numpy.dot perform the same matrix multiplication operation.
For 1-D arrays, the results are different

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

vector_b = np.array([1, 5, 6])

np.dot(vector_a, vector_b)

41

vector_a.shape

(3,)

vector_b.shape

(3,)

np.dot(vector_a, vector_b)

41

np.matmul(vector_a, vector_b)

41

matrix_b

OUTPUT:

array([[ 7,  8],
      [ 9, 10],
      [11, 12]])


matrix_a

OUTPUT:

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

np.dot(matrix_a, matrix_b)

OUTPUT:

array([[ 58,  64],
      [139, 154]])


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

matrix_b = np.array([[7, 8], [9, 10], [11, 12]])

np.matmul(matrix_a, matrix_b)

OUTPUT:

array([[ 58,  64],
      [139, 154]])

a=np.array([1,1,3,1])
b=np.array([1,2,1,1])
a * b

OUTPUT:

array([1, 2, 3, 1])
Matrix A raised to power 3
a=np.array([[2,0],[0,2]])
a

OUTPUT:

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

np.linalg.matrix_power(a,3) # matrix multiplication A A

OUTPUT:

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

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.
Sign up

Create Your Account Now!

Join our platform today and unlock access to expert- led courses, hands- on exercises, and a supportive learning community.
Sign Up