numpy
numpy官方文档解析
千行百行
这个作者很懒,什么都没留下…
展开
-
TypeError: ‘<=‘ not supported between instances of ‘list‘ and ‘int‘
debug原创 2022-04-17 22:06:31 · 8318 阅读 · 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 · 12125 阅读 · 1 评论 -
如何统计二维或者多维空间/二维数组中重复元素的数量/计数?numpy一行代码就行了
统计重复元素的数量,不仅仅适用于一维,而且适用于二维或者多维。原创 2022-01-05 21:15:47 · 3765 阅读 · 0 评论 -
使用numpy生成服从多元正态分布(multivariate normal distribution)的样本
文章目录代码结果图示see also代码import numpy as npmean = [0, 0]sigam1 = 1sigam2 = 1rho = 0 # 相关系数cov12 = rho*sigam1*sigam2 # 协方差cov = [ [sigam1**2, cov12], [cov12, sigam2**2]] # 协方差矩阵x1, y1 = np.random.multivariate_normal( mean, cov,原创 2021-12-23 23:04:13 · 2446 阅读 · 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 · 765 阅读 · 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 · 654 阅读 · 2 评论 -
python按照 某一列/指定列 进行筛选
import numpy as npa_ndarray = np.array( [ [1, 3], [3, 4], [3, 4444], [1, 6], [8, 6], [10, 6] ])# 按照第一列进行筛选,选中等于1的行a_1 = a_ndarray[np.where(a_ndarray[:, 0] == 1)]a_2 = np.compress(a_ndarray[:, 0原创 2021-01-01 21:27:59 · 7636 阅读 · 0 评论 -
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 · 787 阅读 · 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 · 1163 阅读 · 0 评论 -
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 · 20129 阅读 · 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 · 1459 阅读 · 0 评论 -
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 · 10424 阅读 · 0 评论 -
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 · 4164 阅读 · 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 · 4172 阅读 · 1 评论 -
使用python一步完成相关系数计算(correlation coefficient)
代码如下:import numpy as np# x是一个行向量x = np.array([1, 3, 564, 675, 6567]).reshape(1, -1)# y1、y2都是行向量y1 = np.array([1, 3, 555555, 675, 6567]).reshape(1, -1)y2 = 2*x+4# 两个行向量拼接到一起,形成一个两行的矩阵x_y1 = np.r_[x, y1]x_y2 = np.r_[x, y2]# 注意使用np.corrcoef求相关系数原创 2021-01-01 20:51:12 · 13376 阅读 · 0 评论 -
python一行代码实现 判断两个矩阵/数组是否近似相等或相等
numpy.allclose可以用来判断两个矩阵是否近似地相等(约等),例子如下:>>> np.allclose([1e10,1e-7], [1.00001e10,1e-8])False>>> np.allclose([1e10,1e-8], [1.00001e10,1e-9])True>>> np.allclose([1e10,1e-8], [1.0001e10,1e-9])False>>> np.allclose([1原创 2021-01-03 11:35:05 · 5620 阅读 · 0 评论 -
python之numpy之伪逆numpy.linalg.pinv
解读一手官方文档。最新鲜的知识,只需要最简单的了解方式!原创 2020-04-16 15:11:29 · 32161 阅读 · 1 评论 -
python之numpy之方差numpy.var
主要讲述了如何使用numpy计算方差和样本方差文章目录方差样本方差方差方差是与平均值的平方偏差的平均值,即 var = mean(abs(x - x.mean())**2)。例子:1>>> a = np.array([[1, 2], [3, 4]])>>> np.var(a)1.25>>> np.var(a, axis=0) #...原创 2020-04-16 13:57:26 · 20433 阅读 · 0 评论 -
python之numpy之乘法:numpy.dot、numpy.multiply、*
numpy.dot隶属于Linear algebra (numpy.linalg),即numpy.dot是线性代数运算。1例子:>>> a = [[1, 0]] # 1×2维>>> b = [[4], [2]] # 2×1维>>> np.dot(a, b) # 1×1维array([[4]])numpy.multiply隶属...原创 2020-04-15 14:55:19 · 472 阅读 · 0 评论 -
python之numpy之power
搬运自SciPy官方:numpy.power:统一进行幂运算>>> x1 = range(6)>>> x1[0, 1, 2, 3, 4, 5]>>> np.power(x1, 3)array([ 0, 1, 8, 27, 64, 125])分别对应不同的指数>>> x2 = [1.0, 2.0,...原创 2020-04-12 22:46:13 · 840 阅读 · 0 评论 -
python之numpy之reduce
add.reduce()相当于sum()。¶举几个例子:1>>> import numpy as np>>> np.multiply.reduce([2,3,5])30>>> import numpy as np>>> np.logical_and.reduce([1,0,1])FalseSciPy...原创 2020-04-11 22:03:27 · 4801 阅读 · 0 评论 -
python之numpy之axis=1和axis=0
对二维数组而言,一般来说axis=1表示对行进行操作,axis=0表示对列进行操作。以sort1、mean2为例进行描述sort>>> import numpy as np>>> a = np.array([[1,4],[3,1]])>>> np.sort(a) # sort along the last...原创 2020-04-10 10:45:07 · 1644 阅读 · 0 评论 -
numpy官方文档整理—线性代数(Linear algebra (numpy.linalg))
文章目录Matrix and vector productsDecompositionsMatrix eigenvaluesNorms and other numbersSolving equations and inverting matricesExceptionsThe NumPy linear algebra functions rely on BLAS and LAPACK to pr...原创 2019-12-13 10:46:51 · 1591 阅读 · 0 评论 -
numpy官方文档整理—数组操作例程(Array manipulation routines)
官方文档是第一手资料,拒绝二道贩子原创 2019-12-09 10:46:02 · 253 阅读 · 0 评论