Tutorial 2
Tutorial 2
[ ]: import numpy as np
There are several built-in functions in numpy that can be used to create ndarrays
1
[ ]: print(np.random.rand(5)) # random numbers from a uniform distribution␣
,→between [0,1]
[ ]: x = np.array([1,2,3,4,5])
print(x + 1) # addition
print(x - 1) # subtraction
print(x * 2) # multiplication
print(x // 2) # integer division
print(x ** 2) # square
print(x % 2) # modulo
print(1 / x) # division
[ ]: x = np.array([2,4,6,8,10])
y = np.array([1,2,3,4,5])
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x // y)
print(x ** y)
[ ]: x = np.arange(-5,5)
print(x)
2
y[:] = 1000 # modifying the value of y will change x
print(y)
print(x)
my2darr = np.array(my2dlist)
print(my2darr)
print(my2darr[2][:]) # access the third row
print(my2darr[2,:]) # access the third row
print(my2darr[:][2]) # access the third row (similar to 2d list)
print(my2darr[:,2]) # access the third column
print(my2darr[:2,2:]) # access the first two rows & last two columns
[ ]: my2darr = np.arange(1,13,1).reshape(3,4)
print(my2darr)
divBy3 = my2darr[my2darr % 3 == 0]
print(divBy3, type(divBy3))
[ ]: my2darr = np.arange(1,13,1).reshape(4,3)
print(my2darr)
3
1.4 2.1.4 Numpy Arithmetic and Statistical Functions
There are many built-in mathematical functions available for manipulating elements of nd-array.
[ ]: x = np.arange(-2,3)
y = np.random.randn(5)
print(x)
print(y)
4
[ ]: X = np.random.randn(5,3)
print(X)
s = Series([3.1, 2.4, -1.7, 0.2, -2.9, 4.5]) # creating a series from a list
print(s)
print('Values=', s.values) # display values of the Series
print('Index=', s.index) # display indices of the Series
[ ]: import numpy as np
[ ]: s3 = Series([1.2,2.5,-2.2,3.1,-0.8,-3.2],
index = ['Jan 1','Jan 2','Jan 3','Jan 4','Jan 5','Jan 6',])
print(s3)
print('Values=', s3.values) # display values of the Series
print('Index=', s3.index) # display indices of the Series
5
s4 = Series(capitals) # creating a series from dictionary object
print(s4)
print('Values=', s4.values) # display values of the Series
print('Index=', s4.index) # display indices of the Series
[ ]: s3 = Series([1.2,2.5,-2.2,3.1,-0.8,-3.2],
index = ['Jan 1','Jan 2','Jan 3','Jan 4','Jan 5','Jan 6',])
print(s3)
6
[ ]: carData2 = DataFrame(cars, index = [1,2,3,4]) # change the row index
carData2['year'] = 2018 # add column with same value
carData2['dealership'] = ['Courtesy Ford','Capital Honda','Spartan Toyota','N/
,→A']
[ ]: tuplelist = [(2011,45.1,32.4),(2012,42.4,34.5),(2013,47.2,39.2),
(2014,44.2,31.4),(2015,39.9,29.8),(2016,41.5,36.7)]
columnNames = ['year','temp','precip']
weatherData = DataFrame(tuplelist, columns=columnNames)
weatherData
[ ]: import numpy as np
print(data['x2'])
print(type(data['x2']))
print('carData2.iloc[1:3,1:3]=')
print(carData2.iloc[1:3,1:3])
7
[ ]: print('carData2.shape =', carData2.shape)
print('carData2.size =', carData2.size)
[ ]: print(data)
print('Addition:')
print(data + 4) # addition operation
print('Multiplication:')
print(data * 10) # multiplication operation
[ ]: print('data =')
print(data)
columnNames = ['x1','x2','x3']
data2 = DataFrame(np.random.randn(5,3), columns=columnNames)
print('\ndata2 =')
print(data2)
8
print(data.mean(axis=1)) # get average value for each row
[ ]: %matplotlib inline
s3 = Series([1.2,2.5,-2.2,3.1,-0.8,-3.2,1.4],
index = ['Jan 1','Jan 2','Jan 3','Jan 4','Jan 5','Jan 6','Jan 7'])
s3.plot(kind='line', title='Line plot')
[ ]: tuplelist = [(2011,45.1,32.4),(2012,42.4,34.5),(2013,47.2,39.2),
(2014,44.2,31.4),(2015,39.9,29.8),(2016,41.5,36.7)]
columnNames = ['year','temp','precip']
weatherData = DataFrame(tuplelist, columns=columnNames)
weatherData[['temp','precip']].plot(kind='box', title='Box plot')