# Introduction_to_NumPy.pdf - This notebook was adapted from...

• Notes
• 35

This preview shows page 1 - 5 out of 35 pages.

The preview shows page 3 - 5 out of 35 pages.
This notebook was adapted from thePython Data Science Handbookby Jake VanderPlas. The content isavailable onGitHub().Introduction to NumPyNumPy (short for Numerical Python) provides an efficient interface to store and operate on dense data buffers.In some ways, NumPy arrays are like Python's built-in list type, but NumPy arrays provide much more efficientstorage and data operations as the arrays grow larger in size.If you installed Anaconda, you already have NumPy installed and ready to go, or you can go to()and follow the installation instructions found there. Once you do,you can import NumPy and double-check the version:In :By convention, you'll find that most people will import NumPy using np as an alias:In :Creating Arrays from Python ListsFirst, we can use np.array to create arrays from Python lists:In :Remember that unlike Python lists, NumPy is constrained to arrays that all contain the same type. If types donot match, NumPy will upcast if possible (here, integers are up-cast to floating point):In :Out:'1.16.3'Out:array([1, 4, 2, 5, 3])Out:array([3.14, 4., 2., 3.])importnumpynumpy.__version__importnumpyasnp# integer array:np.array([1,4,2,5,3])np.array([3.14,4,2,3])
We can use thedtypekeyword to explicitly set the data type of the resulting array:In :Unlike Python lists, NumPy arrays can explicitly be multi-dimensional; here's one way of initializing amultidimensional array using a list of lists:In :The inner lists are treated as rows of the resulting two-dimensional array.Creating Arrays from ScratchEspecially for larger arrays, it is more efficient to create arrays from scratch using routines built into NumPy.Here are several examples:In :In :Out:array([1., 2., 3., 4.], dtype=float32)Out:array([[2, 3, 4],[4, 5, 6],[6, 7, 8]])Out:array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])Out:array([[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.]])np.array([1,2,3,4], dtype='float32')# nested lists result in multi-dimensional arraysnp.array([range(i, i+3)foriin[2,4,6]])# Create a length-10 integer array filled with zerosnp.zeros(10, dtype=int)# Create a 3x5 floating-point array filled with onesnp.ones((3,5), dtype=float)
In :In :In :In :In :Out:array([[3.14, 3.14, 3.14, 3.14, 3.14],[3.14, 3.14, 3.14, 3.14, 3.14],[3.14, 3.14, 3.14, 3.14, 3.14]])Out:array([ 0,2,4,6,8, 10, 12, 14, 16, 18])Out:array([0., 0.25, 0.5 , 0.75, 1.])Out:array([[0.25499327, 0.74832157, 0.70969185],[0.72810541, 0.14312887, 0.86087057],[0.38073048, 0.1541186 , 0.50612169]])Out:array([[ 0.88587909, -0.71969411, -0.00333912],[ 0.68365454, -0.66764188,1.07801582],[ 0.46099508, -1.02934785,1.01503837]])# Create a 3x5 array filled with 3.14np.full((3,5),3.14)# Create an array filled with a linear sequence# Starting at 0, ending at 20, stepping by 2# (this is similar to the built-in range() function)np.arange(0,20,2)# Create an array of five values evenly spaced between 0 and 1np.linspace(0,1,5)# Create a 3x3 array of uniformly distributed random values between 0 and 1np.random.random((3,3))# Create a 3x3 array of normally distributed random values with mean 0 and standard deviatinp.random.normal(0,1, (3,3))
In :In :In :NumPy Array AttributesLet's discuss some useful array attributes. We'll start by defining three random arrays, a one-dimensional, two-

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 35 pages?

Course Hero member to access this document

Term
Winter
Professor
NoProfessor
Tags
Array, NumPy
• • • 