JavaScript 9 – struttura dei programmi – esercizi – 7

Continuo da qui finendo il capitolo, qui.

Per finire Marijn ci propone gli esercizi, vediamo… 😊
Each exercise starts with a problem description. Read that and try to solve the exercise. If you run into problems, consider reading the hints after the exercise. Full solutions to the exercises are not included in this book, but you can find them online at eloquentjavascript.net/code. If you want to learn something from the exercises, I recommend looking at the solutions only after you’ve solved the exercise, or at least after you’ve attacked it long and hard enough to have a slight headache.

Fare un triangolo con un ciclo
Write a loop that makes seven calls to console.log to output the following triangle:

#
##
###
####
#####
######
#######

It may be useful to know that you can find the length of a string by writing .length after it.

var abc = "abc";
console.log(abc.length);
// → 3

Most exercises contain a piece of code that you can modify to solve the exercise. Remember that you can click code blocks to edit them.

Io (me) sono niubbo ma ci voglio provare. Inizio con il ciclo for (file t0.js):

// t0.js - triangolo con un ciclo for

var ele = "#";
var ris = "";

for (var c = 1; c < 8; c++) {
    ris += ele;
    console.log(ris);
}

Usando while ho t1.js:

// t1.js - triangolo con un ciclo while

var ele = "#";
var ris = "";
var c = 1;

while (c < 8) {
    ris += ele;
    console.log(ris);
    c++;
}

Invece con do ecco t2.js;

// t2.js - triangolo con un ciclo do

var ele = "#";
var ris = "";
var c = 1;

do {
    ris += ele;
    console.log(ris);
    c++;
} while (c < 8);

FizzBuzz
Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print “Fizz” instead of the number, and for numbers divisible by 5 (and not 3), print “Buzz” instead.

When you have that working, modify your program to print “FizzBuzz”, for numbers that are divisible by both 3 and 5 (and still print “Fizz” or “Buzz” for numbers divisible by only one of those).

(This is actually an interview question that has been claimed to weed out a significant percentage of programmer candidates. So if you solved it, you’re now allowed to feel good about yourself.)

Due osservazioni prima di cominciare: 1) lo faccio solo per il ciclo for, tanto abbiamo visto che sono tutti tre molo simili; e 2) 100 viene un output lunghissimissimo, mi sa che metto solo la prima parte e la fine 😜

// fb.js -- FizzBuzz

var f = "Fizz";
var b = "Buzz";
var fb = f + b;

for (var c = 1; c <= 100; c++) {
    if ((c % 3 == 0) & (c % 5 == 0)) {
        console.log(fb);
        continue;
    } 
    if (c % 3 == 0) {
        console.log(f);
        continue;
    }
    if (c % 5 == 0) {
        console.log(b);
        continue;
    }
    console.log(c);
}

inizia così

per terminare così

Scacchiera
Write a program that creates a string that represents an 8×8 grid, using newline characters to separate lines. At each position of the grid there is either a space or a “#” character. The characters should form a chess board.

Passing this string to console.log should show something like this:

 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #
 # # # #
# # # #

When you have a program that generates this pattern, define a variable size = 8 and change the program so that it works for any size, outputting a grid of the given width and height.

Il problema è diverso da quello del triangolo perché conviene impostare la stringa da scrivere globale, usando il suggerimento di Marijn che si dice che “\n” è l’a-capo. Ecco il file s8.js:

// s8.js -- scacchiera 8 x 8
var b = " "; // bianco
var n = "#"; // nero #
var nl = "\n"; // a-capo
var scac = "";
var nc = 8 * 8; // numero caselle
var c = 1;
var sw = 0; // switch, spiegato nel post

while (c <= nc) {
    if (c % 2 == 0) {
        if (sw == 0) // un nerd scriverebbe !sw
            scac += n;
        else
            scac += b;
        if ((c % 8) == 0) {
            scac += nl;
            sw++
            sw %= 2;
        }    
    }
    else
        if (sw == 0) 
            scac += b;
        else
            scac += n;
    c++
}

console.log(scac);

Credo siano necessarie un po’ di spiegazioni.
Intanto dovrei perdere la pessima abitudine di usare nomi molto corti per le variabili, ma sono tutte elencate nei commenti 😜
Le righe sono composte da caselle alternate, OK, ma anche le righe devono essere alternate: se una inizia con bianco la successiva deve iniziare con nero. Questo è regolato dalla variabile sw, switch (anticamente si sarebbe chiamato flip-flop ma non sento questa parola dal secolo scorso), una variabile logica con 2 valori possibili 1 == true e 0 == false. devo resettarla alla fine di ogni linea. Ho scritto codice semplice, avrei potuto scrivere sw = !sw al posto di incrementarla e farne il modulo (resto) della divisione per 2:

Anche

if (sw == 0)
    scac += b;
else
    scac += n;

si può semplificare:

var scac = ""
var b = "o"
var n = "x"
var sw = 0
var c = 0

do {
    if (!sw)
        scac += b;
    else
        scac += n;    
    console.log(scac)
    sw = !sw
    c++
} while(c < 4)

In realtà l’esercizio chiedeva di generalizzare la dimensione della scacchiera, cosa che non ho fatto. O forse sì? Mi sa che basta mettere size al posto del numero 8 e probabilmente… forse… anzi certo! 😜

:mrgreen:

NumPy – 40 – lavorare con dati e Pandas – 1

Continuo da qui, copio qui.

One of the essential pieces of NumPy is the ability to perform quick element-wise operations, both with basic arithmetic (addition, subtraction, multiplication, etc.) and with more sophisticated operations (trigonometric functions, exponential and logarithmic functions, etc.). Pandas inherits much of this functionality from NumPy, and the ufuncs that we introduced in Computation on NumPy Arrays: Universal Functions [qui] are key to this.

Pandas includes a couple useful twists, however: for unary operations like negation and trigonometric functions, these ufuncs will preserve index and column labels in the output, and for binary operations such as addition and multiplication, Pandas will automatically align indices when passing the objects to the ufunc. This means that keeping the context of data and combining data from different sources–both potentially error-prone tasks with raw NumPy arrays–become essentially foolproof ones with Pandas. We will additionally see that there are well-defined operations between one-dimensional Series structures and two-dimensional DataFrame structures.

Ufuncs: preservazione dell’indice
Because Pandas is designed to work with NumPy, any NumPy ufunc will work on Pandas Series and DataFrame objects. Let’s start by defining a simple Series and DataFrame on which to demonstrate this:

If we apply a NumPy ufunc on either of these objects, the result will be another Pandas object with the indices preserved:

Or, for a slightly more complex calculation:

Any of the ufuncs discussed in Computation on NumPy Arrays: Universal Functions [stesso link di prima] can be used in a similar manner.

Ufuncs: allineamento dell’indice
For binary operations on two Series or DataFrame objects, Pandas will align indices in the process of performing the operation. This is very convenient when working with incomplete data, as we’ll see in some of the examples that follow.

allineamento dell’indice in Series
As an example, suppose we are combining two different data sources, and find only the top three US states by area and the top three US states by population:

Let’s see what happens when we divide these to compute the population density:

The resulting array contains the union of indices of the two input arrays, which could be determined using standard Python set arithmetic on these indices:

Any item for which one or the other does not have an entry is marked with NaN, or “Not a Number,” which is how Pandas marks missing data (see further discussion of missing data in Handling Missing Data [prossimamente]). This index matching is implemented this way for any of Python’s built-in arithmetic expressions; any missing values are filled in with NaN by default:

If using NaN values is not the desired behavior, the fill value can be modified using appropriate object methods in place of the operators. For example, calling A.add(B) is equivalent to calling A + B, but allows optional explicit specification of the fill value for any elements in A or B that might be missing:

allineamento dell’indice in DataFrame
A similar type of alignment takes place for both columns and indices when performing operations on DataFrames:

Notice that indices are aligned correctly irrespective of their order in the two objects, and indices in the result are sorted. As was the case with Series, we can use the associated object’s arithmetic method and pass any desired fill_value to be used in place of missing entries. Here we’ll fill with the mean of all values in A (computed by first stacking the rows of A):

The following table lists Python operators and their equivalent Pandas object methods:

Python Op Pandas Method(s)
+         add()
-         sub(), subtract()
*         mul(), multiply()
/         truediv(), div(), divide()
//        floordiv()
%         mod()
**        pow()

:mrgreen:

SICP – cap. 2 – Mappare liste – 25

Continuo da qui, copiando qui.

One extremely useful operation is to apply some transformation to each element in a list and generate the list of results. For instance, the following procedure scales each number in a list by a given factor:

nil l’ho definito, non c’è in Racket.

We can abstract this general idea and capture it as a common pattern expressed as a higher-order procedure, just as in 1.3 [qui]. The higher-order procedure here is called map. map takes as arguments a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list (Scheme standardly provides a map procedure that is more general than the one described here):

Now we can give a new definition of scale-list in terms of map:

map is an important construct, not only because it captures a common pattern, but because it establishes a higher level of abstraction in dealing with lists. In the original definition of scale-list, the recursive structure of the program draws attention to the element-by-element processing of the list. Defining scale-list in terms of map suppresses that level of detail and emphasizes that scaling transforms a list of elements to a list of results.
The difference between the two definitions is not that the computer is performing a different process (it isn’t) but that we think about the process differently.
In effect, map helps establish an abstraction barrier that isolates the implementation of procedures that transform lists from the details of how the elements of the list are extracted and combined. Like the barriers shown in Figure 2.1, this abstraction gives us the flexibility to change the low-level details of how sequences are implemented, while preserving the conceptual framework of operations that transform sequences to sequences. Section 2.2.3 [prossimamente] expands on this use of sequences as a framework for organizing programs.

:mrgreen:

Visto nel Web – 280

Che ora è? (quasi-cit.) è l’ora di wisto nel Web 😊

Interactive Lua code snippets in your browser with Klipse
Viebel rockz! 🚀
#:Web, Internet #:linguaggi di programmazione
::: viebel

Site survey, fatta in casa
#:Web, Internet
::: SiamoGeek

RedMonk Identifies 2017’s Most Popular Languages: JavaScript, Java, And Python
#:linguaggi di programmazione
::: Slashdot

Edge, VMWare, Safari, And Ubuntu Linux Hacked at Pwn2Own 2017
#:sicurezza, spionaggio, virus
::: Slashdot

I was wrong to dismiss Elm, and I think you probably are too
#:programmazione funzionale
::: ThePracticalDev

WikiLeaks Won’t Tell Tech Companies How To Patch CIA Zero-Days Until Demands Are Met
#:sicurezza, spionaggio, virus
::: Slashdot

Adventures in JIT compilation
Eli si lancia in un’impresa ciclopica 🚀
::: Eli Bendersky ::: Eli Bendersky

Does software performance still matter?
#:programming, codice, snippet
::: Daniel Lemire

Apple’s Next Big Thing: Augmented Reality
#:innovazioni, futuro
::: Slashdot

IBM Unveils Blockchain As a Service Based On Open Source Hyperledger Fabric Technology
#:Web, Internet
::: Slashdot

blogging principles I use
#:Web, Internet #:programming, codice, snippet
::: b0rk

Read this thread: Facebook *is already* the Internet for many, and for many reasons
#:Web, Internet #:social media
::: fabiochiusi

It’s official! GNU coreutils says stop using md5sum and sha1sum, switch to b2sum
#:sicurezza, spionaggio, virus
::: zooko

Yay, Python 3.6.1 is released! Thanks @baybryj and release team
#:linguaggi di programmazione
::: gvanrossum

Sad. The rise and fall of the “FOSS Empire of Firefox”
#:Web, Internet
::: Google+

The HP Ways: Lessons on Strategy and Culture
#:storia
::: Computer History Museum

Newly Obtained Documents Prove: Key Claim of Snowden’s Accusers Is a Fraud
#:sicurezza, spionaggio, virus
::: ericgeller

#Turkey leads the world in social media #censorship: new Twitter transparency report
#:censura
::: TurkeyBlocks

Tools for PDF modification on Fedora
probabilmente anche per Ubuntu e altre distro
#:tools, componenti software
::: lucaciavatta

Walmart Unveils ‘Store No. 8’ Tech Incubator In Silicon Valley
niente sarà più come prima; per voi ggiovani
#:innovazioni, futuro
::: Slashdot

Why American Farmers Are Hacking Their Tractors With Ukrainian Firmware
#:ditte
::: Motherboard

W3C moves to finalize DRM standardization, reclassifies suing security researchers as a feature, not a bug
#:copyright e brevetti
::: BoingBoing

5 big ways AI is rapidly invading our lives
#:artificial intelligence
::: lucaciavatta

Opening a new chapter of my work in AI – Andrew Ng
#:artificial intelligence
::: nicolaiarocci

Quand Tim Berners-Lee milite pour la protection des données personnelles
#:Web, Internet #:sicurezza, spionaggio, virus
::: LINCnil

Stack Overflow Developer Survey 2017. Treasure trove of data
#:Web, Internet #:programming, codice, snippet
::: marcelsalathe

Patents Are A Big Part Of Why We Can’t Own Nice Things
#:copyright e brevetti
::: Slashdot

Golden powers are nearly integers
scarsamente pratico ma bellissimo 💥
::: John D. Cook

GNOME 3.24 Released
#:sistemi operativi
::: Slashdot

Intel’s Bold Plan to Reinvent Computer Memory (and Keep It a Secret)
#:innovazioni, futuro
::: ulaulaman

Tiny Folding Robots Could Explore Alien Worlds
#:innovazioni, futuro
::: ulaulaman

Using Machine Learning To Generate Human-Readable News Articles
#:artificial intelligence
::: ThePracticalDev

Group-by From Scratch
#:linguaggi di programmazione
::: Pythonic Perambulations

One Year Using Go
#:linguaggi di programmazione
::: ThePracticalDev

Why does Swatch think it can take on Google and Apple?
#:ditte
::: dcavedon

Vivaldi 1.8 Web Browser Is Just Around the Corner, Will Be Based on Chromium 57
#:Web, Internet
::: MariusNestor

3 open source link shorteners
#:Web, Internet
::: lucaciavatta

Legal Affairs Committee Stops Short of Protecting User Rights
#:copyright e brevetti
::: Communia

Brazilian Facebook User Prosecuted for Posting a Joke About a Politician
#:censura
::: fabiochiusi

My Way of (Purely Functional) Programming
#:programmazione funzionale
::: ThePracticalDev

Linux Desktop is also by far the most-loved platform, with 69.6%
#:sistemi operativi
::: mattdm

Procedural CAD for Weirdos indeed!
#:lisp(s)
::: yminsky

so you want to be a wizard
#:programming, codice, snippet
::: b0rk

Google Reducing Trust In Symantec Certificates Following Numerous Slip-Ups
#:sicurezza, spionaggio, virus
::: Slashdot

Twitter Considers Premium Version After 11 Years As a Free Service
#:social media
::: Slashdot

Modern C++ and Lisp Programming Style
#:linguaggi di programmazione #:lisp(s)
::: chriskohlhepp

Why aren’t macros included in most modern programming languages?
#:linguaggi di programmazione #:lisp(s)
::: FunctorFact ::: wallingf

South Korea Finds Qualcomm Prevented Samsung From Selling Its Exynos Processors
#:copyright e brevetti
::: Slashdot

It “now consists of several websites that produce original reporting and analysis with a strong “patriotic” slant”
#:sicurezza, spionaggio, virus
::: fabiochiusi

kdb+ 3.5 released last month
#:tools, componenti software
::: Lambda the Ultimate

SICP – cap. 2 – Dati gerarchici e closure – esercizi – 24

Continuo da qui, sono qui.

Exercise 2.20: The procedures +, *, and list take arbitrary numbers of arguments. One way to define such procedures is to use define with dotted-tail notation. In a procedure definition, a parameter list that has a dot before the last parameter name indicates that, when the procedure is called, the initial parameters (if any) will have as values the initial arguments, as usual, but the final parameter’s value will be a list of any remaining arguments. For instance, given the definition

(define (f x y . z) ⟨body⟩)

the procedure f can be called with two or more arguments. If we evaluate

(f 1 2 3 4 5 6)

then in the body of f, x will be 1, y will be 2, and z will be the list (3 4 5 6). Given the definition

(define (g . w) ⟨body⟩)

the procedure g can be called with zero or more arguments. If we evaluate

(g 1 2 3 4 5 6)

then in the body of g, w will be the list (1 2 3 4 5 6).

Note:  To define f and g using lambda we would write
(define f (lambda (x y . z) ⟨body⟩))
(define g (lambda w ⟨body⟩))

Use this notation to write a procedure same-parity that takes one or more integers and returns a list of all the arguments that have the same even-odd parity as the first argument. For example,

(same-parity 1 2 3 4 5 6 7)
(1 3 5 7)

(same-parity 2 3 4 5 6 7)
(2 4 6)

Forse SICP è troppo impegnativo per me; o forse devo sviscerare il senso del testo. Ho provato a vedere se Racket (nella REPL) ha la dot-tail notation  e pare di no  NO, c’è, vedi più avanti. Allora cosa devo fare? Chissà se Bill the Lizard

OK, devo costruire la procedeura same-parity come se la notazione ci fosse.

Ma aspetta: la cosa non mi convince, provo a indagare

o, meglio ancora

e ovviamente

L’esercizio non era comunque semplice, vedi sicp-ex e Drewiki.

OK, adesso lo pubblico? devo essere onesto e raccontare quello che faccio o riportare la ricostruzione finale? Siccome il blog è mio e lo leggo solo io (kwasy) la prima opzione mi sembra quella giusta 😜

Ovviamente devo impegnarmi di più: i post di SICP sono impegnativi. Molto 👽

:mrgreen:

NumPy – 39 – indicizzazione e selezione dei dati – 2

Continuo copiando qui.

Selezione in DataFrame
Recall that a DataFrame acts in many ways like a two-dimensional or structured array, and in other ways like a dictionary of Series structures sharing the same index. These analogies can be helpful to keep in mind as we explore data selection within this structure.

DataFrame come dictionary
The first analogy we will consider is the DataFrame as a dictionary of related Series objects. Let’s return to our example of areas and populations of states:

The individual Series that make up the columns of the DataFrame can be accessed via dictionary-style indexing of the column name:

Equivalently, we can use attribute-style access with column names that are strings:

This attribute-style column access actually accesses the exact same object as the dictionary-style access:

Though this is a useful shorthand, keep in mind that it does not work for all cases! For example, if the column names are not strings, or if the column names conflict with methods of the DataFrame, this attribute-style access is not possible. For example, the DataFrame has a pop() method, so data.pop will point to this rather than the “pop” column:

In particular, you should avoid the temptation to try column assignment via attribute (i.e., use data['pop'] = z rather than data.pop = z).

Like with the Series objects discussed earlier, this dictionary-style syntax can also be used to modify the object, in this case adding a new column:

This shows a preview of the straightforward syntax of element-by-element arithmetic between Series objects; we’ll dig into this further [prossimamente].

DataFrame come array bidimensionale
As mentioned previously, we can also view the DataFrame as an enhanced two-dimensional array. We can examine the raw underlying data array using the values attribute:

With this picture in mind, many familiar array-like observations can be done on the DataFrame itself. For example, we can transpose the full DataFrame to swap rows and columns:

When it comes to indexing of DataFrame objects, however, it is clear that the dictionary-style indexing of columns precludes our ability to simply treat it as a NumPy array. In particular, passing a single index to an array accesses a row:

and passing a single “index” to a DataFrame accesses a column:

Thus for array-style indexing, we need another convention. Here Pandas again uses the loc, iloc, and ix indexers mentioned earlier. Using the iloc indexer, we can index the underlying array as if it is a simple NumPy array (using the implicit Python-style index), but the DataFrame index and column labels are maintained in the result:

Similarly, using the loc indexer we can index the underlying data in an array-like style but using the explicit index and column names:

The ix indexer allows a hybrid of these two approaches:

Keep in mind that for integer indices, the ix indexer is subject to the same potential sources of confusion as discussed for integer-indexed Series objects.

Any of the familiar NumPy-style data access patterns can be used within these indexers. For example, in the loc indexer we can combine masking and fancy indexing as in the following:

Any of these indexing conventions may also be used to set or modify values; this is done in the standard way that you might be accustomed to from working with NumPy:

To build up your fluency in Pandas data manipulation, I suggest spending some time with a simple DataFrame and exploring the types of indexing, slicing, masking, and fancy indexing that are allowed by these various indexing approaches.

Ulteriori convenzioni di indicizzazione
There are a couple extra indexing conventions that might seem at odds with the preceding discussion, but nevertheless can be very useful in practice. First, while indexing refers to columns, slicing refers to rows:

Such slices can also refer to rows by number rather than by index:

Similarly, direct masking operations are also interpreted row-wise rather than column-wise:

These two conventions are syntactically similar to those on a NumPy array, and while these may not precisely fit the mold of the Pandas conventions, they are nevertheless quite useful in practice.

:mrgreen:

JavaScript 8 – struttura dei programmi – 6

Continuo da qui, copio qui.

Aggiornare variabili rapidamente
Especially when looping, a program often needs to “update” a variable to hold a value based on that variable’s previous value.

counter = counter + 1;

JavaScript provides a shortcut for this:

counter += 1;

Similar shortcuts work for many other operators, such as result *= 2 to double result or counter -= 1 to count downward.

This allows us to shorten our counting example a little more (file s0.js).

for (var number = 0; number <= 12; number += 2)
  console.log(number);

For counter += 1 and counter -= 1, there are even shorter equivalents: counter++ and counter--.

Uso di switch
It is common for code to look like this:

if (variable == "value1") action1();
else if (variable == "value2") action2();
else if (variable == "value3") action3();
else defaultAction();

There is a construct called switch that is intended to solve such a “dispatch” in a more direct way. Unfortunately, the syntax JavaScript uses for this (which it inherited from the C/Java line of programming languages) is somewhat awkward—a chain of if statements often looks better. Here is an example:

switch (prompt("What is the weather like?")) {
  case "rainy":
    console.log("Remember to bring an umbrella.");
    break;
  case "sunny":
    console.log("Dress lightly.");
  case "cloudy":
    console.log("Go outside.");
    break;
  default:
    console.log("Unknown weather type!");
    break;
}

Sì, nel browser; ma ovviamente switch si può usare anche normalmente.

You may put any number of case labels inside the block opened by switch. The program will jump to the label that corresponds to the value that switch was given or to default if no matching value is found. It starts executing statements there, even if they’re under another label, until it reaches a break statement. In some cases, such as the “sunny” case in the example, this can be used to share some code between cases (it recommends going outside for both sunny and cloudy weather). But beware: it is easy to forget such a break, which will cause the program to execute code you do not want executed.

Uso delle Maiuscole
Variable names may not contain spaces, yet it is often helpful to use multiple words to clearly describe what the variable represents. These are pretty much your choices for writing a variable name with several words in it:

fuzzylittleturtle
fuzzy_little_turtle
FuzzyLittleTurtle
fuzzyLittleTurtle

The first style can be hard to read. Personally, I like the look of the underscores, though that style is a little painful to type. The standard JavaScript functions, and most JavaScript programmers, follow the bottom style—they capitalize every word except the first. It is not hard to get used to little things like that, and code with mixed naming styles can be jarring to read, so we will just follow this convention.

In a few cases, such as the Number function, the first letter of a variable is also capitalized. This was done to mark this function as a constructor. What a constructor is will become clear [prossimamente]. For now, the important thing is not to be bothered by this apparent lack of consistency.

Commenti
Often, raw code does not convey all the information you want a program to convey to human readers, or it conveys it in such a cryptic way that people might not understand it. At other times, you might just feel poetic or want to include some thoughts as part of your program. This is what comments are for.

A comment is a piece of text that is part of a program but is completely ignored by the computer. JavaScript has two ways of writing comments. To write a single-line comment, you can use two slash characters (//) and then the comment text after it.

var accountBalance = calculateBalance(account);
// It's a green hollow where a river sings
accountBalance.adjust();
// Madly catching white tatters in the grass.
var report = new Report();
// Where the sun on the proud mountain rings:
addToReport(accountBalance, report);
// It's a little valley, foaming like light in a glass.

A // comment goes only to the end of the line. A section of text between /* and */ will be ignored, regardless of whether it contains line breaks. This is often useful for adding blocks of information about a file or a chunk of program.

/*
 I first found this number scrawled on the back of one of
 my notebooks a few years ago. Since then, it has often
 dropped by, showing up in phone numbers and the serial
 numbers of products that I've bought. It obviously likes
 me, so I've decided to keep it.
*/
var myNumber = 11213;

:mrgreen:

NumPy – 38 – indicizzazione e selezione dei dati – 1

Continuando da qui oggi sono qui.

In passato we looked in detail at methods and tools to access, set, and modify values in NumPy arrays. These included indexing (e.g., arr[2, 1]), slicing (e.g., arr[:, 1:5]), masking (e.g., arr[arr > 0]), fancy indexing (e.g., arr[0, [1, 5]]), and combinations thereof (e.g., arr[:, [1, 5]]). Here we’ll look at similar means of accessing and modifying values in Pandas Series and DataFrame objects. If you have used the NumPy patterns, the corresponding patterns in Pandas will feel very familiar, though there are a few quirks to be aware of.

We’ll start with the simple case of the one-dimensional Series object, and then move on to the more complicated two-dimesnional DataFrame object.

Selezione in Series
As we saw in the previous section, a Series object acts in many ways like a one-dimensional NumPy array, and in many ways like a standard Python dictionary. If we keep these two overlapping analogies in mind, it will help us to understand the patterns of data indexing and selection in these arrays.

Series come dictionary
Like a dictionary, the Series object provides a mapping from a collection of keys to a collection of values:

We can also use dictionary-like Python expressions and methods to examine the keys/indices and values:

Series objects can even be modified with a dictionary-like syntax. Just as you can extend a dictionary by assigning to a new key, you can extend a Series by assigning to a new index value:

This easy mutability of the objects is a convenient feature: under the hood, Pandas is making decisions about memory layout and data copying that might need to take place; the user generally does not need to worry about these issues.

Series come array a una dimensione
A Series builds on this dictionary-like interface and provides array-style item selection via the same basic mechanisms as NumPy arrays – that is, slices, masking, and fancy indexing.

Among these, slicing may be the source of the most confusion. Notice that when slicing with an explicit index (i.e., data['a':'c']), the final index is included in the slice, while when slicing with an implicit index (i.e., data[0:2]), the final index is excluded from the slice.

Indicizzatori: loc, iloc e ix
These slicing and indexing conventions can be a source of confusion. For example, if your Series has an explicit integer index, an indexing operation such as data[1] will use the explicit indices, while a slicing operation like data[1:3] will use the implicit Python-style index.

Because of this potential confusion in the case of integer indexes, Pandas provides some special indexer attributes that explicitly expose certain indexing schemes. These are not functional methods, but attributes that expose a particular slicing interface to the data in the Series.

First, the loc attribute allows indexing and slicing that always references the explicit index:

The iloc attribute allows indexing and slicing that always references the implicit Python-style index:

A third indexing attribute, ix, is a hybrid of the two, and for Series objects is equivalent to standard []-based indexing. The purpose of the ix indexer will become more apparent in the context of DataFrame objects, which we will discuss [nel prossimo post].

One guiding principle of Python code is that “explicit is better than implicit.” The explicit nature of loc and iloc make them very useful in maintaining clean and readable code; especially in the case of integer indexes, I recommend using these both to make code easier to read and understand, and to prevent subtle bugs due to the mixed indexing/slicing convention.

:mrgreen:

SICP – cap. 2 – Dati gerarchici e closure – esercizi – 23

Continuando da qui ecco un altro esercizio, qui.

Exercise 2.19: Consider the change-counting program of 1.2.2. It would be nice to be able to easily change the currency used by the program, so that we could compute the number of ways to change a British pound, for example. As the program is written, the knowledge of the currency is distributed partly into the procedure first-denomination and partly into the procedure count-change (which knows that there are five kinds of U.S. coins). It would be nicer to be able to supply a list of coins to be used for making change.

We want to rewrite the procedure cc so that its second argument is a list of the values of the coins to use rather than an integer specifying which coins to use. We could then have lists that defined each kind of currency:

(define us-coins 
  (list 50 25 10 5 1))

(define uk-coins 
  (list 100 50 20 10 5 2 1 0.5))

We could then call cc as follows:

(cc 100 us-coins)
292

To do this will require changing the program cc somewhat. It will still have the same form, but it will access its second argument differently, as follows:

(define (cc amount coin-values)
  (cond ((= amount 0) 
         1)
        ((or (< amount 0) 
             (no-more? coin-values)) 
         0)
        (else
         (+ (cc 
             amount
             (except-first-denomination 
              coin-values))
            (cc 
             (- amount
                (first-denomination 
                 coin-values))
             coin-values)))))

Define the procedures first-denomination, except-first-denomination and no-more? in terms of primitive operations on list structures. Does the order of the list coin-values affect the answer produced by cc? Why or why not?

Uh! la prima parte è davvero immediata:

(define (no-more? coin-values)
  (if (null? coin-values) true false))

(define (except-first-denomination coin-values)
  (cdr coin-values))

(define (first-denomination coin-values)
  (car coin-values))

Metto tutto nel file coins.rkt per comodità:

Poi la seconda parte 👿 non ci provo perché poi mi deprimo 👿 passo subito a Bill the Lizard 🚀 , il mio nerd di riferimento 😉

OOPS! 😳 dubbio atroce, forse ho frainteso –niente dimostrazione matematica– devo stare più attento; vista fatta sembra banale: Finally, we’re asked if the order of the list coin-values affects the answer produced by cc. We can find out quickly enough by experiment.

We can see that the order doesn’t matter. This is because the procedure recursively evaluates every sub-list after subtracting the value of the first coin from the target amount. It doesn’t matter if that value is the smallest, largest, or even if the values are shuffled.

Interessanti le osservazioni di sicp-ex 🚀

Devo affrontare in modo diverso questi esercizi; questo è alla terza modifica con riscrittura parziale 😯

:mrgreen:

NumPy – 37 – introduzione agli oggetti Pandas – 3

Continuo da qui copiando qui.

L’oggetto Index di Pandas
We have seen here that both the Series and DataFrame objects contain an explicit index that lets you reference and modify data. This Index object is an interesting structure in itself, and it can be thought of either as an immutable array or as an ordered set (technically a multi-set, as Index objects may contain repeated values). Those views have some interesting consequences in the operations available on Index objects. As a simple example, let’s construct an Index from a list of integers:

Index come array immutabile
The Index in many ways operates like an array. For example, we can use standard Python indexing notation to retrieve values or slices:

Index objects also have many of the attributes familiar from NumPy arrays:

One difference between Index objects and NumPy arrays is that indices are immutable–that is, they cannot be modified via the normal means:

This immutability makes it safer to share indices between multiple DataFrames and arrays, without the potential for side effects from inadvertent index modification.

Index come set ordinato
Pandas objects are designed to facilitate operations such as joins across datasets, which depend on many aspects of set arithmetic. The Index object follows many of the conventions used by Python’s built-in set data structure, so that unions, intersections, differences, and other combinations can be computed in a familiar way:

These operations may also be accessed via object methods, for example

:mrgreen: