
python
千行百行
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
sklearn中GradientBoostingClassifier bug:ValueError: Input contains NaN, infinity or a value too large
sklearn的GradientBoostingClassifier真的支持缺失值处理吗???原创 2022-04-22 17:05:44 · 2637 阅读 · 0 评论 -
TypeError: ‘<=‘ not supported between instances of ‘list‘ and ‘int‘
debug原创 2022-04-17 22:06:31 · 8837 阅读 · 0 评论 -
ValueError: cannot convert float NaN to integer
文章目录问题复现报错解决方案问题复现import numpy as npa = np.arange(10)a[1] = np.nan报错ValueError: cannot convert float NaN to integer解决方案a.astype(float)a[1] = np.nan先把a转化成float类型的即可。原创 2022-02-26 12:47:24 · 12322 阅读 · 1 评论 -
TypeError: ‘coo_matrix‘ object is not subscriptable
错误复现from scipy.sparse import coo_matriximport numpy as nprow = np.array([0, 3, 1, 0])col = np.array([0, 3, 1, 2])data = np.array([4, 5, 7, 9])coo = coo_matrix((data, (row, col)), shape=(4, 4))element = coo[0, 0]报错TypeError: 'coo_matrix' object原创 2022-01-19 17:10:24 · 3615 阅读 · 0 评论 -
如何统计二维或者多维空间/二维数组中重复元素的数量/计数?numpy一行代码就行了
统计重复元素的数量,不仅仅适用于一维,而且适用于二维或者多维。原创 2022-01-05 21:15:47 · 4060 阅读 · 0 评论 -
sklearn的分位数回归(Quantile Regression)耗时太长了
文章目录问题重现问题描述see also问题重现from sklearn.linear_model import QuantileRegressorimport numpy as npn_samples, n_features = 10000, 1rng = np.random.RandomState(0)y = rng.randn(n_samples)X = rng.randn(n_samples, n_features)reg = QuantileRegressor(quantile=0原创 2021-12-23 23:22:24 · 2584 阅读 · 0 评论 -
【python库使用体验】使用Playwright写爬虫方便吗?
文章目录我的观点安装的问题社区没成气候看看作者是谁我的观点毫不客气地说,一点都不方便。简直是刚进门,就有个大坑!下边细细说来安装的问题pip install --upgrade pippip install playwrightplaywright install居然还需要playwright install!!!就这一步估计把好多用户拒之门外。反正我是没搞定。我搜了一大把文档,有说下载Chromium,Firefox,WebKit的免安装压缩包安装的。但是照着做了依然没能成功。社区没成原创 2021-12-19 21:08:13 · 1705 阅读 · 4 评论 -
matplotlib中线宽linewidth的默认值是多少?如何查看?如何设置/设定?如何修改?
文章目录绘图读取线宽修改线宽see also绘图使用下边的代码绘图:import numpy as npimport matplotlib.pyplot as pltx = np.arange(0.0, 2.0, 0.02)y1 = np.sin(2 * np.pi * x)y2 = np.exp(-x)line1, = plt.plot(x, y1)line2, = plt.plot(x, y2)绘制结果如下图所示:读取线宽使用下边的代码,读取线宽linewidth的默认值,发原创 2021-11-10 16:36:50 · 7637 阅读 · 0 评论 -
numpy API 速查手册
文章目录说明手册目录1. 数组对象2. 常数3. 通用函数 ( ufunc)4. 常用操作(Routines)5. 打字 ( numpy.typing)6. 全局状态7. 包装 ( numpy.distutils)8. NumPy Distutils - 用户指南9. NumPy C-API10. NumPy 内部结构11. SIMD 优化12. NumPy 和 SWIGsee also说明本文是官方手册的汉化版。为了便于阅读,下边给出了手册目录,从目录可以链接到对应的内容。我会不定期更新里边的内容原创 2021-10-27 20:16:56 · 938 阅读 · 0 评论 -
使用matplotlib绘图添加标题title时出现TypeError: ‘Text‘ object is not callable,这么改就对了
文章目录错误复现报错解决办法plt.title和ax.set_titlesee also错误复现import matplotlib.pyplot as pltimport numpy as npimport mathx = np.arange(1000)y = np.sin(2*math.pi*x/1000)fig, ax = plt.subplots()ax.plot(x, y)ax.title("sine") # 报错:TypeError: 'Text' object is no原创 2021-10-17 22:09:17 · 9086 阅读 · 0 评论 -
不会吧!!!python中numpy的append竟然没有list的append快???
文章目录有代码有真相有图有真相结果比较有代码有真相# -*- coding: utf-8 -*-# Time : 2021/9/15 上午12:06# File : list_pk_numpy.py# IDE : PyCharmimport numpy as npimport timea = [0]b = np.array(a)n = 1000tic = time.time()for i in range(n): a.append(i)toc = time.tim原创 2021-09-15 00:19:00 · 747 阅读 · 2 评论 -
【Matplotlib系列】绘制折线图时横轴太长,如何选中部分区域进行放大?不妨试试matplotlib的高级交互功能Span Selector
文章目录适用场合代码效果拓展Reference适用场合折线图横轴太长的场合。或者举一个更具体的例子,如果绘制一只股票十年以来每天的收盘价,那么大概有2500条数据,横轴跨度很大,如果只想看一部分时段的数据,就可以用到Span Selector。用了这个功能你就不需要不停地选择数据,不停地plt.plot了。可以大大提高效率。代码import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.widgets import SpanS原创 2021-08-07 21:24:51 · 2736 阅读 · 1 评论 -
python class ‘str‘ 字符串转换为 class ‘pandas._libs.tslibs.timestamps.Timestamp‘时间戳
>>> import pandas as pd>>> pd.to_datetime('2021-02-17')Timestamp('2021-02-17 00:00:00')>>> type('2021-02-17')<class 'str'>>>> type(pd.to_datetime('2021-02-17'))<class 'pandas._libs.tslibs.timestamps.Timest原创 2021-07-16 18:17:52 · 3420 阅读 · 0 评论 -
pip cache 目录下的文件可以删除吗?
文章目录正文see also正文Windows系统中“%LocalAppData%\pip\Cache”,这个地址下的缓存是可以删除的。不放心的话可以先放到回收站几天,没问题了再清空即可。see also图片来自stackoverflow原创 2021-07-12 20:53:11 · 17356 阅读 · 0 评论 -
【官方教你】提高效率超30倍!matplotlib绘制散点图plt.scatter太慢了!
代码see alsomatplotlib官方cheatsheets(GitHub链接)截图如下:吐槽既然plot这么强,为什么不把scatter搞得强一点呢?希望matplotlib再接再厉!原创 2021-07-08 20:28:53 · 7265 阅读 · 0 评论 -
TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
文章目录错误复现报错解决办法错误复现# python代码print('1除以2等于%f' %1/2)报错TypeError: unsupported operand type(s) for /: 'str' and 'int'解决办法加上括号即可>>> print('1除以2等于%f' %(1/2))1除以2等于0.500000...原创 2021-07-08 20:14:13 · 6153 阅读 · 1 评论 -
ValueError: non convertible value 2021-02-09 20:22 with the unit ‘s‘
文章目录错误复现报错及原因解决方案错误复现import pandas as pdpd.to_datetime('2021-02-09 20:22', unit='s')报错及原因ValueError: could not convert string to float: ‘2021-02-09 20:22’ValueError: non convertible value 2021-02-09 20:22 with the unit ‘s’仔细看官方文档发现,使用unit这个参数的时候,第一原创 2021-05-10 23:06:28 · 2808 阅读 · 1 评论 -
DeprecationWarning: parsing timezone aware datetimes is deprecated
错误复现import numpy as pya = np.datetime64('1970-01-01T08:00:00') - np.datetime64('1970-01-01T08:00:00Z')报警及原因DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future'1970-01-01T08:00:00Z’中的“Z”表示时区,仔细看两个原创 2021-05-10 22:36:56 · 907 阅读 · 0 评论 -
三秒搞清楚:numpy两种展平方法(ravel、flatten)的区别
结合代码,三秒搞清楚import numpy as npa = np.arange(5).reshape(-1, 1)b = a.ravel()c = a.flatten()a[0] = 2 # 对a进行修改修改a时,b发生了变化>>> barray([2, 1, 2, 3, 4])修改a时,c并未发生变化>>> carray([0, 1, 2, 3, 4])...原创 2021-04-13 21:03:17 · 1388 阅读 · 0 评论 -
scipy.signal.medfilt出现UserWarning: kernel_size exceeds volume extent: the volume will be zero-padded
文章目录错误复现错误原因see also错误复现import scipy.signal as signalimport numpy as npx = np.arange(0, 100, 10)np.random.shuffle(x)x_filtered = signal.medfilt(x, 9)# 以下是错误示例,就是下面的代码触发了UserWarningx_1 = x.reshape(-1, 1)x_filtered_1 = signal.medfilt(x_1, 9)从结果中(原创 2021-04-11 21:35:15 · 1119 阅读 · 1 评论 -
python从list中提取多个下标/索引不连续的元素
从list中提取第1、3、6个元素,代码如下import numpy as npa = [1,2,3,4,5,6,7,8]a_ndarray = np.array(a)b = a_ndarray[[0,2,5]]原创 2021-04-07 21:06:27 · 20882 阅读 · 0 评论 -
python TypeError: list indices must be integers or slices, not list
文章目录错误复现报错及原因解决办法错误复现a = [1,2,3]b = a[[0,2]]报错及原因TypeError: list indices must be integers or slices, not listlist数据结构不支持从list中取两个下标/索引不连续的元素解决办法a = [1,2,3]import numpy as npb = [a[0], a[2]]当然这种解决办法略显笨拙,而且如果想提取的元素很多的话就很麻烦,更好的解决办法详见:python从list中原创 2021-04-07 20:58:15 · 24544 阅读 · 0 评论 -
python TypeError: list indices must be integers or slices, not tuple
文章目录错误复现报错及原因解决办法错误复现a = [1,2,3]b = a[(0,2)]报错及原因TypeError: list indices must be integers or slices, not tuplelist数据结构不支持从list中取两个下标/索引不连续的元素解决办法a = [1,2,3]import numpy as npb = [a[0], a[2]]当然这种解决办法略显笨拙,而且如果想提取的元素很多的话就很麻烦,更好的解决办法详见:python从list原创 2021-04-07 20:58:04 · 774 阅读 · 0 评论 -
python TypeError: ‘numpy.ndarray‘ object is not callable
文章目录错误复现报错解决办法错误复现import numpy as npa = np.arange(5)b = a(0)报错TypeError: ‘list’ object is not callable解决办法把第三行的圆括号改成方括号即可,取第1个元素是用方括号!import numpy as npa = np.arange(5)b = a[0]...原创 2021-04-07 20:17:42 · 1650 阅读 · 0 评论 -
python TypeError: ‘list‘ object is not callable
文章目录错误复现报错解决办法错误复现a = [1,2,3]b = a(0)报错TypeError: ‘list’ object is not callable解决办法把第二行的圆括号改成方括号即可,取第一个元素是用方括号!a = [1,2,3]b = a[0]原创 2021-04-07 20:02:44 · 430 阅读 · 0 评论 -
python分别使用dtw、fastdtw、tslearn、dtaidistance四个库计算dtw距离,哪个计算速度最快?
用python计算DTW(Dynamic time warping)距离,哪个库最快?原创 2021-04-03 22:51:05 · 15373 阅读 · 13 评论 -
python数组实现差分操作:后一项减去前一项,两项作差
可以借助np.diff实现,示例如下:>>> x = np.array([1, 2, 4, 7, 0])>>> np.diff(x)array([ 1, 2, 3, -7])numpy官方文档:https://numpy.org/doc/stable/reference/generated/numpy.diff.html原创 2021-04-03 15:33:09 · 11330 阅读 · 0 评论 -
时间序列经典python库集锦
prophet:https://github.com/facebook/prophettslearn:https://github.com/rtavenar/tslearntsfresh:https://tsfresh.readthedocs.io/en/latest/原创 2021-04-01 22:30:58 · 259 阅读 · 0 评论 -
缩尾处理(winsorize)-数据分析、数据处理
话不多说,直接搬运scipy.stats.mstats.winsorize一个例子说的清清楚楚,而且还把轮子也搬出来了。python调个包就能用了原创 2020-07-10 22:21:47 · 84253 阅读 · 2 评论 -
python求一组数据的次最大值或次最小值
import numpy as npa = np.array([5, 6, 7, 8, 9, 10, 11, 12, 13, 14])np.random.shuffle(a) # 打乱顺序a_max = np.partition(a,-1)[-1] # 最大值a_2nd_max = np.partition(a,-2)[-2] # 次最大值a_min = np.partition(a,0)[0] # 最小值a_2nd_min = np.partition(a,1)[1] # 次最小值原创 2021-03-23 22:44:37 · 4434 阅读 · 0 评论 -
scipy使用python寻找时间序列的极大值(局部最大值)或极小值(局部最小值),极值点
讲道理不如举例子,下边结合代码快速说明如何求极值>>> from scipy.signal import argrelextrema>>> x = np.array([2, 1, 2, 3, 2, 0, 1, 0])>>> argrelextrema(x, np.greater)(array([3, 6]),)上边的代码是什么意思呢?下边绘图说明一下。运行如下代码,得到下边的图片:import matplotlib.pyplot as pl原创 2021-03-20 22:09:41 · 13231 阅读 · 0 评论 -
python matplotlib 绘制双Y轴曲线图,两个坐标轴的刻度不同、比例不同
import numpy as npimport matplotlib.pyplot as plt# 创建模拟数据t = np.arange(0.01, 10.0, 0.01)data1 = np.exp(t)data2 = np.sin(2 * np.pi * t)fig, ax1 = plt.subplots()color = 'tab:red'ax1.set_xlabel('time (s)')ax1.set_ylabel('exp', color=color)ax1.plo原创 2021-03-18 20:33:43 · 19520 阅读 · 0 评论 -
使用Python快速打开一个千万级别的超大Excel文件,提速数百倍
代码很短,为了节省时间就不说思路了,直接看下边的代码就可以。import pandas as pd # pandas版本0.25.1import numpy as np # numpy版本1.16.3import timet0 = time.time()data = pd.read_excel( 'data.xlsx', encoding='gb2312' # 添加encoding='gb2312'使得能够读取中文)t1 = time.time()cost1 = t1原创 2021-03-14 21:17:16 · 4971 阅读 · 1 评论 -
AttributeError: ‘AxesSubplot‘ object has no attribute ‘ylabel‘,matplotlib报错
解决bug原创 2021-03-12 11:51:49 · 10279 阅读 · 3 评论 -
python使用matplotlib的plt.subplot、plt.subplots绘制多图以及图例legend注意事项
一个图片里边绘制多个图像是绘图中的常见需求。下边介绍几种实现方法,然后简单分析一下他们的细微区别,并说明了添加legend时候常见的一些坑。原创 2021-03-10 20:16:26 · 8254 阅读 · 2 评论 -
sklearn/scikit-learn保证程序执行结果可复现/可重复的诀窍:random_state
sklearn官方文档:random_state原创 2021-02-25 15:25:32 · 425 阅读 · 0 评论 -
从AttributeError: module ‘pandas‘ has no attribute ‘dataframe‘说开去
提供了排除bug的解决方案,额外提出了防止出现此类bug的措施原创 2021-02-12 17:56:59 · 5748 阅读 · 1 评论 -
sklearn/scikit-learn孤立森林(IsolationForest)中decision_function和score_samples函数的区别和联系
讲清楚decision_function和score_samples到底是干什么的原创 2021-02-01 23:55:24 · 3568 阅读 · 0 评论 -
一句话告诉你如何在pycharm安装PyEMD?无需使用pip命令,免安装版本
一句话:在GitHub下载PyEMD,然后将文件夹移动到site-packages文件夹下边即可。亲测有效!找不到site-packages文件夹怎么办?移步这里(点击)遇到问题请留言评论区,将尽力回答。原创 2021-01-31 22:49:30 · 2255 阅读 · 3 评论 -
用Python在word的指定位置插入图片(使用Python-docx包)Pro版-思路
在“用Python在word的指定位置插入图片(使用Python-docx包)”这篇文章里,我们解决了指定位置插入图片的问题。但是,这个只是解决了一个小问题。更大的问题是什么呢?如果需要在图片之前插入一个表格,那么就必须相应地在程序里边修改对应的的待插入图片的表格的编号。这是上一篇文章的遗留问题。这个问题很严重吗?如果这个程序是一个人维护,那么这个问题就不是很突出。因为自己懂怎么改代码,这很好办。但是,如果是一堆人维护这个程序的话,交流的成本就会高昂起来。我们知道有些程序员不喜欢看别人的代码,这时候写代原创 2021-01-31 22:27:27 · 5080 阅读 · 5 评论