https://www.udemy.com/python-for-data-science-and-machine-learning-bootcamp/learn/v4/overview
Answers by Jenifer Yoon
Date 3/27/2019
import numpy as np
np.zeros(10)
np.ones(10)
np.ones(10)*5
np.arange(10, 51)
np.arange(10, 51, 2)
mat = np.arange(0, 9)
mat.reshape(3, 3)
np.eye(3)
np.eye(3, 3)
np.random.rand(1)
# np.random.rand(d0, d1, d2, ...) selects a random number from [0, 1) range. Paramters are dimensions.]
help(np.random.rand)
np.random.rand(25)
np.random.rand(25)
np.arange(0.01, 1.01, .01).reshape(10, 10)
np.linspace(0, 1, 20)
np.linspace(0, 1, 20, endpoint=True)
# linspace returns an array of evenly spaced numbers. (start, spop, numbers, default include endpoint=True)
Now you will be given a few matrices, and be asked to replicate the resulting matrix outputs:
mat = np.arange(1,26).reshape(5,5)
mat
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
mat[2:6, 1:6]
# Indexing is same as list indexing.
# Try negative indexing.
mat[-3:, -4:]
# Negative indexing. Always counts from top-left. [Row start:end, Col start:end]
mat[:-3, :-4]
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
mat[3, -1]
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
mat
# mat 2nd column, 0 to 2 rows.
mat[:3, 1].reshape(3, 1)
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
mat[-1, :]
# WRITE CODE HERE THAT REPRODUCES THE OUTPUT OF THE CELL BELOW
# BE CAREFUL NOT TO RUN THE CELL BELOW, OTHERWISE YOU WON'T
# BE ABLE TO SEE THE OUTPUT ANY MORE
mat[-2:, :] # Last two rows, all columns.
mat.sum() # Method applied to mat object?
# mat is an ndarray object, a built-in numpy class.
# This class object has a method .sum() and .std().
mat.std()
sum(mat)
# sum() function applied to mat ndarray object produce column sums by default.
sum(mat[:3, -2:])
# [row 0 to 2, col 4 to 5]
## mat.sum(axis=0)
# Columns axis=0, not 1.
x = mat.sum(axis=0)
y = mat.sum(axis=1)
print(x, y)