- Remove empty string from a list
word_list = filter(bool, word_list)
- 找打最大 或者最小的 非零数字 in a numpy array (or a tuple) in python
import numpy as np
minval = np.min(a[np.nonzero(a)])
maxval = np.max(a[np.nonzero(a)])
- string 里面的东西 转到 这个东西的格式 本身
import ast
ast.literal_eval("(255, 0, 0)")
(255, 0, 0)
- 在 Mac本地装 numpy
pip install --upgrade --ignore-installed --install-option '--install-data=/usr/local' numpy
-
OS local python need numpy
java.lang.RuntimeException: PipeMapRed.waitOutputThreads(): 1.需要使用本地python ,#!/usr/bin/python #!/usr/bin/env python 2.Numpy !!
-
运行 jupyter
runipy MyNotebook.ipynb
- add value to same key in dict
dict.setdefault( “a” , [ ] ) .append ( b )
- list[:,int] get column ?
Shit u need to convert to np.array
- server use Jupiter
jupyter notebook --no-browser --port=8889
ssh -N -f -L localhost:8888:localhost:8889 asd@xxxx
- product(“abcd”,reprat = 2)
“Ab ” ac ad ba …….
- 所有標點符號 标点符号 用re 替换
re.sub(r'[^\w]', ' ', s)
- make a vocabulary using my own article’s words
from sklearn.feature_extraction.text import CountVectorizer
vectorizer = CountVectorizer()
vectorizer.fit(x)
vectorizer.transform(x)
vectorizer.transform(y)
mydict = vectorizer.vocabulary_
- strip 去掉字串结尾的字元
- split 顾名思义,切割
In [5]: "123,321, 213,,,,".strip(',')
Out[5]: '123,321, 213'
In [6]: "123,321,213".split(',')
Out[6]: ['123', '321', '213']
numpy
内积
np.inner([1,2,3],[2,2,2])
> 12
随机打散
index = np.random.permutation(10)
> array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
new_array = array[index]
Unique, set 之后 再 sort
>>> a=np.random.randint(0,5,8)
>>> a
array([2, 3, 3, 0, 1, 4, 2, 4])
>>> np.unique(a)
array([0, 1, 2, 3, 4])
>>> c,s=np.unique(b,return_index=True)
>>> c
array([0, 1, 2, 3, 4])
>>> s
array([3, 4, 0, 1, 5])(元素出现的起始位置)