AI programming with python code

Blog Tech AI programming with Python. In action.

AI programming with Python. In action.

It’s amazing what people can do. We humans can write novels, build skyscrapers, sing operas, drive race cars, cook delicious food, and so much more. Each of these tasks requires intelligence of one kind or another. 

In recent years, computers and software have grown so powerful that we’ve tried to teach them to do some of these things as well. When computer programs can do something that seems to mimic human intelligence, we say that they have artificial intelligence (AI).

In this article, we’ll discuss a task that requires intelligence: determining whether a mushroom is edible or poisonous. We’ll introduce an AI method that can help us, and we’ll go over some Python code that implements the AI method.

The scenario and data.

Every year, thousands of people contact poison control centers to report that they’ve eaten poisonous mushrooms. Many of these people have to be hospitalized.  Imagine if we could create an app that could use artificial intelligence to accurately classify whether any given mushroom was expected to be edible or poisonous. An app like that could help people stay healthy – and maybe even save lives!

Many applications of artificial intelligence rely on data. In this case, we’ll use an interesting public-domain dataset to train our AI to classify whether mushrooms are edible or poisonous. You can download this data for free. If you save it on your computer in a file called mushrooms.csv, then you can read it into Python as follows:

import pandas as pd
mushrooms = pd.read_csv('mushrooms.csv')

Here, we imported the Python package called pandas. This package provides many capabilities related to working with datasets. In this case, we used one of its methods called read.csv(). That method enabled us to read the data related to mushrooms into our Python session. You can look at the first few rows of this data as follows:


If you run this, you’ll see the first five rows of the dataset:

Here, every row represents a particular mushroom. The column called “class” tells us whether each mushroom is poisonous (“p”) or edible (“e”). Other columns tell us other things about each mushroom. For example, mushrooms that have “cap-shape” equal to “b” are bell-shaped, and mushrooms that have “cap-shape” equal to “x” have a convex shape. 

Now that you’ve loaded the data into your Python session, let’s go over the AI algorithm we’ll use to classify mushrooms.

AI method: working with decision trees.

It would be convenient if there were an easy way to quickly know whether any mushroom is poisonous. For example, what if we could use the following figure to know whether a mushroom is edible:

*Please note – all the figures in this post are only illustrative. They are meant to explain ideas behind artificial intelligence – not give medical advice! Consult an expert if you want to know whether you should eat a particular mushroom.

If you start at the top of this figure, you can answer a question (whether a mushroom is odorless), and then based on the answer, follow the arrows below (either YES or NO) to a conclusion about whether the mushroom is edible. This is known as a decision tree, and it’s a very simple type of artificial intelligence

In real life, answering hard questions (like whether a mushroom is poisonous) is usually harder than the figure above makes it look. In the case of mushrooms, there are more things that need to be considered besides just the odor. For example, you might need to consider the shape and color of different parts of the mushroom, like you can see in the following figure:

You can see that the decision tree has grown: instead of answering only one question, we answer multiple questions before coming to a conclusion about whether the mushroom is edible. Decision trees can be small or large, but regardless of their size we use them in the same way: start at the top of the tree, and answer yes-or-no questions until reaching conclusions in the “leaves” at the bottom of the tree.

In our scenario (determining whether mushrooms are edible or poisonous), we will need a bigger tree to get accurate results. In the next section, we’ll introduce some Python code to generate just such a tree.

Example: predicting mushroom edibility.

We can create a decision tree that classifies mushrooms as poisonous or edible with less than 10 lines of Python code. We can start by getting our data into the right format:

X=pd.get_dummies(mushrooms.loc[:,mushrooms.columns != 'class'])

Here, we created two variables, X and y. Our y variable tells us whether every mushroom is poisonous or edible: it’s our “target,” or in other words what we’re trying to classify or predict. Our X variable contains our “features,” meaning every other variable in the dataset. This is a common setup in machine learning problems: our computer programs learn how to use our features (X) to predict our target (y). In this case, we created X and y as “dummy variables,” meaning variables that consist entirely of 0’s and 1’s. Using dummy variables will make it easier to create our decision tree.

We can create our decision tree with the following code:

from sklearn import tree
clf = tree.DecisionTreeClassifier()
clf =, y)

Here, we imported the tree module from the sklearn package (also called scikit-learn). Then, we created a decision tree classifier, and “fit” it to our data. That’s all it takes to create a decision tree.

You can create a plot of the decision tree we created as follows:

import matplotlib.pyplot as plt

The result is shown below:

If you look closely at this decision tree, it’s similar to the trees we looked at previously – but larger. If you start at the top of the tree, you can see that we’re supposed to check the value of “odor_n” – the variable that records whether a mushroom is odorless. You proceed to the right if odor_n is 1, and you proceed to the left if odor_n is 0. As you continue down the branches of the tree, you check the values of other variables, one by one. 

Eventually, you arrive at a conclusion about whether the mushroom is poisonous or edible. In this figure, the conclusion is color-coded: blue represents edible, and orange represents poisonous.

Congratulations! You have used AI to create a decision tree that classifies whether mushrooms are edible or poisonous.

Learn more about AI programming with Python.

Artificial intelligence is a huge field, and the decision tree we created here is only the beginning of what you can do with it. 

Check out the AI Programming with Python Nanodegree program to learn foundational AI programming tools (Python, NumPy, PyTorch) and the essential math skills (linear algebra and calculus) that will enable you to start building your own AI applications in just a few months.


Stay updated on the latest in technology

"*" indicates required fields

Stay updated on the Latest in Tech

Be first to hear about special offers, news, and resources from Udacity

"*" indicates required fields