WALKTHROUGH: It's Alive! How to Bootstrap Artificial Intelligence

Follow this walkthrough to build artificial intelligence in your basement, in twenty minutes max. It's the quick version of the tutorial Google generously put on Youtube:

Follow that? Let's walk through it.

  1. Download and install Anaconda

    This has all the prerequisite libraries you need, including the new version of Python. I highly recommend this step. You can download Anaconda here.

  2. Import "tree" from the scikit-learn software machine learning library Create a new file (say, "artificial-intelligence.py") and add the four magic words:
    from sklearn import tree
    
  3. Provide the training data

    We have training data regarding the weights and textures of some apples and oranges:

    Training Data
    labels features
    Fruit Weight in grams Texture
    Apple 140 smooth
    Apple 130 smooth
    Orange 150 bumpy
    Orange 170 bumpy

    The way to express this is Python is:
    features = [[140, "smooth"], [130, "smooth"], [150, "bumpy"], [170, "bumpy"]]
    labels = ["apple", "apple", "orange", "orange"]
    
  4. Now, we change our variable types to integers ("ints") instead of strings, with the integer 1 standing for the string "smooth", and the integer 0 standing for the string "bumpy":
    features = [[140, 1], [130, 1], [150, 0], [170, 0]
    
    We do the same for the fruits, using the integer 0 for the string "apple" and 1 for the string "orange":
    labels = [0, 0, 1, 1]
    
  5. Train Classifier Now we are going to train our classifier clf, which in this case is a decision tree (i.e. a box of rules). There are many different types of classifiers, but the input and out type is always the same.
    clf = tree.DecisionTreeClassifier()
    
    At this point, it's just an empty box of rules. To train it, we'll need to import a learning algorithm.
  6. We add our training algorithm. It's called fit, and it's job is to find algorithms in data:
    clf = clf.fit(features, labels)
    
  7. Now, our mystery fruit. Let's see what our newborn AI does with a bumpy piece of fruit that weights 160 grams (Remember, 1 = smooth and 0 = bumpy):
    smoothness = 0
    weight = 160
    mystery_fruit = [weight, smoothness]
    
  8. Are you ready? Let's see what our baby cyborg brain does with the mystery fruit:
    if clf.predict([mystery_fruit]) == 0:
     print("It's an apple.")
    if clf.predict([mystery_fruit]) == 1:
     print("It's an orange.")
    
  9. Drumroll.... Your code should now look something like this:
    from sklearn import tree
    #import decision tree (DT) capability from
    #the scikit-learn software machine learning library
    
    features = [[140, 1], [130, 1], [150, 0], [170, 0]]
    # [weight in grams, smoothness] where:
    # 1 = smooth
    # 0 = bumpy
    
    labels = [0, 0, 1, 1]
    # 0 = apple
    # 1 = orange
    
    clf = tree.DecisionTreeClassifier()
    clf = clf.fit(features, labels)
    
    #a bumpy piece of fruit that weights 160 grams
    smoothness = 0
    weight = 160
    mystery_fruit = [weight, smoothness]
    #1 = smooth 
    #0 = bumpy
    
    if clf.predict([mystery_fruit]) == 0:
     print("It's an apple.")
    if clf.predict([mystery_fruit]) == 1:
     print("It's an orange.")
    
    #Copyright 2017 Peter Charles Gleason
    
  10. It's alive!!! If you run your program and it spits out "it's an apple/orange", welcome to the world of machine learning. 



Comments