Skip to content
superstringer edited this page Aug 4, 2021 · 3 revisions

Python

Install pip and pipenv

$ sudo apt install python3-pip python3-dev
$ pip3 install --user pipenv

Add pipenv (and other python scripts) to PATH

$ echo "PATH=$HOME/.local/bin:$PATH" >> ~/.bashrc
$ source ~/.bashrc

IPython

Install IPython

$ pip install jupyter

Set up remotely hosted IPython server

Method 1

First start an IPython notebook session on your remote machine, with the --no-browser and --port=7777 options

$ ipython notebook --no-browser --port=7777

Then we set up the ssh tunnel from the local machine to the remote machine

$ ssh -N -f -L localhost:7777:localhost:7777 dh@68.137.6.28

(Note: on mac to find and kill the background ssh process, just do

$ ps -ax | grep ssh
$ kill #PID

Go to localhost:7777 on your local browser and start using remotely hosted IPython notebooks.

Method 2

On the server, run

$ jupyter notebook --ip=server_machine.local

It will give an URL, then just go to that URL through the browser on your local machine.

Change working directory

You can just do

cd $work_dir

in your IPython code.

Pandas

Video Tutorial

Go to link.

Pandas DataFrame

Useful methods

>>> df.CompanyGroup.value_counts()

Iteration

itertuples() is about 60 times faster than iterrows() when iterating through the rows of a Pandas dataframe

>>> df = pd.DataFrame({'a': randn(1000), 'b': randn(1000),'N':randint(100, 1000, (1000)), 'x': 'x'})
>>> %timeit [row.a * 2 for idx, row in df.iterrows()] # => 10 loops, best of 3: 50.3 ms per loop
>>> %timeit [row[1] * 2 for row in df.itertuples()] # => 1000 loops, best of 3: 541 µs per loop

Python Packages

readline

For the python interractive environment to work with up and down keystrokes, you need to install the readline package,

$ pip install readline

urllib

There is no request object in urllib as in section 3.1 in the nltk book. Instead we can directly call the urlopen method in urllib

>>> url = "http://www.gutenberg.org/files/2554/2554.txt"
>>> response = urllib.urlopen(url)

Machine Learning packages

  • sklearn/scikit (best)
  • pylearn2

Install freetype first before installing matplotlib

$ sudo apt-cache search freetype | grep dev
$ sudo apt-get install libfreetype6-dev
$ python -m pip install matplotlib

pydot

$ python -m  pip install pydot2

NLTK

Download nltk in the python command-line

$ python
>>> import nltk
>>> nltk.download('all')

Make sure you have the lastest version of nltk because it is constantly maintained

$ python -m pip install --upgrade nltk

Install megam algorithm for MaxentClassifier

$ brew tap homebrew/science
$ brew install megam

Arg Parameters

argparse store false if unspecified

The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. For example

>>> parser.add_argument('-p', '--param', action='store_true')

param will default to false if -p is not present.

Other

List all methods for an object

>>> dir(obj)

List current directory content in python

>>> import os
>>> os.listdir('.')

Capture user input in python

Use raw_input() instead of the input() method as in nltk book section 3.1.

Reload a function in a python session

If we have made some changes to a function func inside a module, we can reload the function by

>>> reload(module)
>>> from module import func

or

>>> import imp
>>> imp.reload(module)

Get method in dict

See page.

Write unicode text to a file

>>> foo = u'Δ, Й, ק, ‎ م, ๗, あ, 叶, 葉, and 말.'
>>> with open('test', 'w') as f:
        f.write(foo.encode('utf8'))

When you read that file, you'll get a unicode-encoded string that you can decode to a unicode object

>>> with open('test', 'r') as f:
        print f.read().decode('utf8')

Time measurement

To measure time for a calculation in python, e.g. calculate cosine distances, do

>>> timeit.timeit('cosine(a,b)',setup="from scipy.spatial.distance import cosine; import numpy as np; a = np.random.rand(1,136702); b = np.random.rand(1,136702)",number=1000)

Python frozenset()

The frozenset() method returns an immutable frozenset object initialized with elements from the given iterable.

Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of frozen set remains the same after creation.

Due to this, frozen sets can be used as key in Dictionary or as element of another set. But like sets, it is not ordered (the elements can be set at any index).

Clone this wiki locally