jupyter notebook美化
pip install jupyterthemes
jt -t gruvboxl -f fira -fs 13 -cellw 90% -ofs 11 -dfs 11 -T #-T为显示工具栏
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
jupyter nbextension enable codefolding/main #实现代码折叠
jupyter nbextension enable code_prettify/code_prettify #代码美化
jupyter nbextension enable collapsible_headings/main #标题折叠
调用matlab
import matlab
import matlab.engine
engine = matlab.engine.start_matlab()
保存成mat格式
scio.savemat("F:MATLAB Code/xyz_ellipse.mat",{'X':X,'Y':Y,'Z':Z})
求逆矩阵
mat.I
os操作
import os
os.remove(file) #删除文件
os.path.getsize(file) #获取文件大小
os.chdir(directory) #切换到directory目录
cwd = os.getcwd() #获取当前目录即dir目录下
os.walk(dir) #获取当前dir目录下所有文件和文件夹,以及子文件夹里的文件
for root,dirs,files in os.walk(dir):
print(root,dirs,files)
os.listdir(dir) #获取dir目录下的文件夹和文件,不包含子文件夹
DataFrame
df.set_index(["Column"], inplace=True) #将某一列作为index
df.rename(columns={'a':'dddd'}, inplace=True) #重命名列名
Powershell
Remove-Item ---- rm #删除文件
数组排序
a.sort() #默认从大到小排序,a发生改变
a.sort(reverse=True) #从小到大
a.reverse() #反转
sorted(a,reverse=True) #从大到小
np.argwhere(np.isnan(a)) #找到a中nan所在的位置
test[np.logical_not(np.isnan(test))] #去除nan
test[~np.isnan(test)] #去除nan
将结果写入excel
#将结果存入excel文件
writer = pd.ExcelWriter('result.xlsx',engine='openpyxl')
df.to_excel(excel_writer=writer,sheet_name='原始数据',encoding='utf-8',index=False)
dfr.to_excel(excel_writer=writer,sheet_name='结果',encoding='utf-8',index=False)
writer.save()
writer.close()
workbook = xlsxwriter.Workbook('result.xlsx')
worksheet = workbook.add_worksheet('原始数据')
worksheet.set_column('A:A',30)
for col in range(len(df.columns)):
worksheet.write(0,col,df.columns[col])
dotIndex = [i.start() for i in re.finditer('\.', ff)] #获取字符串的某个字符的所有索引
集合展开
Values = list(set([item.strip() for sublist in Values for item in sublist]))
计算字符串中某个字符出现的字数
s = "Count, the number of spaces."
print s.count(" ")
x = "I like to program in Python"
print x.count("i")
读取csv
df = pd.read_table('11.csv',engine='python',header=None,sep=',')
合并PDF
from PyPDF2 import PdfFileReader,PdfFileWriter
pdf1 = PdfFileReader('data/ml1.pdf')
pdf2 = PdfFileReader('data/ml2.pdf')
pdf_writer = PdfFileWriter()
for k in range(148):
pdf_writer.addPage(pdf1.getPage(k))
pdf_writer.addPage(pdf2.getPage(k))
with open('data/result.pdf','wb') as f:
pdf_writer.write(f)
ord('a')
#将字符转换为ASCII码
assert(condition) #断言函数,如果condition不正确,则报错
2019.07.04
sortedClassCount = sorted(potential_culprits.items(),
key=operator.itemgetter(1),
reverse=True)
#字典排序
2019.07.13
a = [0,1,2,3,4,5,6,7,8,9]
b = filter(lambda x:x%4==0,a )
2019.07.26
最小包围矩形和最小包围圆
import cv2
import numpy as np
img = cv2.pyrDown(cv2.imread("hammer.jpg", cv2.IMREAD_UNCHANGED))
ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY) , 127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
# find bounding box coordinates
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(img, (x,y), (x+w, y+h), (0, 255, 0), 2)
# find minimum area
rect = cv2.minAreaRect(c)
# calculate coordinates of the minimum area rectangle
box = cv2.boxPoints(rect)
# normalize coordinates to integers
box = np.int0(box)
# draw contours
cv2.drawContours(img, [box], 0, (0,0, 255), 3)
# calculate center and radius of minimum enclosing circle
(x,y),radius = cv2.minEnclosingCircle(c)
# cast to integers
center = (int(x),int(y))
radius = int(radius)
# draw the circle
img = cv2.circle(img,center,radius,(0,255,0),2)
cv2.drawContours(img, contours, -1, (255, 0, 0), 1)
cv2.imshow("contours", img)
cv2.waitKey()
cv2.destroyAllWindows()
离线安装pytorch的命令
conda install numpy mkl cffi
conda install --offline pytorch-0.1.12-py36_0.1.12cu80.tar.bz2 #离线安装
pip install torchvision==0.1.6
列表去重且顺序不变
l2 = sorted(set(l1),key=l1.index)
模拟鼠标滚动
import win32api
import win32con
win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL,0,0,-1)