Changes

Jump to navigation Jump to search

Workshop: Advanced Computation (with Python)

1,454 bytes added, 22:14, 3 October 2019
Useful NumPy Commands
== Useful NumPy Commands ==
=== '''Creating Arrays''' ===
==== numpy.zeros ====
[[File:Numpy zeros.png|thumb|Example of numpy.zeros]]
In numpy we can create an array in many different ways. Firstly I will show you how to create a matrix to any shape with every value set to 0. For this case where we want a blank array we can use the '''zeros''' method. Referring to the NumPy documentation, we find that the syntax is simply numpy.zeros((shape)) where shape is a shape enclosed by parentheses with each axis separated by a comma.
==== numpy.matrix ====
[[File:Numpy.matrix.png|thumb|Example of numpy.matrix]]
We can also use numpy.matrix if we know the values we want in our array. Again, referring to the NumPy documentation, we see that there are actually two different ways we can represent arrays in NumPy. One syntax uses spaces and semicolons to separate elements. Another syntax uses commas and square brackets to separate elements.
  === '''Building / Appending to Arrays''' === ==== numpy.append ====We can append a new row or column to our matrix using the “append” method. Referring to the documentation you can see that the append method takes 3 arguments. 2 ‘array-like’ variables and an integer. The first variable is the original matrix that you want to add to. The second variable is the row or column of information to append. Finally, the last variable refers to the axis in which the row should be added to the end of. Numpy differentiates between axes like this: * 0 refers to X* 1 refers to Y* 2 refers to Z* 3+ refers to additional arbitrary axes ==== numpy.stack ====''(See also: [https://docs.scipy.org/doc/numpy/reference/generated/numpy.dstack.html?highlight=dstack#numpy.dstack numpy.dstack], [https://docs.scipy.org/doc/numpy/reference/generated/numpy.hstack.html?highlight=hstack#numpy.hstack numpy.hstack], [https://docs.scipy.org/doc/numpy/reference/generated/numpy.vstack.html?highlight=vstack#numpy.vstack numpy.vstack])'' Maybe instead of appending data to your array line-by-line you want to add all the data from one array and to the end of an existing array. This is where the stack method shines. According to the documentation, it takes 2 main arguments; a list of arrays to stack and a integer to differentiate which axis it should be appended to. '''It is important to note that the stack command only works for array with the same shape.  '''<br />
118
edits

Navigation menu