Weekly Python Digest for Data Science (1st Week July)
By: Favio Vázquez
Tags: Collection, Digest, Python, Data Science, Machine Learning

Let’s create a new one to test the code:

[{

"objects": ["sky", "grass", "dog", "cat", "tree", "ocean", "boat"],

"relationships": [

[0, "above", 1],

[2, "standing on", 1],

[3, "by", 2],

[4, "behind", 2],

[5, "by", 4],

[6, "on", 1]

]

}]

Running:

python scripts/run_model.py \

--checkpoint sg2im-models/vg128.pt \

--scene_graphs scene_graphs/figure_blog.json \

--output_dir outputs

I got:

Kinda weird but fun :).

2. TheAlgorithms/Python — All Algorithms implemented in Python. Well “All”

Programming is a must skill in Data Science, in this great repository, we will see the implementation of several important algorithms in full Python code.

These are for demonstration purposes only. There are many implementations of sorts in the Python standard library that are much better for performance reasons.

For example you’ll find Machine Learning codes, Neural Networks, Dynamic Programming, Sorts, Hashes and much more. For example this is K-means from scratch in Python with Numpy:

3. mlens — ML-Ensemble — high performance ensemble learning

ML-Ensemble combines a Scikit-learn high-level API with a low-level computational graph framework to build memory efficient, maximally parallelized ensemble networks in as few lines of codes as possible.

ML-Ensemble is thread safe as long as base learners are and can fall back on memory mapped multiprocessing for memory-neutral process-based concurrency. For tutorials and full documentation, visit the project website.

Install through PyPI

ML-Ensemble is available on PyPI. Install with

pip install mlens

Simple example (the iris obligated examples):

import numpy as np

from pandas import DataFrame

from sklearn.metrics import accuracy_score

from sklearn.datasets import load_iris



seed = 2017

np.random.seed(seed)



data = load_iris()

idx = np.random.permutation(150)

X = data.data[idx]

y = data.target[idx] from mlens.ensemble import SuperLearner

from sklearn.linear_model import LogisticRegression

from sklearn.ensemble import RandomForestClassifier

from sklearn.svm import SVC



# --- Build ---

# Passing a scoring function will create cv scores during fitting

# the scorer should be a simple function accepting to vectors and returning a scalar

ensemble = SuperLearner(scorer=accuracy_score, random_state=seed, verbose=2)



# Build the first layer

ensemble.add([RandomForestClassifier(random_state=seed), SVC()])



# Attach the final meta estimator

ensemble.add_meta(LogisticRegression())



# --- Use ---



# Fit ensemble

ensemble.fit(X[:75], y[:75])



# Predict

preds = ensemble.predict(X[75:])

You’ll get:

Fitting 2 layers

Processing layer-1 done | 00:00:00

Processing layer-2 done | 00:00:00

Fit complete | 00:00:00



Predicting 2 layers

Processing layer-1 done | 00:00:00

Processing layer-2 done | 00:00:00

Predict complete | 00:00:00

To check the performance of estimator in the layers, call the data attribute. The attribute can be wrapped in a pandas.DataFrame , but prints in a tabular format as is.

print("Fit data:

%r" % ensemble.data)

And we see:

Fit data:

score-m score-s ft-m ft-s pt-m pt-s

layer-1 randomforestclassifier 0.84 0.06 0.05 0.00 0.00 0.00

layer-1 svc 0.89 0.05 0.01 0.01 0.00 0.00

Not so bad. eLt’s see how the ensemble as a whole fared:

Prediction score: 0.960

They have great tutorials here:

And that’s it for today :). Soon you’ll get more information, and posts with R too. If you want to stay updated please subscribe bellow: