Python for Data Science - Using NumPy to perform arithmetic operations on data

Chapter 5 - Basic Math and Statistics

Segment 1 - Using NumPy to perform arithmetic operations on data

import numpy as np
from numpy.random import randn
np.set_printoptions(precision=2)

Creating arrays

Creating arrays using a list

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

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

b = np.array([[10,20,30],[40,50,60]])
b

array([[10, 20, 30],
[40, 50, 60]])

Creating arrays via assignment

np.random.seed(25)
c = 36*np.random.randn(6)
c

array([ 8.22, 36.97, -30.23, -21.28, -34.45, -8. ])

d = np.arange(1,35)
d

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])

Performing arthimetic on arrays

a*10

array([10, 20, 30, 40, 50, 60])

c + a

array([ 9.22, 38.97, -27.23, -17.28, -29.45, -2. ])

c - a

array([ 7.22, 34.97, -33.23, -25.28, -39.45, -14. ])

c*a

array([ 8.22, 73.94, -90.68, -85.13, -172.24, -48.02])

c/a

array([ 8.22, 18.48, -10.08, -5.32, -6.89, -1.33])

posted @ 2021-01-09 18:41  晨风_Eric  阅读(134)  评论(0)    收藏  举报