^^NumPy Numeric Python.

 

Creating Arrays from Python List

 

np.array(list(range(10)))
    array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

accetta anche l'input senza list(), con ugual risultato

np.array(range(10))

 

dtype keyword to explicitly set the data type of the resulting array

np.array([1, 2, 3, 4], dtype='float32')
    array([1., 2., 3., 4.], dtype=float32)

multi-dimensional arrays from nested lists

np.array([range(i, i + 3) for i in [2, 4, 6]])
     array([[2, 3, 4],
           [4, 5, 6],
           [6, 7, 8]])

create arrays using built in NumPy routines

Create a length-10 integer array filled with zeros

 
np.zeros(10, dtype=int)
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
 
np.full((3, 5), 3.14)
np.arange(5, 20, 2.5)  min,max,step

values evenly spaced (rem: 5 dita 4 spazi)

np.linspace(0, 1, 5)   min,max,quanti_nr
 
np.random.random(10)           10 nr tra 0 e 1
np.random.randint(0,100,10)    10 nr in [0,100)
np.random.random((4, 3))       nr_righe,nr_colonne
np.random.normal(0,1,(4, 3))
 

ARRAY OP   PythonDataScienceHandbook

 

 

np.random.seed(0)                           # seed for reproducibility

x1 = np.random.randint(10, size=6)          # One-dimensional array
x2 = np.random.randint(10, size=(3, 4))     # Two-dimensional array
x3 = np.random.randint(10, size=(3, 4, 5))  # Three-dimensional array

NumPy Array Attributes

    x3 ndim: 3
    x3 shape: (3, 4, 5)
    x3 size: 60
 
    dtype: int32
 
    itemsize: 4 bytes
    nbytes: 240 bytes
 
  1. Indexing of arrays: Getting and setting the value of individual array elements
  2. Slicing of arrays: Getting and setting smaller subarrays within a larger array
  3. Reshaping of arrays: Changing the shape of a given array
  4. Joining and splitting of arrays: Combining multiple arrays into one, and splitting one array into many

Array Indexing: Accessing Single Elements, getting and setting the value

just as with Python lists:

a one-dimensional array, the ith value (counting from zero) can be accessed by specifying the desired index in square brackets.

To index from the end of the array, use negative indices (counting from -1)

 

In a mD-array, items can be accessed using a comma-separated tuple of indices

x3[0,0,0]
x3[2,3,4]

 

inserting a floating-point value to an integer array, the value will be silently truncated.


Array Slicing: Accessing Subarrays

x[start:stop:step]

default: start=0, stop=size of dimension, step=1

The NumPy slicing syntax follows that of the standard Python list.

Multi-dimensional slices work in the same way, with multiple slices separated by commas.

Subarrays are views, not copies; instead in Python list, slices will be copies,

x2_sub_copy = x2[:2, :2].copy()

Array Reshaping

np.arange(1, 10).reshape((3, 3))
    array([[1, 2, 3],
           [4, 5, 6],
           [7, 8, 9]])

to put the numbers 1 through 9 in a 3×3 grid

 

 

1-dimensional array to a 2-dimensional row or column matrix

is common reshaping, can be done with the reshape method, or using of the newaxis keyword within a slice operation

x = np.array([1, 2, 3])
x.reshape((1, 3))
 =  
x[np.newaxis, :])
x.reshape((3, 1))
 =
x[:, np.newaxis]
 

Computation on NumPy Arrays: Universal Functions

 
 

 

wp/Vectorization (mathematics) | Vectorizing Loops

Automatic differentiation, differenziazione automatica, algoritmica.

 

http://www.taigtools.com/cmill.html

from http://www.lfd.uci.edu/tour/

from Unofficial Windows Binaries for Python Extension Packages

from http://packages.python.org/uncertainties/

from http://wiki.python.org/moin/NumericAndScientific

 

"Zen of NumPy" >>>

  1. Strided is better than scattered
  2. Contiguous is better than strided
  3. Descriptive is better than imperative (use data-types)
  4. Array-oriented is often better than object-oriented
  5. Broadcasting is a great idea -- use where possible
  6. Vectorized is better than an explicit loop.
    Unless it’s complicated --- then use numexpr, weave, or Cython
  7. Think in higher dimensions

Having a nice syntax

The point of having nice syntax is to minimize the line-noise and mental overhead of mapping the mental idea to working code

ref: technicaldiscovery.blogspot by Travis Oliphant