Python
lww1993
这个作者很懒,什么都没留下…
展开
-
python中的eval(), repr(), ast.literal_eval()
原创 2022-05-22 10:20:44 · 131 阅读 · 0 评论 -
利用python3调用aliyun API的方法
网上买了个aliyun api, 给出的示例代码是python2的,不能用,所以只能写一个基于python3的代码.import requestshost = 'http://geo.market.alicloudapi.com'path = '/v3/geocode/geo'appcode = '你的appcode'querys='address=%E6%96%B9%E6%81%92%E5%9B%BD%E9%99%85%E4%B8%AD%E5%BF%83A%E5%BA%A7&b.原创 2021-04-22 15:32:33 · 600 阅读 · 0 评论 -
关于在mac中matplotlib/seaborn等绘图中使用中文的问题
有两个命令:fc-list 显示所有本机字体;fc-list :lang=zh family显示本机所有中文字体。比如Songti TC(宋体繁体),Songti SC(宋体简体)添加如下的语句,就可以实现中文显示了。import matplotlib.pyplot as pltplt.rcParams["font.family"]="Songti SC"参考文献:https://matplotlib.org/3.1.0/tutorials/text/tex...原创 2021-04-15 17:52:15 · 444 阅读 · 0 评论 -
kaggle上的start-here-a-gentle-introduction笔记
1.这是一个典型的二分类有监督学习问题,目的是为了预测贷款申请人是否可能违约。2.评价二分类模型一般使用的是AUC值,也就是ROC曲线下方的面积。ROC曲线的纵轴代表的是sensitivity,即True Positive Rate,横轴代表的是1 - specificity,即1 - TrueNegative Rate。AUC取值越大,说明二分类模型的效果越好。代码如下:1. 加载包:# numpy和pandas是为了基本的数据清洗import numpy as ...原创 2021-04-08 10:41:32 · 111 阅读 · 0 评论 -
python计算IV值
1. pdf分页使用的是\newpage 或者 \pagebreak;https://stackoverflow.com/questions/25240541/how-to-add-newpage-in-rmarkdown-in-a-smart-way2. python计算IV值def compute_IV(df, variable, target): """ to compute the IV of the variable df: data f原创 2021-04-08 10:35:48 · 838 阅读 · 0 评论 -
deep learning with python中5.4.3节的visualizing heatmaps代码
书中的代码不能用了。找了一些资料。现将新代码放到这里。from tensorflow.keras.applications.vgg16 import preprocess_input, decode_predictionsfrom tensorflow.keras.preprocessing.image import load_imgfrom tensorflow.keras import preprocessingfrom tensorflow.keras import backend as原创 2021-04-07 17:58:27 · 143 阅读 · 0 评论 -
二分类模型的kaggle notebook笔记
kaggle上有一个这样的比赛:Home Credit Default Risk Competition, 是预测信用卡违约的。有一名作者是Will Koehrsen,他写了一系列的文章,从二分类入门到参数调试,非常详细。https://www.kaggle.com/willkoehrsen/start-here-a-gentle-introduction这是入门的文章,关于基本的数据预处理,包括缺失值的处理,特征工程等,最后采用了logistic model, random forest mo原创 2021-04-02 14:53:01 · 601 阅读 · 0 评论 -
Deep Learning with Python-7.3节学习笔记
本节讲的是搭建高级模型的方法,主要包括1.在某些层中添加如下的方法residual connections,normalization,depthwise separable convolution;2.超参调优,强调了random search的重要性,还有两个工具:Hyperopt, Hyperas;3. 模型组合。强调了不同模型有不同的优点,diversity is strength. 包括深度学习模型,树模型,线性模型等一起的拟合。...原创 2021-04-01 17:50:23 · 62 阅读 · 0 评论 -
利用pickle存储与读取文件对象
# 写文件with open('imdb_data', 'wb') as imdb_data_file: pickle.dump(imdb_data, imdb_data_file)# 读文件with open('imdb_data', 'rb') as imdb_data_file: imdb_data = pickle.load(imdb_data_file)参考https://www.techcoil.com/blog/how-to-save-a...翻译 2021-03-23 18:02:42 · 196 阅读 · 0 评论 -
mac电脑中的python卸载
目前tensorflow2不支持python3.9(2021-3-18),所以只能使用python3.8了.官方文档的卸载说明如下:1. 首先删除应用程序(Applications)下的python3;2. 其次删除/Library/Frameworks/Python.framework下的python版本。需要注意的是不要删除其它地方的python,因为macOS依赖于这些文件。参考文档:https://docs.python.org/3/using/mac.html?highli翻译 2021-03-18 17:03:24 · 1107 阅读 · 0 评论 -
jupyter notebook找不到module但是控制台可以找到的解决办法,同时解决卸载旧版本python安装新版本python出现的问题
pip3 install ipykernel --upgradepython3 -m ipykernel install --user参考文献:https://stackoverflow.com/questions/50914761/jupyter-notebook-cant-find-modules-for-python-3-6转载 2021-03-11 14:10:36 · 878 阅读 · 0 评论 -
http请求中的汉字编码处理
在API请求中,经常需要处理汉字。例如‘北京’,应该转化为‘%E5%8C%97%E4%BA%AC’在python3中如下处理:import urllib.parsequery = '北京'urllib.parse.quote(query)即可。参考:https://www.urlencoder.io/python/#:~:text=URL%20Encoding%20query%20strings%20or%20form%20parameters%20in,uses%20UTF-8e.原创 2020-07-22 15:14:02 · 1205 阅读 · 0 评论 -
python中的CSV大数据读取
主要有两种方式:1. pd.read_csv(file, chunksize=chunk_size)这样可以保证分块读取;下面的博客提供了如下的代码https://blog.csdn.net/qq_36330643/article/details/78739764 def get_df(file, chunk_size=1e5): mylist = [] for chunk in pd.read_csv(file, chunksize=chunk_size): ...原创 2020-06-29 10:22:31 · 1138 阅读 · 0 评论 -
python中利用boolean list检索list的方法
问题如下:numbers = [1, 0, 2, 4]bools = [True, False, False, True]result = [1, 4]通过numbers和bools获取result.方法一:[number for number, b in zip(numbers, bools) if b == True]方法二:np.array(numbers)[np.array(bools)]方法三:from itertools import compress翻译 2020-06-02 15:31:22 · 4135 阅读 · 0 评论 -
用python生成pdf report
1. 安装pip install pdfkitpip install Jinja2同时安装 wkhtmltopdf;https://towardsdatascience.com/creating-pdf-reports-with-python-pdfkit-and-jinja2-templates-64a89158fa2d2. 使用Plotly + Jupyter notebook + xhtml2pdf生成pdf reporthttps://plotly.com/python/v3原创 2020-05-12 12:07:49 · 262 阅读 · 0 评论 -
python中的代替for-loop的操作
1. map(function, list)x = ['a', 'b', 'c']result = map(str.upper, x)list(result)原创 2020-05-09 14:22:15 · 507 阅读 · 0 评论 -
计算文本相似性的方法
常用的有两种度量:Jaccard Similarity与Cosine SimilarityJaccard Similarity的定义如下: 两组文本的交集大小除以两组文本的并集大小;Cosine Similarity的定义如下: 两组文本进行TF或者TF-IDF变换后,化为向量,计算向量夹角的余弦。...翻译 2020-05-07 11:01:37 · 199 阅读 · 0 评论 -
(转载)macOS下的matplotlib的中文显示问题
https://mathsyouth.github.io/2019/06/12/macos-matplotlib1. 查找当前系统的已用字体:fc-list :lang=zh;2. 添加如下代码:from matplotlib import rcParamsfrom matplotlib.font_manager import FontPropertiesimport matp...转载 2020-04-13 15:18:26 · 128 阅读 · 0 评论 -
python调用不同文件中的函数方法
1. 暂时添加文件路径到系统路径中:import syssys.path.append('/Users/abcdefg/Documents/life/miscellaneous/python_function')2. 加载相应的python文件:import file13. file1.function1就可以了需要注意的是: file1的名字不要与python现有的包名冲...翻译 2020-03-27 15:15:46 · 630 阅读 · 0 评论 -
jupytext 同步.ipynb与.py文件方便git管理
1. 安装jupytext文件,python3 -m pip install jupytext --user;2. 生成修改jupyter notebook配置文件jupyter notebook --generate-config -yecho 'c.NotebookApp.contents_manager_class = "jupytext.TextFileContents...原创 2020-03-26 11:49:08 · 676 阅读 · 0 评论 -
python与R中随机种子的设置
为了保证随机结果的可重复性:1. python需要对用到的每个包都设置相应的随机种子,包括random.seed(1234)numpy.random.seed(1234)2.R语言中只用到了set.seed(1234)...原创 2020-02-24 08:14:07 · 1199 阅读 · 0 评论 -
python相关问题汇总
from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = "all"这样就可以保证输出所有的代码表达式了原创 2020-02-20 11:19:46 · 193 阅读 · 0 评论 -
windows下python安装的命令行问题
在windows下安装好python3.7后,开启命令行,如果python找不到的话,需要将python.exe所在的文件夹放到用户的path下;如果pip找不到的话,需要将pip.exe所在的文件夹放到用户的path下。...原创 2020-02-11 16:11:17 · 229 阅读 · 0 评论 -
adding this directory to PATH的命令行方法
export PATH=$PATH:~/opt/binecho $PATHhttps://www.crifan.com/mac_local_install_python_3_pipenv_virtual_environment/翻译 2020-01-02 19:27:19 · 6128 阅读 · 0 评论 -
python中遇到SSL: CERTIFICATE_VERIFY_FAILED error的一个处理办法
用的是macOS的话,Macintosh HD > Applications > Python3.6 folder > 双击Install Certificates.command文件。https://stackoverflow.com/questions/50236117/scraping-ssl-certificate-verify-failed-error-for...转载 2019-06-27 14:54:49 · 399 阅读 · 1 评论