Saving Ipython script history

%save sessionName linesToKeep

This will save script in line numbers you specify in linesToKeep,for example, 1-20 34-50 64 into a file called “” in your current working directory.

If you are not so specific on which lines you want to keep, you can save everything.

%save sessionName ~0/

This saves everything from the current session, denoted as “~0″ into file “” in your cwd (current working directory).

%save pastSession ~1/

This saves everything from the past session, denoted as “~1” into file “” in your cwd.

Discussion on AI to a child | 和孩子讨论人工智能

One of the children asked in class whether it was true that AI would replace a lot of jobs.   Yes and no, I said.   Our current AI is very good at pattern recognition, but not good at other things.   It does not have a brain like we do.

I started to explain that our current AI is like a model, which they know a bit.   To give a concrete example, I said, suppose you have two sets of numbers, one set is 1, 2, 3, 4, 5, and the other set is 2, 4, 6, 8, 10.  The computer is very good at (because we make it so) figuring out that the second set is twice of the first set.    It can recognize faces because all faces have 2 eyes, 1 nose, and 1 mouth, and have similar shapes.    In summary, it is very good at figuring out patterns if you let the computer have lots of them.

The children asked many great questions.  Some questions are truly profound, and can basically summarize the essence of the whole business of modeling.  It is amazing that how uncluttered minds can go straight to the point and not fooled by jargons or buzz.

The site is so good that its site name deserves to be in the title:

What is it? It is the merge of digital science, math and art that translates technical hypes like AI into something that feels like poetry.

scikit-learn processes simplified | 简化scikit-learn过程

Code light and concept heavy–that’s been the trend as programming languages stack on one another to wrap up more and more algorithms with fewer and fewer lines of code. A child can run a linear regression model. Yes, you hear it right. We have done it, and we summarize the building blocks of Python machine learning library scikit-learn in this table:

Process Code
Import libraries %matplotlib inline

import numpy as np

import pandas as pd

import matplotlib

import matplotlib.pyplot as plt

Format pd.options.display.float_format = ‘{:20,.3f}’.format
Import data from sklearn.datasets import load_boston

dataset = load_boston()

Prepare data y =

X =

Split data from sklearn.model_selection import train_test_split
Algorithm a from sklearn.linear_model import RidgeCV

my_regr = RidgeCV()

Train-predict, y_train)

y_pred =my_regr.predict(X_test)

Plot – performance f, ax =plt.subplots(1, 1)

ax.scatter(y_test, y_pred)

plt.plot([0, 50], [0, 50], ‘–k’)

ax.set_ylabel(‘Target predicted’)

ax.set_xlabel(‘True Target’)

ax.set_title(‘Ridge regression on test data’)

ax.text(5, 40, r’$R^2$=%.2f, MAE=%.2f’ % (

   r2_score(y_test, y_pred), median_absolute_error(y_test, y_pred)))

ax.set_xlim([0, 50])

ax.set_ylim([0, 50])

Interpretation interpretation = pd.DataFrame({‘X’: dataset.feature_names, ‘coef’: my_regr.coef_})
Algorithm b from sklearn.model_selection import cross_val_predict

from sklearn import linear_model

Train-predict my_regr = linear_model.LinearRegression()

y_pred = cross_val_predict(my_regr, X, y, cv=10)

Plot – performance f, ax =plt.subplots(1, 1)

ax.scatter(y, y_pred, edgecolors=(0, 0, 0))

plt.plot([0, 50], [0, 50], ‘–k’)

ax.set_ylabel(‘Target predicted’)

ax.set_xlabel(‘True Target’)

ax.set_title(‘linear regression with cross validation’)

ax.text(5, 45, r’$R^2$=%.2f, MAE=%.2f’ % (

   r2_score(y, y_pred), median_absolute_error(y, y_pred)))

Jupyter notebook markdown tips

Tip No. 1- Tip No. 100000: play around and click every button. If you are willing to do this continuously, you will be better than half of the programmers soon, and probably can answer most of the questions in Stackoverflow related to Jupyter Notebook markdown. Go ahead, it won’t break. The worst it could happen is restart. If you have to undo, hit Esc, then ctrl + z.


python numpy basics | 基础 python numpy 1

Tonight our class played together numpy basics and math.  We put together a numpy basics notebook:

Programming by trial and error is a great way for kids to learn not only programming but also math. — this should play a bigger and bigger role in children’s day to day learning.  玩编程也帮助学习算术。

Somehow I have a feeling that learning long division in this day and age is wrong.  在这个时代还学长除法那就不对了。

Go ahead and tinker with the code in the notebook. Nothing will break, we promise 🙂

tensorflow upgrade and testing | tensorflow 升级和测试

It has been over a year since I last used tensorflow. Not only library versions have changed, but also the syntax. My old jupyter notebook was throwing errors all over the place this morning. It is time to update everything.

For tensorflow, I am using Python 3.6.3 (I used to use Python 3.5), and numpy 1.16.1. TensorFlow has a few dependencies. numpy is one of them. Note the mnist dataset has 70,000 images.

python -c ‘import tensorflow as tf; print(tf.__version__)


The only thing that really matters is | 唯一重要的是…

We watched this very informative, entertaining and inspiring video by GitHub co-founder Tom Breston, who tricked the audience to chant on three separate occasions during this short presentation from 2012:

  • “The only thing that really matters is people 是人”
  • “The only thing that really matters is product 是产品”
  • “The only thing that really matters is culture 是文化”.

So what is the one thing that really matters? 到底是什么呢? It is the trichotomy 三分法 of people, product and culture.

From one Tom Breston’s personal blog dated 2008, he wrote:

In the end, just as Indiana Jones could never turn down the opportunity to search for the Holy Grail, I could no less turn down the chance to work for myself on something I truly love, no matter how safe the alternative might be. When I’m old and dying, I plan to look back on my life and say “wow, that was an adventure,” not ‘wow, I sure felt safe.'”

An great collection of Python notebooks | Python 笔记本集

Here is a really great collection of Python notebooks with lots and lots of links.  We start with some appetizers:

But there are so many and so much more!  You can find them from this page:


    • Linear algebra with Cython. A tutorial that styles the notebook differently to show that you can produce high-quality typography online with the Notebook. By Carl Vogel.


Math olympia medal count analysis | 奥数奖牌分析

The International Mathematical Olympiad (IMO) is an annual six-problem mathematical olympiad for pre-college students younger than 20. The first IMO was held in Romania in 1959. As we will see, eastern Europeans were top performers in the IMO in the earlier years. You can find the summary data analysis in our Jupyter Notebook on GitHub.

It has since been held annually, except in 1980 (what happened in 1980?). More than 100 countries, representing over 90% of the world’s population, send teams of up to six students (under 20 years old) to compete.

Problems cover extremely difficult algebra, pre-calculus, and branches of mathematics not conventionally covered at school and often not at university level either, such as
– projective and complex geometry
– functional equations
– combinatorics
– number theory (where extensive knowledge of theorems is required).

No calculus is required. Supporters of not requiring calculus claim that this allows “more universality and creates an incentive to find elegant, deceptively simple-looking problems which nevertheless require a certain level of ingenuity”.

Rank Country Appearance Gold Silver Bronze Honorable_Mentions
0 1 China 32 147 33 6 0
1 2 United States 43 119 111 29 1
2 3 Russia 26 92 52 12 0
3 4 Hungary 57 81 160 95 10
4 5 Soviet Union 29 77 67 45 0
5 6 Romania 58 75 141 100 4
6 7 South Korea 30 70 67 27 7
7 8 Vietnam 41 59 109 70 1
8 9 Bulgaria 58 53 111 107 10
9 10 Germany 40 49 98 75 11