python笔记

  1. 根据列属性筛选行
>>> b = np.array([[1,2,3,4],[1,1,3,2],[5,5,5,5],[6,6,6,6]])
>>> print b
[[1 2 3 4]
 [1 1 3 2]
 [5 5 5 5]
 [6 6 6 6]]
>>> print b[b[:,2] == 5]
[[5 5 5 5]]
>>> print b[b[:,2] == 3]
[[1 2 3 4]
 [1 1 3 2]]

2 y= np.argsort(x)
argsort()函数是将x中的元素从小到大排列,提取其对应的index(索引),然后输出到y。

3文件夹拷贝与删除

import shutil

if os.path.exists(dest_folder):
        shutil.rmtree(dest_folder)
shutil.copytree(src_folder, dest_folder)

4 " {}".format()

>>> my_age = 26
>>> my_name = 'xiaoming'
>>> print "My name is {} and I am {} years old.".format(my_name, my_age)
My name is xiaoming and I am 26 years old.
>>> 

5 多线程组件

from multiprocessing import Pool
def func(int):
      ....
      
pool = Pool(5)
pool.map(func, range(0, 10))
#主线程阻塞,等待并发任务完成再往下执行。
pool.join() 

6 终端快速执行 python -> python -c " 多个python 语句, 分号分割语句"
7 脚本命令行解析工具argparse https://docs.python.org/3/howto/argparse.html#id1
8 list/numpy 索引

list[::]
step 整数代表从start->stop方向提取, 负数反之.
a = [1,2,3,4,5]
print a[::1] -> [1,2,3,4,5]
print a[::-1] -> [5,4,3,2,1]
print a[::,2] -> [1,3,5]
print a[::-2] -> [5,3,1]
print a[1:4:] -> [2,3,4]

9 numpy.polyfit

#! usr/bin/env python
import numpy as np
import argparse
#https://docs.scipy.org/doc/numpy/reference/generated/numpy.polyfit.html
parser= argparse.ArgumentParser()
parser.add_argument("-d","--data_path")

args=parser.parse_args()

#data = np.loadtxt(args.data_path, delimiter=" ", usecols=(1,2))

#print data
data = np.array([[0,1,2,3,4,5], [0,-0.0 ,-0.0,0,0,0]]).astype(np.float32)
print data
#estimate function : y = a[0]*x*x + a[1]*x + a[2]
# y = data[1]
# x = data[0]
a = np.polyfit(data[0], data[1],2,full=False , cov=True )
print a

你可能感兴趣的:(python)