Shantanu's Blog

Corporate Consultant

October 27, 2014

 

Generate series numbers

Here is the Mysql code that can be used to quickly generate 5 digit numbers starting from 10000 to 99999

drop table t;
create table t (series int);

set @n = 1;
drop view if exists v3;
drop view if exists v10;
drop view if exists v100;
create view v3 as select null union all select null union all select null;
create view v10 as select null from v3 a, v3 b union all select null;
create view v100 as select null from v10 a, v10 b;
insert into t select @n:=@n+1 from v10 a,v100 b, v100 c;
delete from  t where length(series) != 5;

Labels:


October 01, 2014

 

Processing and understanding context of plain text using python

Gensim is a Python library for topic modelling, document indexing and similarity retrieval with large corpora.

https://github.com/piskvorky/gensim

here is how it works:

!wget http://mattmahoney.net/dc/text8.zip
!unzip text8.zip

from gensim.models import word2vec
sentences = word2vec.Text8Corpus('text8')
model = word2vec.Word2Vec(sentences, size=200)
print model.most_similar(positive=['woman', 'king'], negative=['man'], topn=1)

The output of above is not a surprise:

[('queen', 0.5359965)]

Labels:


September 13, 2014

 

passing parameters to python script

Option parser module will allow us to pass the parameters while calling the script.

import optparse

def main():
    p = optparse.OptionParser()
    p.add_option('--os', '-o', default="LINUX")
    options, arguments = p.parse_args()
    print 'Hello EPM, I like to make packages on %s' % options.os

if __name__ == '__main__':
    main()

Labels:


August 25, 2014

 

analyse general logs

It is possible to analyze general logs using percona query digest:

wget http://www.percona.com/redir/downloads/percona-toolkit/LATEST/deb/percona-toolkit_2.2.10_all.deb
apt-get install percona-toolkit_2.2.10_all.deb
mk-query-digest --type genlog  study2.txt

August 12, 2014

 

converting dates in pandas

Sometimes I get the date column from the csv string is year-month-day without any space or hyphen. for e.g. 120814
This can be converted to regular date using a function and that function can be called in the read_csv method.

import pandas as pd
import datetime as dt

def mydate(x):
    try:
        return dt.datetime.strptime(x, '%y%m%d')
    except ValueError:
        return pd.NaT

vt_data = [ 'vt_code', 'vt_id' , 'point_date' ,  'deviation' ]

vts = pd.read_csv('test.txt', sep=',', names=vt_data, parse_dates=[2], date_parser=mydate)
Another option is not to do parsing as you read the data in, then set the index as shown below.

df.index = pd.to_datetime(df["ticketDate"] + df["ticketTime"], coerce=True, format='%Y-%m-%d %H:%M:%S')

The coerce=True forces bad values to NaT


Labels: ,


Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014  

This page is powered by Blogger. Isn't yours?