Difference between revisions of "Numpy"

From wiki
Jump to navigation Jump to search
(Created page with "category:Python Module easing handling large data sets. =Array= Class of iterable, mutable objects. Very much like a list but can have elements...")
 
Line 5: Line 5:
 
Class of iterable, mutable objects. Very much like a [[Python:Datatypes#list|list]] but can have elements of only one(1) type (booleans can be mixed with numeric types, True = 1, False = 0). Arrays have their own set of methods. Some things are similar to lists, others differ.
 
Class of iterable, mutable objects. Very much like a [[Python:Datatypes#list|list]] but can have elements of only one(1) type (booleans can be mixed with numeric types, True = 1, False = 0). Arrays have their own set of methods. Some things are similar to lists, others differ.
  
[[Python:Datatypes#Slicing|Slicing]] works like in lists.
+
[[Python:DataTypes#Slicing|Slicing]] works like in lists.
  
 
Numpy provides automatic mapping of operations to the array elements.
 
Numpy provides automatic mapping of operations to the array elements.
Line 14: Line 14:
 
;array1 > x
 
;array1 > x
 
:Returns an array with all elements from array1 larger than x
 
:Returns an array with all elements from array1 larger than x
 +
 +
Arrays are by default multi-dimensional. Basically this is a list of arrays.
 +
;mdarray[0][2]
 +
;mdarray[0,2]  (preferred)
 +
:Return the 3rd element from the first array (row 1)
 +
 +
;mdarray.shape
 +
;np.shape(mdarray)
 +
:Return the array's shape as [[Python:DataTypes#Tuple|tuple]] (rows,colums)
 +
:If the rows have different number of columns only the number of rows is returned (rows,)
 +
 +
Slicing works too:
 +
;mdarray[<nowiki>:</nowiki>,2]
 +
:Return the 3rd element of all rows
 +
;mdarray[2,4<nowiki>:</nowiki>6]
 +
:Return from the 3rd row the the elements 4 and 5 (5th and 6th)
 +
 +
Operations are still applied to all elements on all rows
 +
;mdarray * 2
 +
:Operate on all elements
 +
 +
;mdarray * array(1row)
 +
:Multiply each element in all rows of mdarray with the corresponding element in array
 +
 +
;numpy.mean(array)
 +
:Return the average of all values in array
 +
 +
;numpy.median(array)
 +
:Return the middle value of array(sorted)

Revision as of 23:00, 26 November 2018

Module easing handling large data sets.

Array

Class of iterable, mutable objects. Very much like a list but can have elements of only one(1) type (booleans can be mixed with numeric types, True = 1, False = 0). Arrays have their own set of methods. Some things are similar to lists, others differ.

Slicing works like in lists.

Numpy provides automatic mapping of operations to the array elements.

array1 / array2
Returns an array of the results from the division of all elements of array1 by the corresponding element of array2. Array1 and array2 must have the same number of elements.
array1 > x
Returns an array with all elements from array1 larger than x

Arrays are by default multi-dimensional. Basically this is a list of arrays.

mdarray[0][2]
mdarray[0,2] (preferred)
Return the 3rd element from the first array (row 1)
mdarray.shape
np.shape(mdarray)
Return the array's shape as tuple (rows,colums)
If the rows have different number of columns only the number of rows is returned (rows,)

Slicing works too:

mdarray[:,2]
Return the 3rd element of all rows
mdarray[2,4:6]
Return from the 3rd row the the elements 4 and 5 (5th and 6th)

Operations are still applied to all elements on all rows

mdarray * 2
Operate on all elements
mdarray * array(1row)
Multiply each element in all rows of mdarray with the corresponding element in array
numpy.mean(array)
Return the average of all values in array
numpy.median(array)
Return the middle value of array(sorted)