JavaScript 51 – espressioni regolari – 3

Continuo da qui, copio qui.

Il tipo Date
JavaScript has a standard object type for representing dates—or rather, points in time. It is called Date. If you simply create a date object using new, you get the current date and time (file re13.js).

console.log(new Date());

You can also create an object for a specific time (re14.js).

console.log(new Date(2010, 4, 5));
console.log(new Date(2010, 4, 5, 8, 44, 42, 940));


chissà cos’è quella data?

JavaScript uses a convention where month numbers start at zero (so December is 11), yet day numbers start at one. This is confusing and silly. Be careful.

The last four arguments (hours, minutes, seconds, and milliseconds) are optional and taken to be zero when not given.

Timestamps are stored as the number of milliseconds since the start of 1970, using negative numbers for times before 1970 (following a convention set by “Unix time”, which was invented around that time). The getTime method on a date object returns this number. It is big, as you can imagine (re15.js).

var adesso = new Date(2010, 4, 5, 20, 44, 42, 940).getTime();
console.log(adesso);
console.log(new Date(adesso));

If you give the Date constructor a single argument, that argument is treated as such a millisecond count. You can get the current millisecond count by creating a new Date object and calling getTime on it but also by calling the Date.now function.

Date objects provide methods like getFullYear, getMonth, getDate, getHours, getMinutes, and getSeconds to extract their components. There’s also getYear, which gives you a rather useless two-digit year value (such as 93 or 14).

Putting parentheses around the parts of the expression that we are interested in, we can now easily create a date object from a string (re16.js).

function findDate(string) {
  var dateTime = /(\d{1,2})-(\d{1,2})-(\d{4})/;
  var match = dateTime.exec(string);
  return new Date(Number(match[3]),
                  Number(match[2]) - 1,
                  Number(match[1]));
}
console.log(findDate("5-5-2010"));

:mrgreen:

NumPy – 89 – cos’è il machine learning – 1

Luciana Myriam Nicolosi

Continuo da qui, copio qui.

Before we take a look at the details of various machine learning methods, let’s start by looking at what machine learning is, and what it isn’t. Machine learning is often categorized as a subfield of artificial intelligence, but I find that categorization can often be misleading at first brush. The study of machine learning certainly arose from research in this context, but in the data science application of machine learning methods, it’s more helpful to think of machine learning as a means of building models of data.

Fundamentally, machine learning involves building mathematical models to help understand data. “Learning” enters the fray when we give these models tunable parameters that can be adapted to observed data; in this way the program can be considered to be “learning” from the data. Once these models have been fit to previously seen data, they can be used to predict and understand aspects of newly observed data. I’ll leave to the reader the more philosophical digression regarding the extent to which this type of mathematical, model-based “learning” is similar to the “learning” exhibited by the human brain.

Understanding the problem setting in machine learning is essential to using these tools effectively, and so we will start with some broad categorizations of the types of approaches we’ll discuss here.

OK 😎 un po’ più tranquillo 😁

Categorie del machine learning
At the most fundamental level, machine learning can be categorized into two main types: supervised learning and unsupervised learning.

  • Supervised learning involves somehow modeling the relationship between measured features of data and some label associated with the data; once this model is determined, it can be used to apply labels to new, unknown data. This is further subdivided into classification tasks and regression tasks: in classification, the labels are discrete categories, while in regression, the labels are continuous quantities. We will see examples of both types of supervised learning in the following section.
  • Unsupervised learning involves modeling the features of a dataset without reference to any label, and is often described as “letting the dataset speak for itself.” These models include tasks such as clustering and dimensionality reduction. Clustering algorithms identify distinct groups of data, while dimensionality reduction algorithms search for more succinct representations of the data. We will see examples of both types of unsupervised learning in the following section.

In addition, there are so-called semi-supervised learning methods, which falls somewhere between supervised learning and unsupervised learning. Semi-supervised learning methods are often useful when only incomplete labels are available.

Esempi qualitativi di applicazioni machine learning
To make these ideas more concrete, let’s take a look at a few very simple examples of a machine learning task. These examples are meant to give an intuitive, non-quantitative overview of the types of machine learning tasks we will be looking at in this chapter. In later sections, we will go into more depth regarding the particular models and how they are used. For a preview of these more technical aspects, you can find the Python source that generates the following figures in the Appendix: Figure Code.

classificazione: predire etichette discrete
We will first take a look at a simple classification task, in which you are given a set of labeled points and want to use these to classify some unlabeled points.

Per compltezza copio anche il codice disponibile all’URL indicato. Notare che manca l’istruizione import matplotlib.pyplot as plt.

from sklearn.datasets.samples_generator import make_blobs
from sklearn.svm import SVC

# create 50 separable points
X, y = make_blobs(n_samples=50, centers=2,
                  random_state=0, cluster_std=0.60)

# fit the support vector classifier model
clf = SVC(kernel='linear')
clf.fit(X, y)

# create some new points to predict
X2, _ = make_blobs(n_samples=80, centers=2,
                   random_state=0, cluster_std=0.80)
X2 = X2[50:]

# predict the labels
y2 = clf.predict(X2)

# plot the data
import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 6))

# common plot formatting for below
def format_plot(ax, title):
    ax.xaxis.set_major_formatter(plt.NullFormatter())
    ax.yaxis.set_major_formatter(plt.NullFormatter())
    ax.set_xlabel('feature 1', color='gray')
    ax.set_ylabel('feature 2', color='gray')
    ax.set_title(title, color='gray')

format_plot(ax, 'Input Data')    
point_style = dict(cmap='Paired', s=50)
ax.scatter(X[:, 0], X[:, 1], c=y, **point_style)

ax.axis([-1, 4, -2, 7])

fig.savefig('no868.png')

Here we have two-dimensional data: that is, we have two features for each point, represented by the (x,y) positions of the points on the plane. In addition, we have one of two class labels for each point, here represented by the colors of the points. From these features and labels, we would like to create a model that will let us decide whether a new point should be labeled “blue” or “red.”

There are a number of possible models for such a classification task, but here we will use an extremely simple one. We will make the assumption that the two groups can be separated by drawing a straight line through the plane between them, such that points on each side of the line fall in the same group. Here the model is a quantitative version of the statement “a straight line separates the classes”, while the model parameters are the particular numbers describing the location and orientation of that line for our data. The optimal values for these model parameters are learned from the data (this is the “learning” in machine learning), which is often called training the model.

The following figure shows a visual representation of what the trained model looks like for this data:

from sklearn.datasets.samples_generator import make_blobs
from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt

# common plot formatting for below
def format_plot(ax, title):
    ax.xaxis.set_major_formatter(plt.NullFormatter())
    ax.yaxis.set_major_formatter(plt.NullFormatter())
    ax.set_xlabel('feature 1', color='gray')
    ax.set_ylabel('feature 2', color='gray')
    ax.set_title(title, color='gray')

# create 50 separable points
X, y = make_blobs(n_samples=50, centers=2,
                  random_state=0, cluster_std=0.60)

# fit the support vector classifier model 
clf = SVC(kernel='linear')
clf.fit(X, y)

# Get contours describing the model
xx = np.linspace(-1, 4, 10)
yy = np.linspace(-2, 7, 10)
xy1, xy2 = np.meshgrid(xx, yy)
Z = np.array([clf.decision_function([t])
              for t in zip(xy1.flat, xy2.flat)]).reshape(xy1.shape)

# plot points and model
fig, ax = plt.subplots(figsize=(8, 6))
point_style = dict(cmap='Paired', s=50)
line_style = dict(levels = [-1.0, 0.0, 1.0],
                  linestyles = ['dashed', 'solid', 'dashed'],
                  colors = 'gray', linewidths=1)
ax.scatter(X[:, 0], X[:, 1], c=y, **point_style)
ax.contour(xy1, xy2, Z, **line_style)

# format plot
format_plot(ax, 'Model Learned from Input Data')
ax.axis([-1, 4, -2, 7])

fig.savefig('np869.png')

Now that this model has been trained, it can be generalized to new, unlabeled data. In other words, we can take a new set of data, draw this model line through it, and assign labels to the new points based on this model. This stage is usually called prediction. See the following figure:

from sklearn.datasets.samples_generator import make_blobs
from sklearn.svm import SVC
import numpy as np
import matplotlib.pyplot as plt

# common plot formatting for below
def format_plot(ax, title):
    ax.xaxis.set_major_formatter(plt.NullFormatter())
    ax.yaxis.set_major_formatter(plt.NullFormatter())
    ax.set_xlabel('feature 1', color='gray')
    ax.set_ylabel('feature 2', color='gray')
    ax.set_title(title, color='gray')

# create 50 separable points
X, y = make_blobs(n_samples=50, centers=2,
                  random_state=0, cluster_std=0.60)

# fit the support vector classifier model 
clf = SVC(kernel='linear')
clf.fit(X, y)

# Get contours describing the model
xx = np.linspace(-1, 4, 10)
yy = np.linspace(-2, 7, 10)
xy1, xy2 = np.meshgrid(xx, yy)
Z = np.array([clf.decision_function([t])
              for t in zip(xy1.flat, xy2.flat)]).reshape(xy1.shape)

# create 50 separable points
X, y = make_blobs(n_samples=50, centers=2,
                  random_state=0, cluster_std=0.60)

# fit the support vector classifier model
clf = SVC(kernel='linear')
clf.fit(X, y)

# create some new points to predict
X2, _ = make_blobs(n_samples=80, centers=2,
                   random_state=0, cluster_std=0.80)
X2 = X2[50:]

# predict the labels
y2 = clf.predict(X2)

point_style = dict(cmap='Paired', s=50)
line_style = dict(levels = [-1.0, 0.0, 1.0],
                  linestyles = ['dashed', 'solid', 'dashed'],
                  colors = 'gray', linewidths=1)

# plot the results
fig, ax = plt.subplots(1, 2, figsize=(16, 6))
fig.subplots_adjust(left=0.0625, right=0.95, wspace=0.1)

ax[0].scatter(X2[:, 0], X2[:, 1], c='gray', **point_style)
ax[0].axis([-1, 4, -2, 7])

ax[1].scatter(X2[:, 0], X2[:, 1], c=y2, **point_style)
ax[1].contour(xy1, xy2, Z, **line_style)
ax[1].axis([-1, 4, -2, 7])

format_plot(ax[0], 'Unknown Data')
format_plot(ax[1], 'Predicted Labels')

fig.savefig('np870.png')

This is the basic idea of a classification task in machine learning, where “classification” indicates that the data has discrete class labels. At first glance this may look fairly trivial: it would be relatively easy to simply look at this data and draw such a discriminatory line to accomplish this classification. A benefit of the machine learning approach, however, is that it can generalize to much larger datasets in many more dimensions.

For example, this is similar to the task of automated spam detection for email; in this case, we might use the following features and labels:

  • feature 1, feature 2, etc. →  normalized counts of important words or phrases (“Viagra”, “Nigerian prince”, etc.)
  • label → “spam” or “not spam”

For the training set, these labels might be determined by individual inspection of a small representative sample of emails; for the remaining emails, the label would be determined using the model. For a suitably trained classification algorithm with enough well-constructed features (typically thousands or millions of words or phrases), this type of approach can be very effective. We will see an example of such text-based classification in In Depth: Naive Bayes Classification [prossimamente].

Some important classification algorithms that we will discuss in more detail are Gaussian naive Bayes (see In Depth: Naive Bayes Classification [prossimamente]), support vector machines (see In-Depth: Support Vector Machines [prossimamente]), and random forest classification (see In-Depth: Decision Trees and Random Forests [prossimamente]).

Pausa 😯 poi si continua 😊

:mrgreen:

JavaScript 50 – espressioni regolari – 2

Continuo da qui, copio qui.

Raggruppare sottoespressioni
To use an operator like * or + on more than one element at a time, you can use parentheses. A part of a regular expression that is enclosed in parentheses counts as a single element as far as the operators following it are concerned (file re7.js).

var cartoonCrying = /boo+(hoo+)+/i;
console.log(cartoonCrying.test("Boohoooohoohooo"));

The first and second + characters apply only to the second o in boo and hoo, respectively. The third + applies to the whole group (hoo+), matching one or more sequences like that.

The i at the end of the expression in the previous example makes this regular expression case insensitive, allowing it to match the uppercase B in the input string, even though the pattern is itself all lowercase.

Risultati (matches) e gruppi
The test method is the absolute simplest way to match a regular expression. It tells you only whether it matched and nothing else. Regular expressions also have an exec (execute) method that will return null if no match was found and return an object with information about the match otherwise (re8.js).

var match = /\d+/.exec("one two 100");
console.log(match);
console.log(match.index);

Notare che la versione corrente da un risultato diverso; per ottenere il risultato di Marijn usare (re9.js)

var match = /\d+/.exec("one two 100");
console.log(match[0]);

Inoltre console.log –ma in realtà JavaScript– vede che la stringa è un numero e omette gli apici 😡

An object returned from exec has an index property that tells us where in the string the successful match begins. Other than that, the object looks like (and in fact is) an array of strings, whose first element is the string that was matched—in the previous example, this is the sequence of digits that we were looking for.

String values have a match method that behaves similarly (re10.js)

console.log("one two 100".match(/\d+/));
console.log("one two 100".match(/\d+/)[0]);

Stessa situazione di prima; dovremo conviverci, con possibili problemi per il codice vecchio 😡

Vero che c’è la funzione typeof ma non sempre aiuta.

When the regular expression contains subexpressions grouped with parentheses, the text that matched those groups will also show up in the array. The whole match is always the first element. The next element is the part matched by the first group (the one whose opening parenthesis comes first in the expression), then the second group, and so on (re11.js).

var quotedText = /'([^']*)'/;
console.log(quotedText.exec("she said 'hello'"));
console.log("disperazione qui 👿");
console.log(quotedText.exec("she said 'hello'")[0]);

When a group does not end up being matched at all (for example, when followed by a question mark), its position in the output array will hold undefined. Similarly, when a group is matched multiple times, only the last match ends up in the array (re12).

console.log(/bad(ly)?/.exec("bad"));
console.log(/(\d)+/.exec("123"));

Groups can be useful for extracting parts of a string. If we don’t just want to verify whether a string contains a date but also extract it and construct an object that represents it, we can wrap parentheses around the digit patterns and directly pick the date out of the result of exec.

But first, a brief detour [prossimo post], in which we discuss the preferred way to store date and time values in JavaScript.

:mrgreen:

NumPy – 88 – machine learning

Continuo da qui, copio qui.

L’ultimo capitolo del Notebook di Jake, diversi prossimi posts, è –come dire: sapete che sono vecchio; ma anche incosciente, avventuroso, parto 🤖

In many ways, machine learning is the primary means by which data science manifests itself to the broader world. Machine learning is where these computational and algorithmic skills of data science meet the statistical thinking of data science, and the result is a collection of approaches to inference and data exploration that are not about effective theory so much as effective computation.

The term “machine learning” is sometimes thrown around as if it is some kind of magic pill: apply machine learning to your data, and all your problems will be solved! As you might expect, the reality is rarely this simple. While these methods can be incredibly powerful, to be effective they must be approached with a firm grasp of the strengths and weaknesses of each method, as well as a grasp of general concepts such as bias and variance, overfitting and underfitting, and more.

This chapter will dive into practical aspects of machine learning, primarily using Python’s Scikit-Learn package. This is not meant to be a comprehensive introduction to the field of machine learning; that is a large subject and necessitates a more technical approach than we take here. Nor is it meant to be a comprehensive manual for the use of the Scikit-Learn package (for this, you can refer to the resources listed in Further Machine Learning Resources [prossimamente]). Rather, the goals of this chapter are:

  • To introduce the fundamental vocabulary and concepts of machine learning.
  • To introduce the Scikit-Learn API and show some examples of its use.
  • To take a deeper dive into the details of several of the most important machine learning approaches, and develop an intuition into how they work and when and where they are applicable.

Much of this material is drawn from the Scikit-Learn tutorials and workshops I have given on several occasions at PyCon, SciPy, PyData, and other conferences. Any clarity in the following pages is likely due to the many workshop participants and co-instructors who have given me valuable feedback on this material over the years!

Finally, if you are seeking a more comprehensive or technical treatment of any of these subjects, I’ve listed several resources and references in Further Machine Learning Resources [prossimamente].

Panico? 😯 un po’😡  no, niente panico, si parte 😎

:mrgreen:

Python, dice Jake

Python, si può usare, anche per cose non banali, dice Jake VanderPlas. Si presenta così:

My name is Jake VanderPlas. I am an astronomer by training, and currently am the director of research in physical sciences at the University of Washington’s eScience Institute, where I spend time on research, teaching, and developing software tools for science.

Jake è l’autore del libro su Matplotlib che sto studiando (e copiando sul blog, se non faccio gli esercizi non riesco ad avere chiaro quello che si dice e Jake è ottimo come docente). L’avevo già incontrato nel Web da tempo ma se avessi visto prima Matplotlib forse sarei riuscito a proporlo con maggior forza in almeno un’occasione –acqua passata– dove si usa Octave (quasi per tutto) dicendo che è MatLab. Curiosità mia: in certi ambienti devi dire che hai usato X per fare Y perché tutti usano X e se usi Z e lo confessi passi per eretico. Io che sono vecchio ho visto passare di moda tanti Xs, per esempio il VMS e il Fortran (qualcuno usa ancora il Fortran, il VMS pare sia rimasto solo per la gestione dei missili nucleari americani, sempre se è vero; di sicuro c’è una versione aggiornata).

Jake usa Python, ha un blog, Pythonic Perambulations.

È autore di almeno due testi altamente raccomandabili:

Disponibili free ontehtoobz in vari formati; io consiglio le versioni dove si può copiare (testo e codice); almeno finché l’Autore non si arrabbierà e –non oso pensarci 👿

Si è appena conclusa la convention PyCon 2017 della quale mi sono perso (tra le altre cose) questi interventi:

E adesso dovrei fare come dice Jake: Next up: three weeks of watching videos of all the talks I missed 😎

Chissà se riuscirò a convertire qualcuno? o almeno a indurre qualcuno a dare uno sguardo agli URLs proposti; a guardare bene si trova anche roba JavaScript 😊

:mrgreen:

JavaScript 49 – espressioni regolari – 1

Continuo da qui, copio qui.

Sì, lo so, RE, RegExp, Espressioni Regolari sono una brutta bestia; ma si possono domare 😊 almeno nella misura in cui serve 😊

Programming tools and techniques survive and spread in a chaotic, evolutionary way. It’s not always the pretty or brilliant ones that win but rather the ones that function well enough within the right niche—for example, by being integrated with another successful piece of technology.

In this chapter, I will discuss one such tool, regular expressions. Regular expressions are a way to describe patterns in string data. They form a small, separate language that is part of JavaScript and many other languages and tools.

Regular expressions are both terribly awkward and extremely useful. Their syntax is cryptic, and the programming interface JavaScript provides for them is clumsy. But they are a powerful tool for inspecting and processing strings. Properly understanding regular expressions will make you a more effective programmer.

Creare un’espressione regolare
A regular expression is a type of object. It can either be constructed with the RegExp constructor or written as a literal value by enclosing the pattern in forward slash (/) characters.

var re1 = new RegExp("abc");
var re2 = /abc/;

Both of these regular expression objects represent the same pattern: an a character followed by a b followed by a c.

When using the RegExp constructor, the pattern is written as a normal string, so the usual rules apply for backslashes.

The second notation, where the pattern appears between slash characters, treats backslashes somewhat differently. First, since a forward slash ends the pattern, we need to put a backslash before any forward slash that we want to be part of the pattern. In addition, backslashes that aren’t part of special character codes (like \n) will be preserved, rather than ignored as they are in strings, and change the meaning of the pattern. Some characters, such as question marks and plus signs, have special meanings in regular expressions and must be preceded by a backslash if they are meant to represent the character itself.

var eighteenPlus = /eighteen\+/;

Knowing precisely what characters to backslash-escape when writing regular expressions requires you to know every character with a special meaning. For the time being, this may not be realistic, so when in doubt, just put a backslash before any character that is not a letter, number, or whitespace.

Test per le espressioni
Regular expression objects have a number of methods. The simplest one is test. If you pass it a string, it will return a Boolean telling you whether the string contains a match of the pattern in the expression (file re0.js).

console.log(/abc/.test("abcde"));
console.log(/abc/.test("abxde"));
console.log(/pro/.test("prova"));
console.log(/pro/.test("Prova"));
console.log(/pro/.test("PROVA"));
console.log(/pro/.test("Altra prova"));

A regular expression consisting of only nonspecial characters simply represents that sequence of characters. If abc occurs anywhere in the string we are testing against (not just at the start), test will return true.

Corrispondenza con un set di caratteri
Finding out whether a string contains abc could just as well be done with a call to indexOf. Regular expressions allow us to go beyond that and express more complicated patterns.

Say we want to match any number. In a regular expression, putting a set of characters between square brackets makes that part of the expression match any of the characters between the brackets.

Both of the following expressions match all strings that contain a digit (re1.js):

console.log(/[0123456789]/.test("siamo nel 2017"));
console.log(/[0-9]/.test("siamo nel 2017"));

Within square brackets, a dash (-) between two characters can be used to indicate a range of characters, where the ordering is determined by the character’s Unicode number. Characters 0 to 9 sit right next to each other in this ordering (codes 48 to 57), so [0-9] covers all of them and matches any digit.

There are a number of common character groups that have their own built-in shortcuts. Digits are one of them: \d means the same thing as [0-9].

\d 	Any digit character
\w 	An alphanumeric character (“word character”)
\s 	Any whitespace character (space, tab, newline, and similar)
\D 	A character that is not a digit
\W 	A nonalphanumeric character
\S 	A nonwhitespace character
. 	Any character except for newline

So you could match a date and time format like 20-05-2017 03:14 with the following expression (re2.js):

var dateTime = /\d\d-\d\d-\d\d\d\d \d\d:\d\d/;
console.log(dateTime.test("20-05-2017 03:14"));
console.log(dateTime.test("20-may-2017 03:14"));

That looks completely awful, doesn’t it? It has way too many backslashes, producing background noise that makes it hard to spot the actual pattern expressed. We’ll see a slightly improved version of this expression later.

These backslash codes can also be used inside square brackets. For example, [\d.] means any digit or a period character. But note that the period itself, when used between square brackets, loses its special meaning. The same goes for other special characters, such as +.

To invert a set of characters—that is, to express that you want to match any character except the ones in the set—you can write a caret (^) character after the opening bracket (re3.js).

var notBinary = /[^01]/;
console.log(notBinary.test("1100100010100110"));
console.log(notBinary.test("1100100010200110"));

(guarda bene c’è un 2 😯)

Ripetere parte di una sequenza
We now know how to match a single digit. What if we want to match a whole number—a sequence of one or more digits?

When you put a plus sign (+) after something in a regular expression, it indicates that the element may be repeated more than once. Thus, /\d+/ matches one or more digit characters (re4.js).

console.log(/'\d+'/.test("'123'"));
console.log(/'\d+'/.test("''"));
console.log(/'\d*'/.test("'123'"));
console.log(/'\d*'/.test("''"));

The star (*) has a similar meaning but also allows the pattern to match zero times. Something with a star after it never prevents a pattern from matching—it’ll just match zero instances if it can’t find any suitable text to match.

A question mark makes a part of a pattern “optional”, meaning it may occur zero or one time. In the following example, the u character is allowed to occur, but the pattern also matches when it is missing (re5.js).

var neighbor = /neighbou?r/;
console.log(neighbor.test("neighbour"));
console.log(neighbor.test("neighbor"));

To indicate that a pattern should occur a precise number of times, use curly braces. Putting {4} after an element, for example, requires it to occur exactly four times. It is also possible to specify a range this way: {2,4} means the element must occur at least twice and at most four times.

Here is another version of the date and time pattern that allows both single- and double-digit days, months, and hours. It is also slightly more readable (re6.js)

var dateTime = /\d{1,2}-\d{1,2}-\d{4} \d{1,2}:\d{2}/;
console.log(dateTime.test("20-5-2017 9:15"));

Pausa 😊 ma mica finisce qui.

:mrgreen:

NumPy – 87 – ulteriori risorse Matplotlib

Continuo da qui, copio qui.

Jake è saggio, ecco qui… 😁
A single chapter in a book can never hope to cover all the available features and plot types available in Matplotlib. As with other packages we’ve seen, liberal use of IPython’s tab-completion and help functions (see Help and Documentation in IPython [qui]) can be very helpful when exploring Matplotlib’s API. In addition, Matplotlib’s online documentation can be a helpful reference. See in particular the Matplotlib gallery linked on that page: it shows thumbnails of hundreds of different plot types, each one linked to a page with the Python code snippet used to generate it. In this way, you can visually inspect and learn about a wide range of different plotting styles and visualization techniques.

For a book-length treatment of Matplotlib, I would recommend Interactive Applications Using Matplotlib, written by Matplotlib core developer Ben Root.

Altre librerie grafiche per Python
Although Matplotlib is the most prominent Python visualization library, there are other more modern tools that are worth exploring as well. I’ll mention a few of them briefly here:

  • Bokeh is a JavaScript visualization library with a Python frontend that creates highly interactive visualizations capable of handling very large and/or streaming datasets. The Python front-end outputs a JSON data structure that can be interpreted by the Bokeh JS engine.
  • Plotly is the eponymous open source product of the Plotly company, and is similar in spirit to Bokeh. Because Plotly is the main product of a startup, it is receiving a high level of development effort. Use of the library is entirely free.
  • Vispy is an actively developed project focused on dynamic visualizations of very large datasets. Because it is built to target OpenGL and make use of efficient graphics processors in your computer, it is able to render some quite large and stunning visualizations.
  • Vega and Vega-Lite are declarative graphics representations, and are the product of years of research into the fundamental language of data visualization. The reference rendering implementation is JavaScript, but the API is language agnostic. There is a Python API under development in the Altair package. Though as of summer 2016 it’s not yet fully mature, I’m quite excited for the possibilities of this project to provide a common reference point for visualization in Python and other languages.

The visualization space in the Python community is very dynamic, and I fully expect this list to be out of date as soon as it is published. Keep an eye out for what’s coming in the future!

Uh! 😯 devo indagare, con l’aiuto –se possibile– di Dirk 😜

:mrgreen:

SICP – cap. 2 – Sequenze come interfacce convenzionali – 40

Continuo da qui, copio qui.

Sequenze come interfacce convenzionali
In working with compound data, we’ve stressed how data abstraction permits us to design programs without becoming enmeshed in the details of data representations, and how abstraction preserves for us the flexibility to experiment with alternative representations. In this section, we introduce another powerful design principle for working with data structures—the use of conventional interfaces.

In 1.3 [qui] we saw how program abstractions, implemented as higher-order procedures, can capture common patterns in programs that deal with numerical data. Our ability to formulate analogous operations for working with compound data depends crucially on the style in which we manipulate our data structures. Consider, for example, the following procedure, analogous to the count-leaves procedure of 2.2.2 [qui], which takes a tree as argument and computes the sum of the squares of the leaves that are odd:

(define (sum-odd-squares tree)
  (cond ((null? tree) 0)
        ((not (pair? tree))
         (if (odd? tree) (square tree) 0))
        (else (+ (sum-odd-squares 
                  (car tree))
                 (sum-odd-squares 
                  (cdr tree))))))

On the surface, this procedure is very different from the following one, which constructs a list of all the even Fibonacci numbers fib(k) , where k is less than or equal to a given integer n:

(define (even-fibs n)
  (define (next k)
    (if (> k n)
        nil
        (let ((f (fib k)))
          (if (even? f)
              (cons f (next (+ k 1)))
              (next (+ k 1))))))
  (next 0))

even-fibs richiede un po’ d’attenzione, ecco

Despite the fact that these two procedures are structurally very different, a more abstract description of the two computations reveals a great deal of similarity. The first program

  • enumerates the leaves of a tree;
  • filters them, selecting the odd ones;
  • squares each of the selected ones; and
  • accumulates the results using +, starting with 0.

The second program

  • enumerates the integers from 0 to n ;
  • computes the Fibonacci number for each integer;
  • filters them, selecting the even ones; and
  • accumulates the results using cons, starting with the empty list.

A signal-processing engineer would find it natural to conceptualize these processes in terms of signals flowing through a cascade of stages, each of which implements part of the program plan, as shown in Figure 2.7. In sum-odd-squares, we begin with an enumerator, which generates a “signal” consisting of the leaves of a given tree. This signal is passed through a filter, which eliminates all but the odd elements. The resulting signal is in turn passed through a map, which is a “transducer” that applies the square procedure to each element. The output of the map is then fed to an accumulator, which combines the elements using +, starting from an initial 0. The plan for even-fibs is analogous.

Unfortunately, the two procedure definitions above fail to exhibit this signal-flow structure. For instance, if we examine the sum-odd-squares procedure, we find that the enumeration is implemented partly by the null? and pair? tests and partly by the tree-recursive structure of the procedure. Similarly, the accumulation is found partly in the tests and partly in the addition used in the recursion. In general, there are no distinct parts of either procedure that correspond to the elements in the signal-flow description. Our two procedures decompose the computations in a different way, spreading the enumeration over the program and mingling it with the map, the filter, and the accumulation. If we could organize our programs to make the signal-flow structure manifest in the procedures we write, this would increase the conceptual clarity of the resulting code.

Continua, panicosamente 😯

:mrgreen:

Visto nel Web – 288

Non sono io, è l’algoritmo 👿 ma intanto ecco cosa ho wisto nel Web 😯

Did The UK Police Hire Foreigners To Hack Hundreds of Activists?
#:sicurezza, spionaggio, virus
::: Slashdot

Researchers Find New Version Of WanaDecrypt0r Ransomware Without A Kill Switch
#:sicurezza, spionaggio, virus
::: Slashdot

Aerei, macchine e perdite di controllo
#:artificial intelligence
::: Daniela Ovadia

7 habits to become a better software engineer
#:programming, codice, snippet
::: ThePracticalDev

Vi siete beccati #Wannacry? Ve lo meritate!
#:sicurezza, spionaggio, virus
::: lastknight ::: palinurosec

Demystifying the PageRank Algorithm
bravo Sishaar 🚀
#:algoritmi, codice #:Web, Internet
::: ThePracticalDev

Quick, Theresa, weaken encryption and spy on more people to thwart it!
#:sicurezza, spionaggio, virus
::: aral

Buongiorno da “A bit of [hi]story”, Torino Mirafiori
#:storia
::: Facebook

My All-Time Favorite Demonstration of a Cross-Site Scripting Attack
#:sicurezza, spionaggio, virus
::: ThePracticalDev

2 most clear+helpful posts of my grad career
e… sì, Jake rockz
#:programming, codice, snippet
::: zrsailer

Microsoft Wants To Monitor Your Workplace With AI, Computer Vision and the Cloud
#:innovazioni, futuro
::: Slashdot

È come predicare nel deserto) #WannaCry e la lezione inascoltata
#:sicurezza, spionaggio, virus
::: dcavedon

Governments don’t update Windows due to LICENSING fees
#:sicurezza, spionaggio, virus #:free open source software
::: harryhalpin

EFF Warns Most Of Intel’s Chipsets Contain ‘A Security Hazard’
#:sicurezza, spionaggio, virus
::: Slashdot

Microsoft Blasts Spy Agencies For Leaked Exploits Used By WanaDecrypt0r
#:sicurezza, spionaggio, virus
::: Slashdot ::: Snowden

UK Group Fights Arrest Over Refusing To Surrender Passwords At The Border
#:sicurezza, spionaggio, virus
::: Slashdot

What you need to know about JSON in MySQL
#:programming, codice, snippet
::: lucaciavatta

[Racket] Solar sim
#:lisp(s)
::: pastebin

Introspected.JS  – Objects without secrets!
#:programming, codice, snippet
::: WebReflection

A New Kind of Science (15th anniversary today) just became free, open, and high-res on the web!
Stephen Wolfram
#:manuali, how to
::: stephen_wolfram

A bit of history
#:innovazioni, futuro
::: Facebook

Disney Chief Bob Iger Says Hackers Claim To Have Stolen Upcoming Movie
#:sicurezza, spionaggio, virus
::: Slashdot

20 yrs ago today Amazon went public, and it’s now worth two Walmarts
#:economia
::: MIT_CSAIL

This NSA-enabled hospital attack established instant consensus across politics that funding offense over defense was a mistake. We need it
#:sicurezza, spionaggio, virus
::: Snowden

The Quick vs. the Strong: Commentary on Cory Doctorow’s Walkaway
#:innovazioni, futuro
::: schneierblog

Errori da non fare quando si rappresentano graficamente dei dati
non so come classificarlo; ma vero (e sarebbe bello se potessi aggiungere “elementare”
::: scinet_it

Quando un’app diventa una killer application
#:dispositivi mobili
::: SergioGridelli

Very interesting. AI acting on behalf of individuals is probably an enormous market
#:sicurezza, spionaggio, virus
::: marcelsalathe

WikiLeaks Dump Reveals CIA Malware That Can Sabotage User Software
#:sicurezza, spionaggio, virus
::: Slashdot

It is with great sadness that I inform the CS community of the passing of Alain Colmerauer, AI specialist and father of Prolog. RIP Alain
#:protagonisti
::: fbenhamou

The inaugural issue of Internet Histories
#:manuali, how to
::: jr_carpenter

Hackers Aligned With Vietnam Government Are Attacking Foreign Companies
#:sicurezza, spionaggio, virus
::: Slashdot

Group Linked To NSA Spy Leaks Threatens Sale of New Tech Secrets
#:sicurezza, spionaggio, virus
::: Slashdot

Microsoft Build 2017: riflessioni sulla strada del ritorno
#:ditte #:sistemi operativi
::: Grab the Blaster

Great news everyone! WCry 2.0 functions PERFECTLY under Wine
::: hackerfantastic

sending e-mails from your app
#:Web, Internet
::: The Ubuntu Incident

Big Banks Will Fall First To AI, China’s Most Famous VC Predicts
#:artificial intelligence
::: Slashdot

Microsoft Commits $5 Million To ‘Landmark’ United Nations Technology Partnership
#:ditte
::: Slashdot

European Privacy Regulators Take Coordinated Action Against Facebook
#:sicurezza, spionaggio, virus #:ditte
::: Slashdot ::: fabiochiusi ::: vestager

HPE Unveils The Machine, a Single-Memory Computer Capable of Addressing 160 Terabytes
#:innovazioni, futuro
::: Slashdot

The Tech Sector Is Leaving the Rest of the US Economy In Its Dust
#:economia
::: Slashdot

Così il digitale e lo smartphone hanno salvato la televisione
io non conto; non la guardo quasi mai
#:media
::: datajournalism.it

Malware Case Is Major Blow for the N.S.A
#:sicurezza, spionaggio, virus
::: fabiochiusi

Google Takes Another Shot At Making Android Great On Low-Budget Smartphones
#:dispositivi mobili
::: Slashdot

Android Now Supports the Kotlin Programming Language
#:linguaggi di programmazione
::: Slashdot ::: ThePracticalDev ::: _mchav ::: ThePracticalDev

What @Twitter showed me vs what I saw…
#:social media
::: kentcdodds ::: madbob

arriva dagli USA una conferma della tenuta giuridica delle licenze open source
#:free open source software
::: guidoscorza

Current status: Falling in love with Visual Studio Code
#:tools, componenti software
::: lunaryorn

Dunque il Senato Usa ha approvato l’uso della app cifrata Signal per il personale
::: carolafrediani

Any Half-Decent Hacker Could Break Into Mar-a-Lago
#:sicurezza, spionaggio, virus
::: Slashdot

This is What the Firefox Photon Redesign Looks like on Linux
#:Web, Internet
::: dcavedon

Net Neutrality Goes Down in Flames as FCC Votes To Kill Title II Rules
#:Web, Internet
::: Slashdot ::: johnmdonnelly

Any Half-Decent Hacker Could Break Into Mar-a-Lago
#:sicurezza, spionaggio, virus
::: Slashdot

‘WannaCry Makes an Easy Case For Linux’
#:sicurezza, spionaggio, virus
::: Slashdot

Windows XP PCs Infected By WannaCry Can Be Decrypted Without Paying Ransom
buone notizie; ma poi cambiate tutto
#:sicurezza, spionaggio, virus
::: Slashdot

Font Sharing Site DaFont Has Been Hacked, Exposing Thousands of Accounts
#:sicurezza, spionaggio, virus
::: Slashdot

Google To Launch a Jobs Search Engine In the US
#:tools, componenti software
::: Slashdot

Apple Is Lobbying Against Your Right To Repair iPhones, New York State Records Confirm
#:ditte
::: Slashdot ::: Symbo1ics

Create websites & apps simply by writing HTML, w/Mavo
#:Web, Internet
::: MIT_CSAIL

Red Hat #OpenStack Platform 11 is now available. Great team work!
#:sistemi operativi
::: nijaba

Maybe the network is the computer after all?
::: doughellmann

Friendly reminder that everyone’s code is broken, not just yours
#:sicurezza, spionaggio, virus
::: zeeg

Facebook’s meteoric five-year climb as a public company in five charts
#:ditte
::: fabiochiusi

So, this makes me happy. RT @whitequark: there’s going to be a production-quality LLVM Fortran frontend soon
#:linguaggi di programmazione
::: wallingf

I feel like I’m back in the 90s – I’m downloading a file that is going to take 90+ hours to download
#:Web, Internet
::: astrofrog

Conky Harmattan Adds an Interactive Setup Tool
#:tools, componenti software
::: lucaciavatta

On the heels of Google’s @kotlin announcement we made it available on @replit
#:linguaggi di programmazione #:Web, Internet
::: amasad

5 reasons the D programming language is a great choice for development
#:linguaggi di programmazione
::: lucaciavatta

History of Emacs and vi Keys
#:storia
::: blockloop

No technology under control of the police will be used for police accountability
#:sicurezza, spionaggio, virus
::: NabilHassein

Facebook to Vestager: Let’s be friends
l’Europa…
#:ditte
::: fabiochiusi

Microsoft beats Google and Amazon to announce first African data centers!
#:ditte
::: shanselman

Facebook HQ has a rooftop park, napping pods, staff chiropractors. But per bank regs’ formulas, this is a poor area
#:ditte
::: dseetharaman

Theresa May Wants To Regulate The Internet
#:Web, Internet #:censura
::: fabiochiusi

We may have just won the crypto war, says Bruce
#:sicurezza, spionaggio, virus
::: fbajak

Here is Zuck’s “open and connected” world
#:censura
::: fabiochiusi

Science and Technology links (May 18th, 2017)
#:elenco links
::: Daniel Lemire

ThuderDodo
#:Web, Internet
::: MadBob

Visual Studio Code is now available as a snap on Ubuntu
#:tools, componenti software #:programming, codice, snippet
::: lucaciavatta

Almost All WannaCry Victims Were Running Windows 7
#:sicurezza, spionaggio, virus
::: Slashdot

London City First In UK To Get Remote Air Traffic Control
#:innovazioni, futuro
::: Slashdot

UK Conservatives Pledge To Create Government-Controlled Internet
#:Web, Internet
::: Slashdot

#WannaCry Ransomware Decryption Tool Released; Unlock Your Files Without Paying Ransom
#:sicurezza, spionaggio, virus
::: security_wang

Wow, TIL dask can be used as a drop-in for sklearn’s joblib to automatically distribute sklearn computations!
#:programming, codice, snippet
::: jakevdp

A letter from WordPress community to @EmmanuelMacron, the president of France. Let’s be inclusive together!
#:Web, Internet
::: WCEurope

Incredible x86 emulator written in Javascript that runs Linux
#:programming, codice, snippet
::: climagic

Pagassero il giusto e offrissero condizioni di lavoro attraenti, li troverebbero, eccome
disinformazione sulla stampa seria (presunta)
#:innovazioni, futuro
::: demartin

How to program games with the LÖVE gaming engine on the Raspberry Pi
#:programming, codice, snippet
::: jjmerelo

Facebook could tell us how Russia interfered in our elections
#:sicurezza, spionaggio, virus
::: djspooky

A Beginner’s guide to the Scala Programming Language
#:linguaggi di programmazione
::: ThePracticalDev

Install and test Visual Studio Code on Ubuntu or Fedora
#:tools, componenti software
::: lucaciavatta

JavaScript 48 – bugs e gestione degli errori – 6 – esercizi

Continuo da qui, copio qui.

La scatola chiusa
Consider the following (rather contrived) object:

var box = {
  locked: true,
  unlock: function() { this.locked = false; },
  lock: function() { this.locked = true;  },
  _content: [],
  get content() {
    if (this.locked) throw new Error("Locked!");
    return this._content;
  }
};

It is a box with a lock. Inside is an array, but you can get at it only when the box is unlocked. Directly accessing the _content property is not allowed.

Write a function called withBoxUnlocked that takes a function value as argument, unlocks the box, runs the function, and then ensures that the box is locked again before returning, regardless of whether the argument function returned normally or threw an exception.

function withBoxUnlocked(body) {
  // Your code here.
}

withBoxUnlocked(function() {
  box.content.push("gold piece");
});

try {
  withBoxUnlocked(function() {
    throw new Error("Pirates on the horizon! Abort!");
  });
} catch (e) {
  console.log("Error raised:", e);
}
console.log(box.locked);
// → true

For extra points, make sure that if you call withBoxUnlocked when the box is already unlocked, the box stays unlocked.

Ecco getunlockB.js; chiama withBoxUnlocked: se viene sollevata l’eccezione “Locked!” chiama unlock e ripete la chiamata a withBoxUnlocked e termina chiamando lock.

var box = {
  locked: true,
  unlock: function() { this.locked = false; },
  lock: function() { this.locked = true; },
  _content: [],
  get content() {
    if (this.locked) throw new Error("Locked!");
    return this._content;
  }
};

function withBoxUnlocked(body) {
  var locked = box.locked;
  if (!locked)
    return body();

  box.unlock();
  try {
    return body();
  } finally {
    box.lock();
  }
}

withBoxUnlocked(function() {
  box.content.push("gold piece");
  console.log("Gold pieces!");
});

try {
  withBoxUnlocked(function() {
    console.log("visto e poi...");
    throw new Error("Pirates on the horizon! Abort!");
  });
} catch (e) {
  console.log("Error raised:", e);
}
console.log(box.locked);

:mrgreen: