Featured Post

Set up machine learning and deep learning on AWS

Here is the simple instructions to set up a EC2 instance to run machine learning and deep learning on AWS 1.  Run an EC2 instance from ...

Dec 30, 2020

Ways to format the code snippet and tables in the blog

 


I'm trying to embed the Python code into the the blog posts. I have played around the format and found there is no good support in blogger to fulfill this task. After a deep dive, I have found two ways to embed the code snippet in a good format. 

One way is to use the HTML format site - http://hilite.me/.  Here is a sample Python code to judge if a number is null or not.

# import numpy
import numpy as np

# function to judge if a number is null
def is_null(x):
   return x != x

# test samples
is_null(np.nan)
is_null(2)

Or you are not comfortable to use HTML, simply copy and paste the code below in the HTML view and modify it in the Compose view.

<!-- HTML --><div style="background: #f0f0f0; overflow:auto;width:auto;border:None gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;"><pre style="margin: 0; line-height: 125%"><span style="color: #60a0b0; font-style: italic"># print</span>
<span style="color: #007020; font-weight: bold">print</span>(<span style="color: #4070a0">&quot;All About Python&quot;</span>)
</pre></div>

The output of the above embedding.

# modify the code here
print("All About Python")

The other way is to use gist - https://gist.github.com/. Here are the steps to create the formatted code in the gist. An example of Python code is showed below.

  1. Sign up a account with GitHub if you don't have it
  2. Enter the file name with proper extension. For example, I use isnull.py as the file name
  3. Copy or type your code
  4. Select "Create public gist" and click on it. Now the code is ready to share
  5. Choose "Embed" and Copy the link address and paste in the blog editor in HTML mode.

To add tables to the blog, it's easier to use tableizer to format table in HTML code. It's simply to copy the table from the excel sheet to the tableizer and choose the proper font size, color, and font style. Click on the "Tableize it!" and copy the HTML, and paste it in the HTML view. An example is showed below.

DateOpenHighLowClose
10/18/19128.34129.21127.38129.12
10/27/19128.21129.39127.1129.31
10/26/19127.31128.21127.03128.12
10/25/19126.41127.56125.98127.21

Dec 28, 2020

Python installation and environment setup

 


There are many ways to install Python on your machine. The easiest and most popular way is to download and install Anaconda. A pure Python without any packages is useless, as we are using a list of Python packages to do the work. To use Python for numerical computation, we need NumPy and SciPy. To process data and tables, we would need the Pandas, and for data visualization, Matplotlib is the basic package. You may also need machine learning packages, such as scikit-learn, TensorFlow, etc. To manage the dependencies of the package version,  it's essential to use a Python package manager. Anaconda is the best of the Python package mangers that provides the essential Python tools to get started. 

For individual uses, you can find the Anaconda for your machine specifics here on Anaconda's download page. Choose the operation system and the corresponding installer, follow the instruction and install the Anaconda on your machine. 

Suppose you are using a Mac OS and you have installed the Anaconda using the Graphical Installer. There are multiple ways to open Python.

1. Open the terminal and type 'python'

$ python

2. Open the graphical Anaconda-Navigator and click on the notebook

3. Open the terminal and type 'jupyter notebook'

$ jupyter notebook

Introduction to Python language

 


There are so many programming languages. Why do we need to learn Python? What are the advantages that make Python unique for beginners to learn coding? Well, there might be many answers to this question. The best answer would be that Python is popular, especially in data science and machine learning. Python is now becoming the basic and almost unique tool to data scientist and model developers. 


Another advantage of the Python is the availability in open source libraries. Python has now been used prevalently in machine learning and deep learning with a list of high level packages available in open source, such as scikit-learn, TensorFlow, PyTorch, etc.

Python as a programming language was designed with simple syntax, emphasizing on the readability and prototyping. 

Writing the first line of code with Python on terminal or Jupyter notebook

print('All About Python')

Define a sequence of numbers

alist = [2, 3, 5, 9, 10]
print(alist)

Write a function to filter the even numbers in a list

def get_even_numbers(a):
    even_nums = []
    for x in a:
        if x % 2 == 0:  
            even_nums.append(x)
    return even_nums

get_even_numbers(alist)




Apr 25, 2020

Python methods cheat sheet

Python Tips and Tricks, You Haven't Already Seen, Part 2

Python list methods

append(item)
>>> lst = [1, 2, 2]
>>> lst.append(4)
[1, 2, 2, 4]
count(item)
>>> lst.count(2)
2
extend(list)
>>> lst.extend([5, 6])
[1, 2, 2, 4, 5, 6]
index(item)
>>> lst.index(5)
4
insert(position, item)
>>>lst.insert(1, 9)
[1, 9, 2, 2, 3, 5, 6]
sort()
>>> lst.sort()
[1, 2, 2, 3, 5, 6, 9]
pop(index)
>>> lst.pop()
9
reverse()
>>> lst.reverse()
[6, 5, 3, 2, 1, 1]

Python string methods
>>> s = 'Hello2Python'
count(sub, start, end)
>>> s.count('l', 1, 5)
2
find(sub, start, end)
>>> s.find('l', 0, 5)
2
index(sub, start, end)
>>> s.index('t', 0, 9)
8
isalnum()
>>> s.isalnum()
True


isalpha()
>>> s.isalpha()
False
isdigit()
>>> s.isdigit()
False
islower()
>>> s.islower()
False
isupper()
>>> s.isupper()
False
isspace()
>>> s.isspace()
False
join()
>>> s.join('___')
'_Hello2Python_Hello2Python_Hello2Python_'
lower()
>>> s.lower()
hello2python
partition(sep)
>>> s.partition('2')
('Hello', '2', 'Python')
split(sep)
>>> s.split('2')
['Hello',  'Python']
strip()
>>> s.strip('n')
'Hello2Pytho'
upper()
>>> s.upper()
'HELLO2PYTHON'



Common Linux command examples

Best Linux distros of 2020: for beginners and advanced users ...
Here is a collection of commonly used Linux command

Move files between servers

Move file from current server to another server (remote_server)

scp this_path/this_file username@remote_server:remote_path/

Move file from remote server to local server

scp username@remote_server:remote_path/remote_file current_path/

Check resource usage
top
free -h

Check server CPU information
lscpu
cat /proc/cpuinfo

Set up machine learning and deep learning on AWS

File:AWS Simple Icons AWS Cloud.svg - Wikimedia Commons

Here is the simple instructions to set up a EC2 instance to run machine learning and deep learning on AWS

1.  Run an EC2 instance from AWS
Login the AWS account and switch to Oregon. Click on the Community AMI on the left panel.
Search ami-813110b1 and lunch the instance
Choose t2.micro the free tier


2.  Install and configure Anaconda
Download Anaconda
$ wget https://repo.continuum.io/archive/Anaconda2-4.2.0-Linux-x86_64.sh

  • Install with bash
$ bash Anaconda2-4.2.0-Linux-x86_64.sh

  • Configure Anaconda
Start python
>>> from notebook.auth import passwd
>>> passwd()

Copy the passwrod here and paste to the configuration file
$ jupyter notebook --generate-config

Open ./jupyter/jupyter_notebook_config.py
c.NotebookApp.ip = '*'
c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'

3. Install tensorflow, keras

$ pip install tensorflow
$ pip install keras

4. Start jupyter notebook
$ jupyter notebook --no-browser --port=8888


Open the browser and enter DNS the address
http://ec2-35-161-215-37.us-west-2.compute.amazonaws.com:8888

Apr 24, 2020

Adjust the cell width of the Jupyter notebook

The default width of the notebook cell is sometimes not fit the purpose to display the content. In this case, we would like to adjust the cell size. There are few solutions to do this.

The permanent solution is to create a file in the home folder for IPython notebook.

~/.ipython/profile_default/static/custom/custom.css

or Jupyter notebook

~/.jupyter/custom/custom.css

with content

.container { width:100% !important; }

Then restart iPython/Jupyter notebooks. Note that this will affect all notebooks. 

If you don't want to change your default settings, and you only want to change the width of the current notebook you're working on, you can enter the following into a cell:

from IPython.core.display import display, HTML
display(HTML("<style>.container {width:90% !important;</style>"))