Skip to content

Instantly share code, notes, and snippets.

View dongguosheng's full-sized avatar

董国盛 dongguosheng

  • Sogou
  • Beijing
View GitHub Profile
# -*- coding: gbk -*-
import math
def cal_ndcg(r_list, k):
idcg = cal_dcg(sorted(r_list, reverse=True), k)
if idcg <= 1e-10:
return 1.0
return cal_dcg(r_list, k) / idcg
@dongguosheng
dongguosheng / test_omp.cpp
Last active April 13, 2016 11:40
compare normal, c++11 thread and openmp
#include <iostream>
#include <ctime>
#include <random>
#include <vector>
#include <thread>
#include <omp.h>
using namespace std;
const static int TOTAL_SIZE = 100000;
@dongguosheng
dongguosheng / kmeans.h
Last active April 14, 2016 11:46
a simple k-means implementation, use c++11 and openmp
#ifndef KMEANS_H
#define KMEANS_H
#include <vector>
#include <set>
#include <random>
#include <ctime>
#include <cfloat>
#include <cassert>
#include <cmath>
#include <iostream>
@dongguosheng
dongguosheng / solve.py
Last active March 1, 2016 12:55
toy code about gradient descent, newton method.
# -*- coding: gbk -*-
import numpy as np
def foo(x):
return x**2 + 2*x + 1
def g(x):
return 2*x + 2
@dongguosheng
dongguosheng / lr.py
Last active February 29, 2016 13:05
a simple logistic regression implementation, test on heart_scale dataset.
# -*- coding: utf-8 -*-
'''
Feature vectors are column vectors.
X, n * m, n -> feature dim, m -> sample number
Y, 1 * m, row vector
theta/weight/beta, n * 1, column vector
gradient, n * 1, column vector
gradient = X * (Y - sigmoid(theta' * X))'
@dongguosheng
dongguosheng / mail_util.py
Last active August 29, 2015 14:18
Send mail util with attachment
# -*- coding: utf-8 -*-
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
class Message(object):
'''Email util.'''
def __init__(self, mailto_list=['xxxxxx@qq.com'], mail_host='smtp.126.com',
mail_user='yyyyyy', mail_password='zzzzzz', mail_postfix='126.com', attach_file_list=[]):
@dongguosheng
dongguosheng / df_mp.py
Last active August 29, 2015 14:05
Hadoop Streaming计算tfidf
#! /usr/bin/python
import sys
def read_input(file):
'''
read input tf mapper-reducer file
class:Queries\tword_1:val_1,word_2:val_2,.....
'''
for line in file:
@dongguosheng
dongguosheng / StringUtils.cpp
Last active September 8, 2015 03:15
C++ utils
#include <string>
#include <vector>
#include <sstream>
using std::vector;
using std::string;
using std::stringstream;
vector<string> &split(const string &s, char delim, vector<string> &result) {
stringstream ss(s);
@dongguosheng
dongguosheng / TestJsoup.java
Last active August 29, 2015 13:56
第一次使用jsoup
package test_jsoup.test_jsoup;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class App
{
public static void main( String[] args ) throws Exception
@dongguosheng
dongguosheng / AutoSwitch.py
Last active January 3, 2016 08:29
TJU更改pppoe后查询流量和账户余额,以及切换账户的小脚本。顺便也体验了一下getter和setter的语法糖和yaml。 之前为固定IP时,切换账户的同时需要切换IP以及mac地址,当时粗暴地使用os.system+cmd和_winreg模块拼凑而成。使用cPickle存储。 运行环境为win server 2003,使用pyinstaller-2.0做成单exe。
# -*- coding: utf-8 -*-
import requests
from pyquery import PyQuery as pq
import yaml
from hashlib import md5
from datetime import datetime
import smtplib
from email.mime.text import MIMEText