Skip to content

baowj-678/toppush-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TopPush Python Code

本项目是对《Top Rank Optimization in Linear Time》 论文的python代码实现。实现过程参考了官方matlab和c代码,原论文以及代码见official目录。

纯python实现

主体toppush函数以及epne函数都用python实现。

代码文件

  • python/toppush.py

使用方式

直接调用toppush.py中的topPush函数即可。 例如:

w = topPush(X, y)

相关参数以全局变量形式定义:

lambdaa = 1  # radius of l2 ball
maxIter = 10000  # maximal number of iterations
tol = 1e-4  # the relative gap
debug = False  # the flag whether it is for debugging
delta = 1e-6

python+C实现

用python实现toppush函数,用C实现epne函数。

代码文件

  • python_with_c/epne.c
  • python_with_c/toppush.py

使用方式

1.编译epne.c生成epne.so动态链接库文件。

gcc -shared -o epne.so epne.c

2.直接调用toppushWithC.py中的topPush函数即可。 例如:

w = topPush(X, y)

相关参数以全局变量形式定义:

# load C func epne
mylib = cdll.LoadLibrary('python_with_c/epne.so')
epne = mylib.epne

# params
lambdaa = 1  # radius of l2 ball
maxIter = 10000  # maximal number of iterations
tol = 1e-4  # the relative gap
debug = False  # the flag whether it is for debugging
delta = 1e-6

实验对比

主要包括功能性实验对比实验

  • 功能性实验:验证计算结果的正确性;
  • 对比实验:对比两种实现方式的速度差别;

代码文件

  • main.py

实验数据

  • data/spambase.mat 实验数据中的ans字段是利用matlab使用官方提供的代码计算保留的结果,用于验证本项目代码的正确性。

实验结果

--------------------------------------  start functional test --------------------------------------
----------------------------------- start python functional test -----------------------------------
the max diff is: 7.580741590018647e-16 less than 1e-08
---------------------------------- python functional test passed -----------------------------------
------------------------------- start python-with-c functional test --------------------------------
the max diff is: 7.580741590018647e-16 less than 1e-08
------------------------------- python-with-c functional test passed -------------------------------
----------------------------------------- start speed test -----------------------------------------
------------------------------------- start python speed test --------------------------------------
topPush time: 17.7223345 s
------------------------------- start python-with-c functional test --------------------------------
topPushWithC time: 0.7064349 s

Process finished with exit code 0

通过实验可以发现:

  • 功能性实验通过,计算结果在计算误差范围内;
  • 对比实验:python-with-c速度远快于python,建议使用python-with-c

经验教训

numpy.ndarray运算后会生成新的numpy.ndarray,不是在原数据上进行修改。例如(data = -data,会生成新的数据)

About

python code for toppush algorithm

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published