Azure ML Scripting with Python

Evangelist at Microsoft
@timmyreilly
Timmyreilly.com

Free Machine Learning Platform
https://studio.azureml.net/

Full ML Documentation:
https://azure.microsoft.com/en-us/documentation/services/machine-learning/

Python ML Documentation:
https://azure.microsoft.com/en-us/documentation/articles/machine-learning-execute-python-scripts/

Gallery of Projects:
https://gallery.cortanaanalytics.com/

Gallery of Projects tagged Python:
https://gallery.cortanaanalytics.com/browse/?skip=0&orderby=trending%20desc&tags=%5B%22Python%22%5D

Breast Cancer Correlation Matrix:
https://gallery.cortanaanalytics.com/Experiment/Correlation-Matrix-2

def azureml_main(dataframe1 = None):
    import numpy as np
    import pandas as pd
    import matplotlib
    matplotlib.use("agg")  
    import matplotlib.pyplot as plt
    cm=dataframe1.corr()
    
    fig=plt.figure()
    plt.imshow(cm,interpolation='nearest')
    plt.xticks(list(range(0,len(cm.columns))),list(cm.columns.values), rotation=45)
    plt.yticks(list(range(0,len(cm.columns))),list(cm.columns.values))
    
    plt.colorbar()


    fig.savefig("CorrelationPlot.png")

    return pd.DataFrame(cm), 

Feature Importance with sklearn
https://gallery.cortanaanalytics.com/Experiment/Feature-Importance-with-sklearn-2

import numpy as np
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
import pandas as pd


from sklearn.ensemble import ExtraTreesClassifier


def azureml_main(iris_data):
    X = iris_data[["sepal-length","sepal-width","petal-length","petal-width"]]
    Y = iris_data["Class"]
    
    etc = ExtraTreesClassifier(n_estimators=250, random_state=0)
    etc.fit(X,Y)
    
    feat_imp = etc.feature_importances_
    std = np.std([tree.feature_importances_ for tree in etc.estimators_],\
                 axis=0)
    indices = np.argsort(feat_imp)[::-1]
    length_fp =  len(indices)

    fig = plt.figure()
    plt.title("Feature importances")
    plt.bar(range(length_fp), feat_imp[indices],\
            color="r", yerr=std[indices], align="center")
    plt.xticks(range(length_fp), indices)
    plt.xlim([-1, length_fp])
    plt.xlabel("Feature")
    plt.ylabel("Importance")
    fig.savefig("Feature_Importance.png")
    return iris_data

Beer Forecasting Example:
https://gallery.cortanaanalytics.com/Experiment/Forecasting-Beer-Consumption-2

Using Oxford TTS Service with Python 2.7 and Requests

This is a follow up to a recent post I made about using Project Oxford with Python. If you haven’t used Project Oxford before visit that post and complete the first three steps to acquire a token.

The previous example used Python 3 and urllib which is cool. But I’m more familiar with the requests library and python 2 so I spent some time re-writing the app with requests, and in the process learned what’s actually going on.

The source code can be found here: https://github.com/timmyreilly/oxford-tts-requests.

And the example from the Project Oxford Official Repo for python 3 can be found here: https://github.com/Microsoft/ProjectOxford-ClientSDK/blob/master/Speech/TextToSpeech/Samples-Http/Python/TTSSample.py

The meat of the program is tts.py which provides the methods we use in the app to provide an access token and use it to hit the API with our words to digitize.

Here’s a link to it if you’d like to follow along: https://github.com/timmyreilly/oxford-tts-requests/blob/master/FlaskWebProject/tts.py

Then we take that token and send a POST request with our access Token and specific headers “TTS_HEADERS” and “body” which includes the text to be translated.

After we POST we receive a JSON object that we encode into base 64 and inject into our HTML Template (index.html) using views.py.

To run this project:

clone the project
create a virtual env
pip install requirements.txt
python runserver.py
visit http://localhost:5555/

This project is ready to be deployed to Azure as a Web App! If you don’t know/care about that you can simply delete all the other files besides runserver.py and the contents of FlaskWebProject

Let me know if you have any questions or would like to contribute to the next iteration!

What's underneath the Man suit?
What’s underneath the Man suit?