NumPy – 16 – Calcoli con gli arrays NumPy – funzioni universali – 2

Anette Moldvaer - coffee

Anette Moldvaer – coffee

Continuo da qui con le ufunc, qui.

Ufuncs exist in two flavors: unary ufuncs, which operate on a single input, and binary ufuncs, which operate on two inputs. We’ll see examples of both these types of functions here.

Ufuncs aritmetiche
NumPy’s ufuncs feel very natural to use because they make use of Python’s native arithmetic operators. The standard addition, subtraction, multiplication, and division can all be used:

np101

There is also a unary ufunc for negation, and a ** operator for exponentiation, and a % operator for modulus:

np102

In addition, these can be strung together however you wish, and the standard order of operations is respected:

np103

Each of these arithmetic operations are simply convenient wrappers around specific functions built into NumPy; for example, the + operator is a wrapper for the add function:

np104

The following table lists the arithmetic operators implemented in NumPy:

Op. Equivalent ufunc Description
+   np.add           Addition (e.g., 1 + 1 = 2)
-   np.subtract      Subtraction (e.g., 3 - 2 = 1)
-   np.negative      Unary negation (e.g., -2)
*   np.multiply      Multiplication (e.g., 2 * 3 = 6)
/   np.divide        Division (e.g., 3 / 2 = 1.5)
//  np.floor_divide  Floor division (e.g., 3 // 2 = 1)
**  np.power         Exponentiation (e.g., 2 ** 3 = 8)
%   np.mod           Modulus/remainder (e.g., 9 % 4 = 1)

Additionally there are Boolean/bitwise operators; we will explore these [prossimamente].

Valore assoluto
Just as NumPy understands Python’s built-in arithmetic operators, it also understands Python’s built-in absolute value function:

np105

The corresponding NumPy ufunc is np.absolute, which is also available under the alias np.abs:

np106

This ufunc can also handle complex data, in which the absolute value returns the magnitude:

np107

Funzioni trigonometriche
NumPy provides a large number of useful ufuncs, and some of the most useful for the data scientist are the trigonometric functions.

np108

The values are computed to within machine precision, which is why values that should be zero do not always hit exactly zero. Inverse trigonometric functions are also available:

np109

Continua 😊

:mrgreen:

Logistica informatizzata

nikon-small-world-butterfly-scales
Visto l’altro giorno, forse mi faccio dei nemici ma devo raccontarla 😜
Ormai tutti hanno il telefono che fa tutto quello che una volta si faceva con il computer.
Ormai si possono semplificare tante operazioni con internet –e il telefono ce l’ha– eliminando inutili passaggi umani.

Ecco il mio caso: per gestire gli ordini di una clientela stabile e ampiamente conosciuta fino all’anno scorso –uh! due anni ormai– si faceva l’ordine al telefono. Nel caso in oggetto l’ordine (in dialetto piemontese comand, pronuncia cumand) è semplice, basta identificarsi, precisare il codice del prodotto (sono pochi e mnemonici, mica l’Ikea) e il quintalaggio (termine tecnico). Va da se che è sempre urgente, non devi precisarlo che perderesti solo tempo. Chi riceveva la telefonata compilava un modulo che fotocopiava e passava al magazzino.
Visto che tutti hanno il telefono e internet si può informatizzare? Certo, semplicissimo, prima di subito. Fatto! 😄

Per i primi tempi si può adottare una procedura provvisoria: chi riceve la telefonata che continua ad arrivare compila l’ordine nell’app –previo inserimento del codice cliente– e racconta della nuova procedura automatizzata, quando sei comodo ti mandiamo il nostro tecnico…
Poi stampa l’ordine, 2 copie di cui una per il magazzino, fatto anche questo.

Ormai la fase provvisoria dovrebbe essere passata, da tempo. No, non esattamente: il cliente trova molto bello che l’app abbia anche l’interfaccia vocale.

Non vale nemmeno la differenza che via ‘puter non si è legati all’orario d’ufficio, mi dicono.
Funziona; l’informatizzazione va introdotta dolcemente, un po’ alla volta, adattandola alle condizioni ambientali 😜

:mrgreen:

SICP – cap. 2 – Esercizi: intervallo aritmetico – 12

topi

Sempre esercizi, continuo da qui copiando qui.

Exercise 2.9: The width of an interval is half of the difference between its upper and lower bounds. The width is a measure of the uncertainty of the number specified by the interval. For some arithmetic operations the width of the result of combining two intervals is a function only of the widths of the argument intervals, whereas for others the width of the combination is not a function of the widths of the argument intervals. Show that the width of the sum (or difference) of two intervals is a function only of the widths of the intervals being added (or subtracted). Give examples to show that this is not true for multiplication or division.

Questo esercizio mi mette in difficoltà, non perché sia difficile –anzi– ma perché consiste nel verificare un’asserzione matematica. E invece di mettere i miei tentativi mi rivolgo a Bill the Lizard.

Che poi anche Bill per il caso della moltiplicazione non sviluppa la dimostrazione matematica ma riporta solo una prova empirica (cioè un caso numerico).
La soluzione riportata da sicp-ex è simile e più sintetica (al solito).

Sì, salto ma non è un esercizio di quelli che mi attirano 👿  sarò più diligente i fururo, sorse 😉

:mrgreen:

NumPy – 15 – Calcoli con gli arrays NumPy – funzioni universali – 1

cxkwkc1wqaaqevl

Continuo da qui; adesso entriamo nel merito dice Jake, qui.

Up until now, we have been discussing some of the basic nuts and bolts of NumPy; in the next few sections, we will dive into the reasons that NumPy is so important in the Python data science world. Namely, it provides an easy and flexible interface to optimized computation with arrays of data.

Computation on NumPy arrays can be very fast, or it can be very slow. The key to making it fast is to use vectorized operations, generally implemented through NumPy’s universal functions (ufuncs). This section motivates the need for NumPy’s ufuncs, which can be used to make repeated calculations on array elements much more efficient. It then introduces many of the most common and useful arithmetic ufuncs available in the NumPy package.

La lentezza dei cicli
Python’s default implementation (known as CPython) does some operations very slowly. This is in part due to the dynamic, interpreted nature of the language […]. Recently there have been various attempts to address this weakness: PyPy, Cython and Numba. Each of these has its strengths and weaknesses, but it is safe to say that none of the three approaches has yet surpassed the reach and popularity of the standard CPython engine.

The relative sluggishness of Python generally manifests itself in situations where many small operations are being repeated – for instance looping over arrays to operate on each element. For example, imagine we have an array of values and we’d like to compute the reciprocal of each. A straightforward approach might look like this:

np95

This implementation probably feels fairly natural to someone from, say, a C or Java background. But if we measure the execution time of this code for a large input, we see that this operation is very slow, perhaps surprisingly so! We’ll benchmark this with IPython’s %timeit magic (discussed in Profiling and Timing Code):

np96
It takes several seconds [ahemmm, più di 1] to compute these million operations and to store the result! When even cell phones have processing speeds measured in Giga-FLOPS (i.e., billions of numerical operations per second), this seems almost absurdly slow. It turns out that the bottleneck here is not the operations themselves, but the type-checking and function dispatches that CPython must do at each cycle of the loop. Each time the reciprocal is computed, Python first examines the object’s type and does a dynamic lookup of the correct function to use for that type. If we were working in compiled code instead, this type specification would be known before the code executes and the result could be computed much more efficiently.

Introduco le UFuncs
For many types of operations, NumPy provides a convenient interface into just this kind of statically typed, compiled routine. This is known as a vectorized operation. This can be accomplished by simply performing an operation on the array, which will then be applied to each element. This vectorized approach is designed to push the loop into the compiled layer that underlies NumPy, leading to much faster execution.

Compare the results of the following two:

np97

Looking at the execution time for our big array, we see that it completes orders of magnitude faster than the Python loop:

np98

Vectorized operations in NumPy are implemented via ufuncs, whose main purpose is to quickly execute repeated operations on values in NumPy arrays. Ufuncs are extremely flexible – before we saw an operation between a scalar and an array, but we can also operate between two arrays:

np99

And ufunc operations are not limited to one-dimensional arrays–they can also act on multi-dimensional arrays as well:

np100

Computations using vectorization through ufuncs are nearly always more efficient than their counterpart implemented using Python loops, especially as the arrays grow in size. Any time you see such a loop in a Python script, you should consider whether it can be replaced with a vectorized expression.

Continua 😊

:mrgreen:

cit. & loll – 32

Ecco che arriva 😊 🎶 🚀

oggi

Signor Zuckerberg, Signor Zuckerberg!
::: welikechopin

You know what std::allocator is, right?
::: isotrumpp

Presto, un convegno, un appello, un disegno di legge per contrastare questo terribile fenomeno della #fakepolitics!1
::: fabiochiusi

working on a tiny zine for a keynote I’m writing
::: b0rk

ciq0hafwgaargpx

I work on some problems [of] no practical significance
::: Symbo1ics

Cubic trisection by Oskar van Deventer
::: MachinePix

Who are you going to believe
::: zachheltzel

School of Informatics, University of Edinburgh
::: GrayInGlasgow

When I prematurely overengineer my code
::: victorporof

c5esa2tumaadiug

#java #cplusplus #lisp
::: sovietspaceship

Assolutamente perfetto
::: AlfonsoFuggetta

Twitter has become completely unreadable
::: Donearm

True story
::: tespitograf

Do you use a background image for your shell window?
::: climagic

16831819_10212113269007309_8339218940630865583_n

Do you use a transparent background in your shell window?
::: climagic

Multics working very hard to generate a Fibonacci
::: sdf_pubnix

A heuristic we follow is that whenever we feel the need to comment something
::: KC_56

Self documenting code
::: Symbo1ics

Where there are two bugs, there is likely to be a third
::: CodeWisdom

Dite no ai monitor di sistema fighetti!
::: Flavio_MfM

John Edmark

John Edmark

SICP – cap. 2 – Esercizi: intervallo aritmetico – 11

ox_1hg-z

Continuo da qui con gli esercizi, qui.

Exercise 2.8: Using reasoning analogous to Alyssa’s [post precedente], describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval.

Semplicissimo, basta sostiuire il segno + con - in add-interval –ahemmm NO, l’intervallo massimo si ha, vedi Bill, [a,b] − [c,d] = [a − d, b − c], ecco:

(define (sub-interval x y)
  (make-interval (- (lower-bound x) 
                    (upper-bound y))
                 (- (upper-bound x) 
                    (lower-bound y))))

Aggiungo questa procedura al file apa.rkt e eseguo:

s161

Nota per me: nel file apa.rkt mancavano le procedure lower-bound e upper-bound; aggiunte.

A questo punto mi verrebbe quasi voglia di continuare con il prossimo ma il post diventerebbe troppo lungo, rimando alla prossima puntata 😜

I miei nerds di riferimento: Bill the Lizard, sicp-ex (interessante) e Drewiki.

:mrgreen:

NumPy – 14 – Le basi degli arrays di NumPy – 3

the_house_by_the_railroad

Continuo da qui, copio qui.

Ridimensionare gli arrays
Another useful type of operation is reshaping of arrays. The most flexible way of doing this is with the reshape method. For example, if you want to put the numbers 1 through 9 in a 3×3 grid, you can do the following:

np87

Note that for this to work, the size of the initial array must match the size of the reshaped array. Where possible, the reshape method will use a no-copy view of the initial array, but with non-contiguous memory buffers this is not always the case.

Another common reshaping pattern is the conversion of a one-dimensional array into a two-dimensional row or column matrix. This can be done with the reshape method, or more easily done by making use of the newaxis keyword within a slice operation:

np88

We will see this type of transformation often throughout the remainder of the book. Da ricordarselo; io sarei per l’altra versione.

Concatenazione e suddivisione di arrays
All of the preceding routines worked on single arrays. It’s also possible to combine multiple arrays into one, and to conversely split a single array into multiple arrays. We’ll take a look at those operations here.

Concatenazione di arrays
Concatenation, or joining of two arrays in NumPy, is primarily accomplished using the routines np.concatenate, np.vstack, and np.hstack. np.concatenate takes a tuple or list of arrays as its first argument, as we can see here:

np89

You can also concatenate more than two arrays at once:

np90

It can also be used for two-dimensional arrays:

np91

For working with arrays of mixed dimensions, it can be clearer to use the np.vstack (vertical stack) and np.hstack (horizontal stack) functions:

np92

Similary, np.dstack will stack arrays along the third axis.

Suddividere arrays
The opposite of concatenation is splitting, which is implemented by the functions np.split, np.hsplit, and np.vsplit. For each of these, we can pass a list of indices giving the split points:

np93

Notice that N split-points, leads to N + 1 subarrays. The related functions np.hsplit and np.vsplit are similar:

np94

Similarly, np.dsplit will split arrays along the third axis.

:mrgreen:

Bisogna essere precisi

mib4

Jake VanderPlas rockz! 🚀 e oggi recentemente ne ha dato un’ulteriore dimostrazione, con questo tweet.

Si sa che io ci metto un po’ a capire le cose e poi c’è il rischio che me le dimentico e allora ho deciso di mettere giù tutta la storia, qui in questo post.

Intanto con l’installazione di Anaconda (sempre per merito|colpa di Jake (e del suo corso su NumPy)) sono cambiati i comandi, ecco:

i0

Questo comporterà di ridefinire la shebang in caso di proteste degli script, l’evilussione 👿

OK, sono pronto per ripetere il test di Jake:

i1

Ecco 💥 errore! 😡

Twitter impone la concisione, a volte troppo. E Jake da per scontato che chi lo segue sia smart come lui. E anche i commentatori, tutti. Mi lasciano solo 😯

Però, dai, l’avevo già sentita questa raccomandazione: import * neanche una volta; che si potrebbe anche dire, con parole mie mai import *.
Verifico:

i2

Ecco, proprio come dice Jake: import *. Not even once.
Perché sum viene ridefinita in un modulo di Numpy, con altri argomenti.
OK, l’informatica e il mondo sono salvi; e io posso pensare ad altro 🎶 🎵 😄
:mrgreen:

SICP – cap. 2 – Esercizi: intervallo aritmetico – 10

nikon-small-world-air-bubbles-formed-from-melted-ascorbic-acid-crystals

Continuo da qui, oggi copio qui.

Esercizio esteso: intervallo aritmetico
Alyssa P. Hacker is designing a system to help people solve engineering problems. One feature she wants to provide in her system is the ability to manipulate inexact quantities (such as measured parameters of physical devices) with known precision, so that when computations are done with such approximate quantities the results will be numbers of known precision.
Alyssa è una lisp hacker 😜

Electrical engineers will be using Alyssa’s system to compute electrical quantities. It is sometimes necessary for them to compute the value of a parallel equivalent resistance Rp of two resistors R1 and R2 using the formula

s158

Resistance values are usually known only up to some tolerance guaranteed by the manufacturer of the resistor. For example, if you buy a resistor labeled “6.8 ohms with 10% tolerance” you can only be sure that the resistor has a resistance between 6.8 − 0.68 = 6.12 and 6.8 + 0.68 = 7.48 ohms. Thus, if you have a 6.8-ohm 10% resistor in parallel with a 4.7-ohm 5% resistor, the resistance of the combination can range from about 2.58 ohms (if the two resistors are at the lower bounds) to about 2.97 ohms (if the two resistors are at the upper bounds).

Alyssa’s idea is to implement “interval arithmetic” as a set of arithmetic operations for combining “intervals” (objects that represent the range of possible values of an inexact quantity). The result of adding, subtracting, multiplying, or dividing two intervals is itself an interval, representing the range of the result.

Alyssa postulates the existence of an abstract object called an “interval” that has two endpoints: a lower bound and an upper bound. She also presumes that, given the endpoints of an interval, she can construct the interval using the data constructor make-interval. Alyssa first writes a procedure for adding two intervals. She reasons that the minimum value the sum could be is the sum of the two lower bounds and the maximum value it could be is the sum of the two upper bounds:

(define (add-interval x y)
  (make-interval (+ (lower-bound x) 
                    (lower-bound y))
                 (+ (upper-bound x) 
                    (upper-bound y))))

Alyssa also works out the product of two intervals by finding the minimum and the maximum of the products of the bounds and using them as the bounds of the resulting interval. (min and max are primitives that find the minimum or maximum of any number of arguments.)

(define (add-interval x y)
  (make-interval (+ (lower-bound x) 
                    (lower-bound y))
                 (+ (upper-bound x) 
                    (upper-bound y))))

To divide two intervals, Alyssa multiplies the first by the reciprocal of the second. Note that the bounds of the reciprocal interval are the reciprocal of the upper bound and the reciprocal of the lower bound, in that order.

(define (add-interval x y)
  (make-interval (+ (lower-bound x) 
                    (lower-bound y))
                 (+ (upper-bound x) 
                    (upper-bound y))))

OK, con questo ecco…

Exercise 2.7: Alyssa’s program is incomplete because she has not specified the implementation of the interval abstraction. Here is a definition of the interval constructor:

(define (make-interval a b) (cons a b))

Define selectors upper-bound and lower-bound to complete the implementation.

Raccolgo per comodità nel file apa.rkt le 4 procedure che caricherò in Racket.

s159

A questo punto ecco i risultati

s160

AhemmmmBill the Lizard e sicp-ex.

Mi sa che si continuerà con questa roba, prossimamente 😳

:mrgreen:

Il Nuovo Mondo

build
C’è questo post di Mark Zuckerberg, quello di Facebook, che ci sto pensando su da qualche giorno. Per me intanto un anacoluto, per tanti altri articoli, commenti, roba.

Parlo per me: Facebook non mi piace più di tanto. Il mio social preferito è Twitter ma gli amici, quelli reali e quelli del Web sono su Facebook e allora ogni tanto ci faccio un salto anch’io. C’è chi riesce a sviluppare discorsi articolati (non faccio nomi ma è facile indovinare) con commenti e risposte adeguate. Ma quelli sono un’eccezione, almeno per me: ho 173 amici (di FB) ma quelli con cui interagisco davvero sono molto di meno, forse un ventesimo. Peccato perché sto perdendo i contatti con chi avevo iniziato, gente che conosco da fuori del Web. I giovani e chi non deve usare il ‘puter per lavoro ormai usa il telefono e magari altri social. O tanti hanno realizzato che sono noioso (assay) 😡

brainpinkyMa oltre a queste considerazioni perso(nali) non mi è chiaro cosa ha in mente Mark. O vuole conquistare il mondo? Non solo economicamente, politicamente. Ecco io sono vecchio ma sono mooolto contrario. Non vorrei trovarmi un Silvio globale (ah! forse c’è già, Donald; e un aspirante in locale, il #Fuffaro (questa pare sia una considerazione solo mia, kissene 😡)). È una mia fissa vecchia: uno dei film cult (per me) è Quarto potere (Citizen Kane), da studente avevo fatto una tesina (allora si diceva “ricerca”), peccato non averla conservata, sarebbe antiquariato.

E gli altri? C’è l’imbarazzo della scelta.

TBL, sir Tim Berners-Lee, è stato lui a dirmelo (ci sarei arrivato comunque aprendo FB); strano che il suo tweet non abbia avuto una risonanza più ampia.
Fabio Chiusi subito dopo twitta il primo link a uno dei primi articoli sull’argomento. E sì, conferma fin da subito i miei timori. Come dice Fabio, da leggere tutto.
Sempre di Fabio –uno da followare se siete su Twitter– due altre dritte, qui e qui.
Annalee Newitz su Ars Technica, kneel before Zuck, è drastica, sono d’accordo con lei: Mark Zuckerberg’s manifesto is a political trainwreck. He says that Facebook is developing AI to create a global democracy—kind of.
Il Guardian, The Observer view on Mark Zuckerberg: Facebook’s success is prophetic of a future in which we may all have to rely on a few all-powerful companies benevolently gifting us scraps like grants for struggling media organisations, or a meagre basic income to supplement poverty wages. But today, consumers and citizens still have the power to demand something different, and the laws of our lands still have the power to enforce it. We should act while we can.
The Atlantic, The Mark Zuckerberg Manifesto Is a Blueprint for Destroying Journalism. Secondo me non solo il giornalismo.
Sono più in sintonia con Polis: That Facebook vision thing: a platform still grappling with political realities: I initially described Mark Zuckerberg’s latest mission statement as ‘vacuous’. That was a rather rude way of saying that there is a vacuum at the heart of his rhetoric. It’s politics.
citizenkane