
python
天青色的瓷
AI算法工程师
展开
-
超大json文件拆分
背景:330M的json文件,有大概100w+条数据,内部存在格式问题,需手动替换修改办法:拆分为6个小json文件,代码如下:# 读取大json文件with open('binguan_all.json', 'r', encoding='utf-8') as f1: N = [json.loads(line.strip()) for line in f1.readlines()] # 将数据按照每20w条一份,分别存储 total = len(N)//200000 # 为了原创 2020-09-10 16:13:13 · 7452 阅读 · 2 评论 -
jupyter notebook nbextensions安装
若网络速度还可以,之间从GitHub下载:pip install https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tarball/master原创 2020-08-10 17:05:34 · 786 阅读 · 1 评论 -
日期时间datetime与时间戳timestamp互相转换
字符串日期时间转换成时间戳# '2015-08-28 16:43:37.283' --> 1440751417def string2timestamp(strValue): d = datetime.datetime.strptime(strValue, "%Y-%m-%d %H:%M:%S") t = d.timetuple() timeStamp = int(time.mktime(t)) timeStamp = float(str(timeStamp)原创 2020-07-21 11:47:32 · 5183 阅读 · 1 评论 -
Mac中设置默认从anaconda路径打开jupyter notebook
背景:近期update了anaconda的所有包,设置了多个虚拟环境。问题:启动terminal后,输入“jupyter notebook”无法打开jupyter,如下:$ jupyter notebook-bash: jupyter: command not found临时解决方案:(激活conda的base环境,下方路径是anaconda的安装路径)$ source ~/anaconda3/bin/activate(base) ccm ~再输入“jupyter notebook”,就原创 2020-06-29 08:50:11 · 2565 阅读 · 0 评论 -
python中numpy.argsort(),将数组按照大小返回其index
A = array([ 0, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 3, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,原创 2020-05-20 14:18:24 · 3759 阅读 · 0 评论 -
Pandas经过groupby聚合变为Series后,经过排序返回值最大的前2项(TOP2)
希望返回values对应为5和4的两项,及其对应的longitude和latitude值df_agg1.groupby(['longitude','latitude'])['foot_mark'].sum()运行以上返回:longitude latitude121.258340 37.493010 5121.259149 37.496940 1121.259180 37.502860 1121.264030 37.487900 4121.302646原创 2020-05-16 12:32:40 · 487 阅读 · 0 评论 -
正则表达式str.extract后多出1列
想extract匹配的项,结果发现返回了2列train.Ticket.str.extract('(([A-Z]+\.?)+\s*\d*)')返回:标题尝试加入 ?: 后,将(?:[A-Z]+.?)看为一个整体,再运行则只返回1列了。train.Ticket.str.extract('((?:[A-Z]+\.?)+\s*\d*)')返回:如果你的问题解决了,欢迎收藏+点赞+关注哦...原创 2020-04-06 14:46:39 · 544 阅读 · 0 评论 -
seaborn.kdeplot绘制图像显示异常问题
背景:在验证数据集的分布情况时,用kdeplot绘制可视化查看,但发现存在锯齿状、脉冲状的异常图像,乍一看是非常令人费解的。个人数据探索的真实案例(spark环境):from scipy.stats import kstestimport numpy as npimport seaborn as snsimport matplotlib.pyplot as plt# 定义函数kde_...原创 2020-04-04 15:01:57 · 3284 阅读 · 0 评论 -
pyspark.sql.functions.udf中使用numpy,出现Py4JJavaError错误
背景:pyspark,通过udf定义函数,以辅助添加新列出错原因:udf不能返回numpy类型举例:df.head()Row(artist=‘Martha Tilston’, auth=‘Logged In’, firstName=‘Colin’, gender=‘M’, userId=‘30’, hour=8)# 切割时间,每6个小时为一组get_6hour = udf(lamb...原创 2020-02-15 10:41:56 · 660 阅读 · 0 评论 -
progressbar进度条在python中的异常和正确使用方法
conda安装progressbar包conda install -c anaconda progressbarprogressbar的导入和应用(出现异常!)import progressbartotal = 10000000bar = progressbar.ProgressBar(maxval=total+1, widgets=[progressbar.Bar('=', '[',...原创 2020-01-20 21:32:36 · 1747 阅读 · 4 评论 -
【解决方法】EOFError:Compressed file ended before the end-of-stream marker was reached
错误原因文件下载中断后,重新下载,或者文件已经下载完成。解决办法删除已经下载的文件,例如运行此代码时出错:testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)**则在电脑中搜索“F_MNIST_data”,将该文件夹删除即...原创 2019-11-30 14:34:43 · 6644 阅读 · 0 评论 -
torch.sum(),dim=0,dim=1解析
直接上代码:# 定义一个2维张量aa = torch.tensor([[1,2,3],[4,5,6]])print(a)a.shapetensor([[ 1, 2, 3], [ 4, 5, 6]])torch.Size([2, 3])# 2行3列,没毛病dim=0,降维(纵向压缩)b = torch.sum(a,dim=0)print(b)...原创 2019-11-27 20:01:10 · 20922 阅读 · 10 评论 -
TypeError: unhashable type: 'list'解决办法。
错误原因:因为list、set、dict:是不可哈希的什么意思?举2个不同栗子:例1:list_0 = [1,2,3,4]s = set(list_0)print(s)>>>{1, 2, 3, 4}原创 2019-11-16 16:15:40 · 99480 阅读 · 1 评论 -
from collections import Counter()
计数器Counter()的应用示范:from collections import Counterlist_01 = ['A','C','S','A','B','f','S','A']dict_01 = Counter(list_01)print(dict_01)将会直接输出一个字典,内容是列表中的元素及其出现频数:Counter({'A': 3, 'S': 2, 'C': 1, '...原创 2019-11-16 09:02:23 · 939 阅读 · 0 评论 -
target is multiclass but average='binary'. please choose another average setting.
引用sklearn模型,fit之后进行模型评估时,出现该错误,代码如下:from sklearn.tree import DecisionTreeClassifierfrom sklearn.metrics import f1_scoreregressor = DecisionTreeClassifier(random_state=42)regressor.fit(X_train,y_t...原创 2019-11-05 18:33:43 · 5321 阅读 · 1 评论 -
feature_importances_提取特征重要性的应用
直接上代码!# 在训练集上训练一个监督学习模型model = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=3),n_estimators=8)model.fit(X_train,y_train)# 提取特征重要性importances = model.feature_importances_#...原创 2019-10-29 21:41:11 · 5848 阅读 · 1 评论 -
typeerror: 'float' object is not callable问题的解决
首先,这不是个特别难解决的问题,问题出现的原因可能有以下3种:1,因为coding马虎,导致的语法错误!函数/方法调用时的拼写错误! :此情况至少有80%的可能!至少有80%的可能!至少有80%的可能!重要的事情说三遍!2,函数名和变量名一致导致。 如 mean = mean(***)3,运算符号遗漏。 如a = bc 应改为 a= b*c...原创 2019-10-29 21:35:52 · 8973 阅读 · 1 评论 -
sklearn中train_test_split里,参数stratify含义解析
直接上代码:from sklearn.model_selection import train_test_split# 将'features'和'result'数据切分成训练集和测试集X_train, X_test, y_train, y_test = train_test_split(features, result, test_size = 0.2, random_state = 0,...原创 2019-10-27 15:28:08 · 32992 阅读 · 18 评论 -
Python sklearn错误:Expected 2D array, got scalar array instead…Reshape your data…
bmi_life_model.fit(x,y)bmi_life_model.predict(21.079)ValueError: Expected 2D array, got scalar array instead:array=21.079.Reshape your data either using array.reshape(-1, 1) if your data has a ...原创 2019-10-20 10:42:42 · 2271 阅读 · 6 评论 -
python路径拼接os.path.join()
os.path.join()函数:连接两个或更多的路径名组件1.如果各组件名首字母不包含’/’,则函数会自动加上2.如果有一个组件是一个绝对路径,则在它之前的所有组件均会被舍弃3.如果最后一个组件为空,则生成的路径以一个’/’分隔符结尾用法:ebert_review_urls = [‘https://d17h27t6h515a5.cloudfront.net/topher/2017/S...原创 2019-08-17 10:00:22 · 1241 阅读 · 0 评论