
numpy
NockinOnHeavensDoor
blog已不用~谢谢关注~
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
numpy添加维度:newaxis
关于newaxis的例子: http://blog.youkuaiyun.com/xtingjie/article/details/72510834今天用sklearn里的线性模型fit的时候报错:ValueError: Expected 2D array, got 1D array instead: array=[2 3 4 5]. Reshape your data either using arra原创 2017-12-09 20:57:49 · 2019 阅读 · 0 评论 -
numpy API:np.set_printoptions
作用:确定浮点数字、数组、和numpy对象的显示形式。 例子:#精度为小数点后4位np.set_printoptions(precision=4)print(np.array([1.123456789]))[1.1235]#超过阈值就缩写np.set_printoptions(threshold=4)print(np.arange(10))[0 1 2 … 7 ...原创 2018-05-15 19:33:35 · 4141 阅读 · 0 评论 -
numpy API: np.random.seed() 与 np.random.RandomState()
np.random.seed()设置种子之后,用np.random.RandomState()记录种子,这样后续生成的随机数都是可以重用的。例子:np.random.seed(1234)np.random.uniform(0, 10, 5)array([1.9151945 , 6.22108771, 4.37727739, 7.85358584, 7.79975808])np...原创 2018-06-03 11:03:20 · 899 阅读 · 0 评论 -
numpy API:numpy.random.multinomial
作用:从多样式分布采样。numpy.random.multinomial(n, pvals, size=None)参数:n : 试验次数pvals : p长度序列,表示每次的概率size : int or tuple of ints, optional例子: 1.投一个均匀骰子20次:np.random.multinomial(20, [1/6.]*6, si...原创 2018-05-23 19:45:47 · 3218 阅读 · 0 评论 -
numpy API:np.hstack(水平) 、np.vstack(垂直)
import numpy as npa = np.array(range(10)).reshape(-1, 1)b = np.array(range(10,20)).reshape(-1,1)c = np.array(range(20,30)).reshape(-1,1)#horizontal水平叠加tmp = np.hstack([a,b])np.hstack([tmp,c]...原创 2018-06-12 08:22:02 · 1032 阅读 · 0 评论 -
random.seed()设置可复用的随机结果
例子: #Simple python programme to understand random.seed() importanceimport randomrandom.seed(10)print(random.randint(1,100))#和seed 10 结果一样random.seed(10)print(random.randint(1,100))random...原创 2018-05-30 10:28:03 · 2251 阅读 · 0 评论 -
jacobian行列式以及生成相同协方差的数据和不同协方差的数据的函数
jacobian设随机变量xxx 服从正态分布N=(μ,σ2)N=(μ,σ2)N=(\mu,\sigma^2),概率密度函数为f(x)f(x)f(x),则xxx的仿射变换(affine transformation): r=ax+b(1)(1)r=ax+br = ax + b \tag{1} 随机变量rrr服从分布N(aμ+b,a2σ2)N(aμ+b,a2σ2)N(a\mu +b,a^2\...原创 2018-06-02 10:20:01 · 642 阅读 · 0 评论 -
numpy API: np.random.multivariate_normal
例子:>>> mean = (1, 2)>>> cov = [[1, 0], [0, 1]]>>> x = np.random.multivariate_normal(mean, cov, (3, 3))#这里的最后一维代表组成多维随机向量的随机变量的个数x.shape(3, 3, 2)...原创 2018-06-02 10:48:17 · 1060 阅读 · 0 评论 -
numpy API: numpy.flipud
numpy API: numpy.flipudFlip array in the up/down direction. 对数组进行上下翻转。例子:A = np.random.randn(2,3,5)# 相当于从最后一个元素开始翻转A[::-1]array([[[ 0.7264141 , -0.68764277, -2.03427692, -0.2503444 , ...原创 2018-08-24 17:41:22 · 1442 阅读 · 0 评论 -
numpy.where() 用法详解
1. np.where(condition, x, y)满足条件(condition),输出x,不满足输出y。>>> aa = np.arange(10)>>> np.where(aa,1,-1)array([-1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) # 0为False,所以第一个输出-1>>>...转载 2018-09-21 15:35:37 · 268 阅读 · 0 评论 -
np:带broadcasting的softmax
转:https://stackoverflow.com/questions/34968722/how-to-implement-the-softmax-function-in-python# my (correct) solution:def softmax(z): assert len(z.shape) == 2 s = np.max(z, axis=1) s = s...转载 2018-09-21 15:49:14 · 243 阅读 · 0 评论 -
numpy API:np.vectorize
使用长度为1的数组才行,比如:x = array([1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])y = np.int(x)TypeError: only size-1 arrays can be converted to Python scalars用np.vectorize使得函数np.int以广播的形式作用在x上:vect...原创 2018-10-21 08:41:16 · 1158 阅读 · 0 评论 -
Numpy:array、asarray、asanyarray
array和asarray的区别看asarray的定义:def asarray(a, dtype=None, order=None): return array(a, dtype, copy=False, order=order)和array相似, 除了默认设置copy=False. 而array默认设置copy=True 所以主要的不同在于array对目标做一个拷贝...原创 2018-05-08 10:43:54 · 4929 阅读 · 0 评论 -
numpy.unravel_index
numpy.unravel_index(indices, dims, order=’C’) 作用:将平面索引或平面索引数组转换为坐标数组。Parameters: indices : array_like dims : 整型元组,指定原多维数组的维度 order : {‘C’, ‘F’}, 可选,默认为C,决定索引是否看作C行主序,即坐标表示为(x, y)形式,数组是row在前,col...原创 2018-05-08 09:52:17 · 1193 阅读 · 1 评论 -
np.flatten():合并两个数组ndarray
今天遇到要把两个np.ndarray合并在一起的情况:a = np.array([1,1,2,3])b = np.array([2,3,4,5])先叠一起:c = np.vstack([a,b])print(c)array([[1, 1, 2, 3], [1, 1, 2, 3]])然后就可以用flatten()合并了:c.flatten()array([1, 1, 2, 3,原创 2017-12-09 21:41:39 · 3038 阅读 · 0 评论 -
numpy : np.percentile使用
首先明确百分位数:第p个百分位数是这样一个值,它使得至少有p%的数据项小于或等于这个值,且至少有(100-p)%的数据项大于或等于这个值。举个例子:高等院校的入学考试成绩经常以百分位数的形式报告。比如,假设某个考生在入学考试中的语文部分的原始分数为54分。相对于参加同一考试的其他学生来说,他的成绩如何并不容易知道。但是如果原始分数54分恰好对应的是第70百分位数,我们就能知道大约70%的学生的考分比原创 2017-12-10 16:37:06 · 13663 阅读 · 0 评论 -
numpy.asarray: 转输入为数组
Examples转list为array:>>> a = [1, 2]>>> np.asarray(a)array([1, 2])不复制现有数组:>>> a = np.array([1, 2])>>> np.asarray(a) is aTrue如果dtype设定,当数据类型不一致会产生复制:>>> a = np.array([1, 2], dtype=np.float32)>>> np原创 2017-12-14 14:41:16 · 1019 阅读 · 0 评论 -
numpy:np.random.randn
numpy.random.randn numpy.random.randn(d0, d1, …, dn) 从标准正太分布里返回样本官网例子:#正态分布中的随机数>>> np.random.randn()2.1923875335537315 #random#生成(2,4)的矩阵:>>> 2.5 * np.random.randn(2, 4) + 3array([[-4.49401501,原创 2017-12-22 22:03:30 · 676 阅读 · 0 评论 -
numpy: np.c_
Translates slice objects to concatenation along the second axis. 将可切片的目标沿着1轴连接起来>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]array([[1, 2, 3, 0, 0, 4, 5, 6]])Stack 1-D arrays as columns i翻译 2017-12-24 10:43:31 · 513 阅读 · 0 评论 -
numpy.meshgrid
meshgrid的目的是从x值数组和y值数组中创建一个矩形网格。xvalues = np.array([0, 1, 2, 3, 4])yvalues = np.array([0, 1, 2, 3, 4,5])xx, yy = np.meshgrid(xvalues, yvalues)plt.plot(xx, yy, marker='.', color='k', linestyle='none')原创 2018-01-02 18:12:22 · 328 阅读 · 0 评论 -
numpy:np.random.rand
从[0, 1)均匀分布生成值np.random.rand(3,2)array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909878]]) #random原创 2018-03-30 13:34:49 · 841 阅读 · 0 评论 -
numpy:排序
import numpy as np"""选择排序"""def selection_sort(x): for i in range(len(x)): swap = i + np.argmin(x[i:]) (x[i], x[swap]) = (x[swap],x[i]) return xx = np.random.randint(1...原创 2018-04-09 22:35:14 · 405 阅读 · 0 评论 -
np.random.RandomState
rng = numpy.random.RandomState(23355)arrayA = rng.uniform(0,1,(2,3))产生一个2行3列的assarray,其中的每个元素都是[0,1]区间的均匀分布的随机数 这里看以看到,有一个23355这个数字,其实,它是伪随机数产生器的种子,也就是“the starting point for a sequence of pseudor...转载 2018-04-02 20:24:21 · 650 阅读 · 0 评论 -
numpy:图像RGB用numpy降低为灰度值
下载图像:from sklearn.datasets import load_sample_imagechina = load_sample_image("china.jpg")flower = load_sample_image("flower.jpg")截图部分图像:image = china[150:220, 130:250]获取图像的高度,宽度,色道:h...原创 2018-04-11 09:55:09 · 2884 阅读 · 0 评论 -
numpy API: np.repeat
np.repeat(a, repeats, axis=None) 作用: 复制数组的元素。Parametersa : array_like Input array. repeats : int or array of ints 每个元素复制多少次. axis : 默认会先flattened输入数组,返回flat的输出数组。Returnsrepeated...原创 2018-05-09 20:25:13 · 406 阅读 · 1 评论 -
numpy.argmax
作用:返回轴上最大值的索引。例子:a = np.arange(6).reshape(2,3)aarray([[0, 1, 2], [3, 4, 5]])np.argmax(a)5np.argmax(a, axis=0)#列上最大值的索引array([1, 1, 1]) np.argmax(a, axis=1)#行上最大值的索引array([2, 2])...原创 2018-05-08 09:32:55 · 727 阅读 · 0 评论 -
numpy:interp插值
numpy.interp作用:给定1维数据点上做插入。Parameters:x : 要估计坐标点的x坐标值。xp : x坐标值,must be increasing if argument period is not specified. Otherwise, xp is internally sorted after normalizing the periodic boundari...原创 2018-10-25 20:20:13 · 11006 阅读 · 0 评论