"### Using a Multi-Layered Perceptron (MLP) to Classify Mushrooms as Edible or Poisonous\n",
"In this Notebook, we'll be using the mushroom classification dataset, which you can find here https://www.kaggle.com/uciml/mushroom-classification to train an MLP to determine whether a mushroom is edible (e) or poisonous (p), based its physical characteristics."
"Now, let's just pick the 'class_e' feature. This means if the Perceptron returns a value of 1, then the mushroom is edible. If it returns 0, then the mushroom is poisonous. Let's also pick 'gill_size_b', because the only other value it can be is 'gill_size_n', which means the gill size will be broad when it = 1, and narrow when it = 0. We'll pick all the colours to train on. "
"final = subset.filter(['class_e','gill-size_b','spore-print-color_h','spore-print-color_h','spore-print-color_k','spore-print-color_n','spore-print-color_o','spore-print-color_r','spore-print-color_u','spore-print-color_w','spore-print-color_y'])\n",
"final.head()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x train/test (6499, 10) (1625, 10)\n",
"y train/test (6499,) (1625,)\n"
]
}
],
"source": [
"#Create the train/test splits as we did before\n",
"Let's create an MLP. Currently the only loss function it supports is the Cross-Entropy loss function, which is used by default. By default, it uses the ReLU activation function. It also has a default of 1 hidden layer, containing 100 neurons. \n",
"\n",
"Here are some parameter options you can explore:\n",
"Test a mushroom with a broad gill-size and black spore print color, where index = 0 is gill-size and index = 3 is black"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_mushroom = [1,0,0,0,0,0,0,0,1,0]\n",
"prediction = MLP.predict([test_mushroom])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if prediction==1:\n",
" print('Edible')\n",
"else:\n",
" print('Poisonous')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise:\n",
"Have a go at changing some of the learning parameters, e.g. add more layers, or more neurons per layer, or change the activation function, and see if you can improve performance beyond 98%!"
### Using a Multi-Layered Perceptron (MLP) to Classify Mushrooms as Edible or Poisonous
In this Notebook, we'll be using the mushroom classification dataset, which you can find here https://www.kaggle.com/uciml/mushroom-classification to train an MLP to determine whether a mushroom is edible (e) or poisonous (p), based its physical characteristics.
%% Cell type:code id: tags:
``` python
#load the dataset
data=pd.read_csv("./datasets/mushrooms.csv")
```
%% Cell type:code id: tags:
``` python
#check out its features
data.head()
```
%% Output
class cap-shape cap-surface cap-color bruises odor gill-attachment \
Now, let's just pick the 'class_e' feature. This means if the Perceptron returns a value of 1, then the mushroom is edible. If it returns 0, then the mushroom is poisonous. Let's also pick 'gill_size_b', because the only other value it can be is 'gill_size_n', which means the gill size will be broad when it = 1, and narrow when it = 0. We'll pick all the colours to train on.
Let's create an MLP. Currently the only loss function it supports is the Cross-Entropy loss function, which is used by default. By default, it uses the ReLU activation function. It also has a default of 1 hidden layer, containing 100 neurons.
Test a mushroom with a broad gill-size and black spore print color, where index = 0 is gill-size and index = 3 is black
%% Cell type:code id: tags:
``` python
test_mushroom=[1,0,0,0,0,0,0,0,1,0]
prediction=MLP.predict([test_mushroom])
```
%% Cell type:code id: tags:
``` python
ifprediction==1:
print('Edible')
else:
print('Poisonous')
```
%% Cell type:markdown id: tags:
### Exercise:
Have a go at changing some of the learning parameters, e.g. add more layers, or more neurons per layer, or change the activation function, and see if you can improve performance beyond 98%!