solution for installing pygame

I have two computers, the one has windows system at home, the other has ubuntu system in my office. In each computer, I have two python, the one is normal python, the other is anaconda.

I try to install the pygame on both computer, then I got in chaos. My computer can not find it, even I installed pygame with pip several times.

After some effort, I got the solution:

download the pygame binary distribution for your operating system:



  • For normal python: pip install your_download_path\downloaded_pygame
  • For anaconda python:
    • use the anaconda pip
    • pip install your_download_path\downloaded_pygame


Setting in Eclipse:
In eclipse, you can set the python interpreter by yourself

  • In menu: Project -> Properties ->  pyDev-Interpreter/Grammar -> Click here to configure an interpreter not listed -> New -> …
  • you can also set PYTHONPATH: Project -> Properties -> pyDev-PYTHONPATH (you can also set the external library here)

check the loaded packages in project:

  • In the “PyDev Package Explorer” viewer:
    • under all the project file, your can see your python interpreter name, double click, all the installed packages are listed here. 🙂




trunc() and floor(), round() and signif() in R

floor takes a single numeric argument x and returns a numeric vector containing the largest integers not greater than the corresponding elements of x.

trunc takes a single numeric argument x and returns a numeric vector containing the integers formed by truncating the values in x toward 0.

round rounds the values in its first argument to the specified number of decimal places (default 0).

signif rounds the values in its first argument to the specified number of significant digits.



> x <- c(-5.2, -3.8, 5.2, 3.8)


-6 -4 5 3


-5 -3 5 3

> x <- 3.1415









Plot Histogram in R

Plot Histogram in R:

Method 1: hist(vector, breaks)

Take care for the parameter “breaks”:

according the help:

breaks: a single number giving the number of cells for the histogram.

Important: the number is a suggestion only! The breakpoints will be set to pretty values.

For example:

>data(“women”) #load dateset “women”

> hist(women$weight, breaks = 7)








If you want to have exact 7 bins, you should specify the position

>hist(women$weight, breaks=seq(min(women$weight), max(women$weight), l = 7))









so, you will get the exact 7 bins


matrix calculation in R and Python

  • Useful packages:
    • R: library(matrixcalc)
    • Python: numpy
  • Matrix generation:
    • R: matrix(1:6, 2, 3)
    • Python: np.arange(1,7).reshape(2,3)
  • construct a diagnal matrix:
    • R: advanced-R
      • diag(vector, nrow, ncol) #construct a matrix which diagonal elements is equal to the vector
      • diag(matrix) # a vector which elements is the diagonal elements of the matrix
    • Python: np.diag(vector)
      • np.diag(vector, k) # like the diag(vector) in R, k means the shift of the diagnoal
      • np.diag(matrix) #return a vector which values are the diagonal elements of the matrix
  • singular value decomposition:
    • R:
      • svd(matrix)
    • Python:
      • np.linalg.svd(matrix, full_matrices=True) #if full_matrices=False, the result will be the same as with R
  • resolve Ax=b
    • R: solve(A,b)
    • Python: np.linalg.solve(A,b)

How to install R-extention in RapidMiner for Ubuntu?

I found the easiest way to install R extention for Ubuntun

  • RapidMiner -> Marketplace -> R Extention
  • RStudio -> Tools -> Install Packages… -> Input in the field “Packages”: rJava, JavaGD
    Or input in R Console: install.packages(c(“rJava”,”JavaGD”))
  • Restart RapidMiner
  • you get a popup -> choose the jri file, the path is here:
  • Restart RapidMiner

This article is more detailed.


Start with R

Useful web sites for R:

About Package

  • install.packages(“ggplot2”), you can also install with RStudio menu: Tools -> Install Packages…
  • library(ggplot2)

Use Python to detect your computer system and name

I work on several computers, sometimes Widows, sometimes Linux, so I got a problem after I pull my project file from Git. That is the file path.

To cope with this problem, I have to find out which computer I am working on at moment. With Python, you have sevral ways to do that:

  • >>>import platform
    >>>platform.node() => you will get the name of the computer
    [Out]: codehamster-home
  • >>>import socket
    >>>socket.gethostname() => get also the name of the computer
    [Out]: codehamster-work
  • >>>import platform
    >>>platfrom.uname() => you will get the detail information about your computer
  • >>>import sys
    >>>sys.platform => you will the type of the system
    [Out]: win32

Here is my function, which adjust the path according your computer:


Read an Image with python

  • If you want to read an image, you will need the ‘image’ package:
    • import Image
  • read the image:
    • Image.open(filename)
  • convert the Image to grey scale:
    • Image.open(filename).convert('L')
  • convert the image to array:
    • numpy.asarray(Image.open(filename).convert('L'))


  • Here is a introduction to “Image” package
  • opencv, pillow are the python image library


Use the np.array.argsort() and list.sort() to get the rank of the number

I have a list and I want to get the rank for each member. This is important for many data mining algorithm, for example kNN (k-Nearest-Neighbor), you want to know the rank of a certain record.

For example:

myList = [2, 1, 7, 3, 6]

My expected result:

rank =[1, 0, 4, 2, 3]

Take care, I mean the “rank” not the index of the argument.

Solution 1:

Step 1: Get the sorted index of myList

  • Method 1: use the numpy.array.argsort() to get a list of returned index
    Out: array([1,0,3,4,2])
  • Method 2: use the numpy.array.sort(key=…)
    indices = range[5]
    Out: array([1,0,3,4,2])

Step 2: Turn the index to the rank

  • indices = np.array(myList).argsort()
    [indices.tolist().index(i) for i in range(5)]

Solution 2:

You can do all the things in one strike:

  • ranking = [sorted(list).index(each) for each in list]

sort 2d array in python

There are many ways to sort a 2d arrays.

In general, you can use the sorted(), np.array.sort(),…

But you should also understand

  • what is meaning of list1.sort(list2)
  • ravel() vs. flatten()
  • what return the  key function back in sorted()
  • what will be returned by np.lexsort(), np.argsort()