Pages

Friday, February 28, 2014

[Python] Cannot write data into multiple array

In this case, I created a 2-D array via numpy, initiated with 0, and update them with float number.
However, without any error message, all of them are 0.0.

It is because I initiated them array with 0 (int), and my data are always between 0 to 1 so that all of them are casted to 0.0 (float)

Make sure initiate your array with the same type you will use.

Below are some codes to generate the 2-D array and update them

multiDimArray.py
import numpy as np nCV = 5 nFeat = 3
# generate a 5-by-3 array initiate with 0.0 result_list = [[0.0 for j in range(0, nFeat)] for i in range(0, 5)] """ Below is what drove me crazy result_list = [[0 for j in range(0, nFeat)] for i in range(0, 5)] """ # convert to array via numpy result_array = np.array(result_list) print 'Before:', result_array # update (Python starts at 0) result[2][2] = 0.8 print 'After: ', result_array """ if you want to use list rather than array afterward, you can use either result_list = result_array.tolist() or result__list = list(result_array) """

Tuesday, February 25, 2014

More pain, more progress, and happiness in the end

經濟、愛情、親情、學業在毫無預警同時施壓的當下,仍在發展初期的新人格突然茁壯了。聯想到某說客對我說「你有著超齡的獨立」,我對曰「哈!我根本沒得選擇」不禁莞爾。

Being suffered by finance, love, parents, and study at the same time.  I will make all the obstacles my strength.

Sunday, February 23, 2014

[Linux] Some tips for screen

In my opinion, screen is like a CLI-version VNC or RDP, which means you can log in your previous window again using only ssh session.

Screen is especially useful if you connect the server via SSH and need to run a time-consuming process.  Just open a window, running the process on that window, then you can leave and log into the same window to continue you work.

In this post, I use 'window' rather than 'screen' to give you a clear image.  But technically, they are 'screen's by definition.

Show the window list
screen -ls

Create new window with given name
screen -S [window-name]

Attach to an unattached, or log in if it is attached, window
screen -r [window-name]

Attach to an attached window (multiple log in the same window)
screen -x [window-name]

Detach (leave but not kill) a window
screen -d [window-name]

Kill a detached window
screen -X -S [session # you want to kill] quit

Leave (and kill) the current window you are on
exit

Check whether you are on any window
echo $STY

if is is 'null', then you are not on any window.  You can also edit your shell to show that in your prompt:
~/.bashrc
# just showing that you are using screen or not if [ -n "$STY" ]; then export PS1="(screen) $PS1"; fi # or showing the window name # if [ -n "$STY" ]; then export PS1="($STY) $PS1"; fi

You can also use hot-keys, which I do not recommend to the beginners.  Check the reference for more detailed information:

References
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
http://www.bartbania.com/index.php/linux-screen/

Tuesday, February 11, 2014

[Python] Dealing with the 'CSV new-line character seen in unquoted field error'

To load the Windows-imported csv files or any csv files which are generated via Excel without the error message:

_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

Just do as it said: use universal-newline mode, which means ‘U’ in the arguments.

Example:
import csv fin = open(‘test.csv’, ‘rU’) myReader = csv.reader(fin, delimiter = ‘,’)

Thursday, February 6, 2014

[Linux] Check the memory size in human readable format

Just a quick backup.

sudo lshw -businfo -C memory | awk '/System\ Memory$/{print $2}'


Alternative: Show the number of GB (only the number)
free -g | awk '/Mem:/{print $2}'