顯示具有 Python 標籤的文章。 顯示所有文章
顯示具有 Python 標籤的文章。 顯示所有文章

2017年10月25日 星期三

Pytorch RuntimeError: t() expects a 2D Variable, but self is 3D

RuntimeError: t() expects a 2D Variable, but self is 3D
As of PyTorch v0.2, the .t() function is only applicable to 2D Variables: pytorch/pytorch@8d33603. 

https://github.com/facebookresearch/ParlAI/issues/270
https://discuss.pytorch.org/t/how-to-transpose-3d-matrix-in-pytorch-v0-2/7102

UnicodeDecodeError

python3 使用 python2 pickle
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)

2017年7月27日 星期四

pyodbc connect to sql server

Install:

https://github.com/mkleehammer/pyodbc/wiki/Install

code:
import pyodbc
#connect to db
conn = pyodbc.connect(
    r'DRIVER={ODBC Driver 13 for SQL Server};'
    r'SERVER=127.0.0.1;'
    r'DATABASE=DB_table;'
    r'UID=yasam;'
    r'PWD=password'
    )
cursor = conn.cursor()
sqlInsert="INSERT INTO [dbo].[test_table](RecordID,Model,SubmitDate,CountryCode,Score,Comment) VALUES "

for i, d in enumerate(df):
    print(i)
    #truncate to 1024 for db size
    if len(d[5])>1024:
        d[5]=d[5][:1024]
    if len(d[1])>32:
        d[2]=d[1][:32]
    d[5]=d[5].replace("'","''") # Comment, replace ' for sql
    d[2]=d[2].replace("'","''") #Model
    d[4]=str(d[4]) # float to str
    temp="("+",".join(["N'"+dd+"'" for dd in d])+")" #N for encoding
    tList.append(temp)
    if i == len(df)-1:
        text=','.join(tList)
        cursor.execute(sqlInsert+text)    #last insert
    elif i % 10 == 9:
        text=','.join(tList)
        print(text)
        cursor.execute(sqlInsert+text)   #batch insert
        tList=[]
        temp=''
conn.commit()


reference:
pyodbc 用法
https://my.oschina.net/zhengyijie/blog/35587

Inserting multiple rows in a single SQL query
https://stackoverflow.com/questions/452859/inserting-multiple-rows-in-a-single-sql-query

Pyodbc query string quote escaping
使用兩個''避免或用?方式



unicode 問題(沒遇到)
http://blog.csdn.net/samed/article/details/50539742

2017年7月13日 星期四

python3 gensim id2token not fund

rspList=sorted(glob.glob('./data/*'))
df=[]
for rsp in rspList:
    data=pd.read_csv(rsp)
    df.append(data)  
df=pd.concat(df)
stoplist= set('i am you are he she is a for of the and to in'.split())
sents=df['translated_feedback'][df['translated_feedback']!='\\N'] #remove no response
texts=[[word for word in sent.translate(trans_table).lower().split()
        if word not in stoplist] for sent in sents.values] #remove stopwords and punctuation
texts=list(filter(None,texts)) #filter empty list
#print(texts)
feq=defaultdict(int)
for text in texts:
    for token in text:
        feq[token]+=1

texts=[[token for token in text if feq[token]>1] for text in texts] #remove low frequency

dic=corpora.Dictionary(texts) #build dictionary
 
dic.save('./dictionary.dict')
#print(dic)
corpus =[dic.doc2bow(text) for text in texts] #build bag of words corpus
corpora.MmCorpus.serialize('./corpus.mm',corpus)
#print(corpus)

when i want to get id2token in dic, it is empty dictionary {}
dic.id2token
{}

I have to traverse(iterate) the dic ones
for k,v in dic.items():
    pass
dic.id2token
{0:'yes',1:'got',2:'it'}

2014年8月1日 星期五

python virtualenv packages path not found

I practice to install django with virtualenv in windows.
When i type "django-admin.py startproject mysite", 
it can not import django.core and the error message is    
" from django.core import management
ImportError: No module named django.core"

find the solution from below link

This is because, it still use the python.exe in default (C:\python27) not the python.exe in my virtualenv (D:\venv\Scripts).

using the ftype to check the Python.file
D:> ftype Python.file

using the ftype to change the Python.file
D:\ftype Python.file = "D:\venv\Scripts\python.exe" "1%" %* 

2013年11月27日 星期三

python search path

current directory(sys.path)-->PYTHONPATH-->installation-dependent default path; on Unix, this is usually "/usr/local/lib/python".

2013年6月26日 星期三

flask

$ sudo easy_install flask
$ sudo easy_install flask-wtf                     //for wtf
$ sudo easy_install sqlalchemy-migrate    //for db
sqlalchemy-migrate==0.7.2 is not compatible with SQLAlchemy 0.8.x. install SQLAlchemy 0.7.10 instead with pip install SQLAlchemy==0.7.10 or easy_install QLAlchemy==0.7.10


2013年5月30日 星期四

pyinstaller

convert your py file to exe file
user who can execute your program without python
Install:
just unzip to your file folder

Usage:
python pyinstaller.py /path/to/yourscript.py
or
python /path/to/pyinstaller.py yourscript.py

2012年12月24日 星期一

dist-packages

source from :
http://stackoverflow.com/questions/9387928/whats-the-difference-between-dist-packages-and-site-packages

dist-packages is a Debian-specific convention that is also present in its derivatives, like Ubuntu.

/usr/lib/python2.7/dist-packages

easy_install and pip are installed from the package manager, they also use dist-packages, butthey put packages here:
/usr/local/lib/python2.7/dist-packages

you manually install Python from source, it uses the site-packages directory.


2011年6月9日 星期四

open cv in python

Step 1: Download and install Python 2.7 & OpenCV 2.2
Step 2: Download and install NumPy 1.5.1 & SciPy 0.9.0rc5
Step 3: Setup Windows Path in Environment Variables(Add “C:/Python2.7;C:/OpenCV2.2/bin” to PATH)
Step 4:  Copy the content of folder “C:/OpenCV2.2/Python2.7/Lib/site-packages” to folder “C:\Python27\Lib\site-packages“

2011年5月31日 星期二

Python

some python skills

python tab complete
import rlcompleter, readline
readline.parse_and_bind('tab: complete')