Tag Archives: computer programming

Logarithm | 对数

As we had explored in previous classes, division is subtraction again and again and again, multiplication is adding again and again.  Exponentiation is multiply again and again and again— They are all inventions to simplify repeated computation.

So is the invention of logarithm: taking log is division again and again and again.   They were invented by John Napier who was a Scottish mathematician, physicist, and astronomer  in 1614 as a means to simplify calculations.

🙂 Today’s  Python numpy class summary:

Log10 means how many times divide by 10 will return you to 1. log10(100) will give you 2 because 100 divide by 10 twice returns us to one.
>>> np.log10(100)
One trillion divide by 10 twelve times returns it to 1.
>>> np.log10(1000000000000)

>>> np.linspace(0.0, 3.0, num=4)
Out: array([0., 1., 2., 3.])

>>> np.logspace(0.0, 3.0, num=4)
Out: array([   1.,   10.,  100., 1000.])

>>> np.linspace(0.0, 12.0, num=13)
Out: array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.])
>>> np.logspace(0.0, 12.0, num=13)
Out: array([1.e+00, 1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07, 1.e+08, 1.e+09, 1.e+10, 1.e+11, 1.e+12])

Bonus:  Did you know that Engineers and scientists used to use a tool called “slide rule” (计算尺) to do logarithmic computations until 1970s when electronic computer and calculators came into use.  You should go and check it out if any of your grandparents have one of these.

Find if something is also somewhere else (contd) | 找一找那里是不是也有

Today’s class we continued the game of finding matches.  We expanded from numbers to names.

Let’s pretend that there is a room out there that has the following famous people:

room1 = pd.Series([‘Grace Hopper’, ‘Albert Einstein’,’Michael Faraday’,’Emmy Noether’,’Ada Lovelace’])

And another room with these famous people:

room2 = pd.Series([‘Isaac Newton’, ‘Thomas Edison’,’Mary Somerville’,’Matilda’,’Ada Lovelace’])

These two rooms are in building:

building = pd.concat([room1, room2], axis=1)

building.columns= [‘room1′,’room2’]

             room1            room2
0     Grace Hopper     Isaac Newton
1  Albert Einstein    Thomas Edison
2  Michael Faraday  Mary Somerville
3     Emmy Noether          Matilda
4     Ada Lovelace     Ada Lovelace

Now we want to find a list of people “who” in the building and the rooms.

who = np.array([‘Albert Einstein’,’Michael Faraday’,’Ada Lovelace’])

Are they in room 1?

np.isin(who, room1)
Out: array([ True,  True,  True])

Are they in room2?

np.isin(who, room2)

Out: array([False, False,  True])

Are they in the building?

np.isin(who, building)
Out: array([ True,  True,  True])

We are constantly comparing things.  How to compare is a very tricky and interesting subject.  You should look up the source code of the function in1d and see how it does it.

Find if something is also somewhere else | 找一找那里是不是也有

In today’s class we played a game: finding the numbers in a group that are also in another group. As usual, we started simple using numpy (np).   Here is the summary:

score1 = np.array([0, 1, 3, 5, 10,3])

score2 = np.array([2,3])

We use the inld function from numpy to do the matching and counting:


Our result is:

array([3, 3])

There is another way to do this:

np.array([item in score2 for item in score1])

array([False, False,  True, False, False,  True])

score1[np.array([item in score2 for item in score1])]

will give you exactly the same answer.   Try it.

For very large groups of numbers or words, what we’ve just learned will work just as well.

Please practice and turn in the homework.

Count non-zeros using numpy.count_nonzero | 数非零数

Today our class practiced making the computer count number of non-zero numbers using the numpy library from Python.  This can be useful if you have a ton of numbers.

import numpy as np; import pandas as pd

some_array = np.array([[0,1,7,0,0],[3,0,0,2,19]])

array([[ 0,  1,  7,  0,  0],
[ 3,  0,  0,  2, 19]])



np.count_nonzero(some_array,  axis=0)  Count across the rows, i.e. count along the column

array([1, 1, 1, 1, 1], dtype=int64)

np.count_nonzero(some_array,  axis=1)  Count across the columns, i.e. count along the row

array([2, 3], dtype=int64)

We talked about this example:

d = {'Basket1': [3, 0], 'Basket2': [3, 4]}
df = pd.DataFrame(data=d, index=['Apple','Chips'])

# Count the number of non-zeros across the rows
pd.Series(np.count_nonzero(df, axis=0), index=df.columns.tolist())

This was the result we got.

Basket1    1
Basket2    2
dtype: int64

That was a very tiny data. If we have a dataset with a million rows and columns, we should definitely do this!

GitHub repo vs gist

  • For sharing snippets 代码片段 (secret or public), use gist. Gist keeps all revision history without making a fuss. No tickets or pull requests. Note: secret is not private, but you would need the url to access, unlike public gists.
  • For projects that want publicity and open collaboration, use something like GitHub repo.

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__)


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.


error: Content is protected !!