Easy AI with Python

Easy AI with Python 3

By Monty, 19th May 2016

Survey several basic AI techniques implemented with short, open-source Python code recipes. Appropriate for educators and programmers who want to experiment with AI and apply the recipes to their own problem domains. For each technique, learn the basic operating principle, discuss an approach using Python, and review a worked out-example. We’ll cover database mining using neural nets, automated categorization with a naive Bayesian classifier, solving popular puzzles with depth-first and breadth-first searches, solving more complex puzzles with constraint propagation, and playing a popular game using a probing search strategy.

— Raymond Hettinger, Pycon 2009

I haven’t posted here in a few months (for no particularly good reason). Having recently become interested in Artificial Intelligence (AI), especially Machine Learning (ML), and then more especially Artificial Neural Networks (ANN), I was thinking about posting some short AI programs – but I have yet to write any. So today I came across this video of Raymond Hettinger talking about Easy AI with Python. Now if you don’t know about RH, you’re in for a treat! He’s possibly (IMHO) the world’s best teacher of Python, and he’s written a great deal of the Python interpreter. The code fragments in that (old) video seem to me to be ideal for this site, and so I’ll be posting some of them in due course (updated to Python 3). Meanwhile, please enjoy the video, embedded below!

Note that the codes are in Python 2; as just mentioned, I’ll be posting Python 3 versions, but till then, here are some of the things to change if you can’t wait:

  • Remove the x from xrange;
  • Change print to a function, with brackets;
  • More as I notice them.

I’m not sure whether all the programs he describes would be regarded as true AI by everyone, but they’re certainly fun, interesting, useful, and educational. And anyway some of the techniques typically found in AI/ML seem to me to be just ‘curve-fitting’ or some other piece of mathematics or computer science that doesn’t really warrant a fancy name; most of what we program computers to do previously required some level of intelligence. But I’m still a newb at this so I hope I’ll figure it out eventually!

00:00 Intro
05:00 Eight Queens – Six Lines http://code.activestate.com/recipes/5…
06:45 Alphametics Solver http://code.activestate.com/recipes/5…
11:30 Neural Nets for Data Mining http://code.activestate.com/recipes/4…
16:20 Each unique value gets a neuron
22:10 Code
22:30 Mastermind http://code.activestate.com/recipes/4…
31:20 Shannon’s formula applied to strategy – select probe with greatest information
33:55 Sudoku-style Puzzle http://code.activestate.com/recipes/4…
38:50 Bayesian Classifier http://www.divmod.org/projects/reverend or http://sourceforge.net/projects/rever…
43:30 Code44:50 Hettinger http://users.ren.com/python/download/…
47:30 Q&A

Thanks to GothAlice for uploading this to YouTube.


Artificial intelligence (AI) is intelligence exhibited by machines. In computer science, the field of AI research defines itself as the study of "intelligent agents": any device that perceives its environment and takes actions that maximize its chance of success at some goal. Colloquially, the term "artificial intelligence" is applied when a machine mimics "cognitive" functions that humans associate with other human minds, such as "learning" and "problem solving" (known as Machine Learning). As machines become increasingly capable, mental facilities once thought to require intelligence are removed from the definition. For instance, optical character recognition is no longer perceived as an exemplar of "artificial intelligence", having become a routine technology. Capabilities currently classified as AI include successfully understanding human speech, competing at a high level in strategic game systems (such as Chess and Go), self-driving cars, intelligent routing in content delivery networks, and interpreting complex data.

AI research is divided into subfields that focus on specific problems or on specific approaches or on the use of a particular tool or towards satisfying particular applications.

The central problems (or goals) of AI research include reasoning, knowledge, planning, learning, natural language processing (communication), perception and the ability to move and manipulate objects. General intelligence is among the field's long-term goals. Approaches include statistical methods, computational intelligence, and traditional symbolic AI. Many tools are used in AI, including versions of search and mathematical optimization, logic, methods based on probability and economics. The AI field draws upon computer science, mathematics, psychology, linguistics, philosophy, neuroscience and artificial psychology.

The field was founded on the claim that human intelligence "can be so precisely described that a machine can be made to simulate it". This raises philosophical arguments about the nature of the mind and the ethics of creating artificial beings endowed with human-like intelligence, issues which have been explored by myth, fiction and philosophy since antiquity. Some people also consider AI a danger to humanity if it progresses unabatedly. Attempts to create artificial intelligence have experienced many setbacks, including the ALPAC report of 1966, the abandonment of perceptrons in 1970, the Lighthill Report of 1973, the second AI winter 1987–1993 and the collapse of the Lisp machine market in 1987.

In the twenty-first century, AI techniques, both "hard" and "soft" have experienced a resurgence following concurrent advances in computer power, sizes of training sets, and theoretical understanding, and AI techniques have become an essential part of the technology industry, helping to solve many challenging problems in computer science.

What do you think?

Leave a Reply

%d bloggers like this: