22 4 1 编程 articles

JUnit常用代码

单元测试模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 import org.junit.After; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.mkcjito.InjectMocks; import org.mkcjito.Mock; import org.mkcjito.Mockito; import org.mkcjito.MockitoAnnotations; public class Test { // 模拟对象 @Mock private ClassA a; // 被测类 @InjectMocks private Instance instance; // 初始化 @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() throws Exception() { // 初始化测试用例类中由Mockito的注解……

Continue reading

Pandas常用代码

最佳迭代方法: 1 2 3 4 5 import pandas as pd from tqdm import tqdm for row in tqdm(df.to_dict(orient="records")): # do something 获取行数和列数 1 2 3 4 5 6 7 import pandas as pd rows = len(df.axes[0]) cols = len(df.axes[1]) rows = df.shape[0] cols = df.shape[1] 分块读取超大文件 1 2 3 4 5 6 7 8 import pandas as pd from tqdm import tqdm data = pd.read_csv('dataset.csv', chunksize=1000) for chunk in data: for row in tqdm(chunk.to_dict(orient="records")): # do something 根据已有单个列扩充新列 1 2 3 4 5 6 import pandas as pd def valFunc(val): return val+1 df['D'] = df['C'].apply(valFunc) 自定义函数筛选 1 2 3 4 5 6 import pandas as……

Continue reading

XGBoost常用代码

显示特征重要性 1 2 3 4 5 6 import pandas as pd f_importance = xbg_reg.get_booster().get_score(importance_type='gain') importance_df = pd.DataFrame.from_dict(data=f_importance, orient='index') importance_df.plot.bar() 辅助数据文件:组输入 适用于排序任务中的pairwise和listwise;组文件自动加载,若数据文件为train.txt,则组文件为train.txt.group;组文件为单列数字,按顺序指示数据文件中每个组的实例数。 辅助数据文件……

Continue reading

TQDM常用代码

与Pandas结合: 1 2 3 4 from tqdm.autonotebook import tqdm for row in tqdm(df.itertuples(), total=df.shape[0], ncols=128): # do something……

Continue reading

Python常用代码

Dict按value排序 1 sorted_obj = {k: v for k, v in sorted(obj.items(), key=lambda item: item[1], reverse=True)} 多线程执行,注意结果的获取方式 1 2 3 4 5 6 7 8 9 10 11 12 from multiprocessing import Pool import os def f(x): print('Child process id:', os.getpid()) return x*2 if __name__ == '__main__': with Pool(5) as p: results = p.map(f, [1, 2, 3]) print(results)……

Continue reading

Python操作Excel常用代码

读Excel,方法一: 1 2 3 4 5 6 7 8 9 10 import xlrd from tqdm import tqdm workbook = xlrd.open_workbook('some.xlsx') sheet = workbook.sheet_by_name('sheet1') #sheet = workbook.sheet_by_index(0) nrows = sheet.nrows for idx in tqdm(range(1, nrows)): data = sheet.cell_value(idx, 0) 写Excel,方法一(支持xls,小于6w行数): 1 2 3 4 5 6 7 8 9 10 11 12 13 import xlwt workbook = xlwt.Workbook() sheet = workbook.add_sheet('sheet1') sheet.write(0, 0, 'Head_1') idx = 1 for row in tqdm(datas): sheet.write(idx, 0, row['data_1']) idx += 1 workbook.save('some.xls') 写Excel,方法二(支持xlsx,大于6w行数): 1 2 3……

Continue reading

正则表达式常用代码

全句匹配: 1 2 3 import re is_match = re.match(r"[regex_str]*", "match_str") 部分匹配: 1 2 3 4 5 6 7 8 9 10 import re matchObj = re.search(r"[regex_str]*", "match_str") if matchObj: print("matchObj.group() : {}".format(matchObj.group())) print("matchObj.group(1) : {}".format(matchObj.group(1))) print("matchObj.group(2) : {}".format(matchObj.group(2))) else: print("No match!!") 正则替换: 1 2 3 import re replace_result = re.sub(r"[regex_str]*", "replace_to", "match_str")……

Continue reading

Jupyter添加kernel

1、切换到对应的虚拟环境,安装ipykernel 1 pip install ipykernel 2、注册kernel 1 python -m ipykernel install --user --name <name> --display-name "Python (name)" 3、删除kernel 1 2 jupyter kernelspec list jupyter kernelspec remove <name>……

Continue reading