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, also machine intelligence, MI) is intelligence exhibited by machines, rather than humans or other animals (natural intelligence, NI). 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".

The scope of AI is disputed: as machines become increasingly capable, task considered as requiring "intelligence" are often removed from the definition, a phenomenon known as the AI effect, leading to the quip "AI is whatever hasn't been done yet." For instance, optical character recognition is frequently excluded from "artificial intelligence", having become a routine technology. Capabilities generally classified as AI, as of 2017, include successfully understanding human speech, competing at a high level in strategic game systems (such as chess and Go), autonomous cars, intelligent routing in content delivery networks, military simulations, and interpreting complex data.

AI research is divided into subfields that focus on specific problems, approaches, 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, artificial psychology and many others.

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 (using a symbolic approach) and soft (sub-symbolic), 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. Recent advancements in AI, and specifically in machine learning, have contributed to the growth of Autonomous Things such as drones and self-driving cars, becoming the main driver of innovation in the automotive industry.

What do you think?

Leave a Reply

%d bloggers like this: