【python/numpy随机选取训练集/测试集索引】教程文章相关的互联网学习教程文章

python数据分析numpy库学习【代码】

import numpy as np def asum(a_list,b_list,n1=2,n2=3):a = np.array(a_list)b = np.array(b_list)c = pow(a,n1) + pow(b,n2)return ca_lst = [1,2,3,4] b_lst = [2,3,4,5] print(asum(a_lst,b_lst))#np.array()生成数据对象ndarray a = np.array([[1,2,3,4],[1,2,3,4]]) print(type(a)) #<class numpy.ndarray> print(a) print(a.ndim)#轴数 print(a.shape) #(2,4)2行4列 print(a.size) #总元素数 print(a.itemsize) #元素大小 ...

python – 在numpy数组中查找最接近的值【代码】

有一种numpy-thonic方式,例如函数,找到数组中最近的值? 例:np.find_nearest( array, value )解决方法: import numpy as np def find_nearest(array, value):array = np.asarray(array)idx = (np.abs(array - value)).argmin()return array[idx]array = np.random.random(10) print(array) # [ 0.21069679 0.61290182 0.63425412 0.84635244 0.91599191 0.00213826 # 0.17104965 0.56874386 0.57319379 0.28719469]valu...

连接boost :: python :: numpy时出现问题【代码】

我写了一个小例子来说明发生了什么. my_test.cpp#include <iostream> #include <boost/python/numpy.hpp> namespace np = boost::python::numpy; int my_Foo() {Py_Initialize();np::initialize();std::cout << "hello\n";return 0; } BOOST_PYTHON_MODULE(my_test) {using namespace boost::python;def("my_Foo", my_Foo); }py_test.pyimport my_test as t t.my_Foo();我用命令编译所有:g++ -shared -fPIC -o my_test.so my_tes...

python – 从np.empty初始化numpy数组【代码】

从空内存初始化ndarray时,符号位是如何确定的?>>> np.random.randn(3,3) array([[-0.35557367, -0.0561576 , -1.84722985],[ 0.89342124, -0.50871646, 1.31368413],[ 0.0062188 , 1.62968789, 0.72367089]]) >>> np.empty((3,3)) array([[0.35557367, 0.0561576 , 1.84722985],[0.89342124, 0.50871646, 1.31368413],[0.0062188 , 1.62968789, 0.72367089]])从空记忆中初始化的这些浮点值已经失去了它们的标志?.这是为什么? ...

python – 无法卸载’numpy’

我一直在尝试安装astropy,在安装结束时我收到了这条消息:Cannot uninstall ‘numpy’. It is a distutils installed project and thuswe cannot accurately determine which files belong to it which wouldlead to only a partial uninstall.我试过:pip uninstall numpy然后我收到相同的消息.我在macOS High Sierra版本13.10.5中使用Python 2.7.10 (我是Python的新手,我在询问前寻找答案,但没有任何效果).解决方法:这并没有直接...

python学习笔记35:numpy_learn【代码】

numpy_learn 重要特点:N维数组对象,即ndarray对象 ndarray对象中存储的数据类型要是一致的 import numpy as np data = np.arange(12).reshape(3,4) data array([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]]) # 维度个数 data.ndim 2 # 数组维度 data.shape (3, 4) # 数组元素总个数 data.size 12 # 数组元素类型 data.dtype dtype('int64') # 数组元素字节大小 data.itemsize 8 创建Numpy对象 array(),括号里面传入pyt...

python – 将vtkPoints转换为numpy数组?【代码】

我在Python脚本中使用Mayavi2来计算3d等值面.结果我得到了一个vtkPoints对象.现在我想将这个vtkPoints对象(下面的代码示例中的’vtkout’)转换为一个简单的numpy数组,其中3行包含所有x,y和z值.我使用这样的代码得到vtkout:import numpy from enthought.mayavi import mlab import arrayrandVol = numpy.random.rand(50,50,50) # fill volume with some random potential X, Y, Z = numpy.mgrid[0:50, 0:50, 0:50] # g...

Boost.python重载了numpy数组和python列表的构造函数【代码】

给定使用Boost.Python公开的C类,如何公开两个构造函数: >一个采用numpy数组,和>另一个需要python列表?解决方法:我不是100%的意思,但我假设你想让一个构造函数采用Python列表而另一个采用numpy数组.有几种方法可以解决这个问题.最简单的方法是使用make_constructor函数并重载它:using boost; using boost::python;shared_ptr<MyClass> CreateWithList(list lst) {// construct with a list here }shared_ptr<MyClass> CreateWit...

计算 – numpy python bug【代码】

我正在使用NumPy通过一个大盒子和一个小盒子之间的Aperture来寻找Y截距.我在大盒子里有超过100,000颗颗粒,在小盒子里有大约1000颗颗粒.这需要花费大量时间.所有self.YD,self.XD都是非常大的数组,我正在成倍增加. PS:ind是需要乘以的值的索引.我的代码中该行之前有一个非零条件. 有什么想法我会以更简单的方式进行这种计算吗?YD_zero = self.oldYD[ind] - ((self.oldYD[ind]-self.YD[ind]) * self.oldXD[ind])/(self.oldXD[ind]-s...

python – 在NumPy中复制Matlab的ISMEMBER函数的索引结果?【代码】

我一直在绞尽脑汁寻找与older question一致的解决方案.我一直试图找到一个复制索引结果的Python代码模式.例如:A = [3;4;4;3;6] B = [2;5;2;6;3;2;2;5] [tf ix] = ismember(A,B) >> A(tf)ans =336 >> B(ix(tf))ans =336这让我能做的是,如果有一个数组C以与B相同的方式排序,我现在可以适当地将C的值插入到一个新的数组D中,该数组D的排序方式与A相同.我做了很多数据映射!我希望这能用于各种数据类型,特别是字符串和日期时间.看起来n...

python – Numpy:使用numpy.ndarray.view的永久性更改?【代码】

如果我想永久更改numpy数组的数据类型,重新分配是最好的方法吗? 这是一个说明语法的示例:import numpy as np x = np.array([1],dtype='float') x = x.view(dtype=('x','float"))但是,我更愿意更改“就地”更改数据类型. 有没有办法在适当的位置更改数组的dtype?或者这样的东西更好吗?:x = x.view(dtype=('x',"float")).copy()解决方法:真的,没有“永久”的dtype. Numpy数组基本上只是一种查看内存缓冲区的方法. 在numpy意义上...

python – 如何将常规numpy数组转换为记录数组?【代码】

我读了一系列数字np.array(f.read().split(),dtype=np.float64)然后我使用np.reshape()将其转换为二维数组. 在此之后,如何将arr转换为记录数组?我尝试了(类似)以下内容:filename = 'unstructured-file.txt' nfields = 3 names = ('r','g','b') with open(filename,'r') as f:arr = np.array(f.read().split(),dtype=np.float64)arr = arr.reshape(-1,nfields)out = np.array(arr,dtype=zip(names,['float64']*length(names))但是...

python – 使用numpy.where()返回完整数组的索引,其中测试条件位于切片上【代码】

我有以下3 x 3 x 3 numpy数组称为a(在阅读完其余问题后,注释将有意义):array([[[8, 1, 0], # irrelevant 1 (is at position 1 rather than 0)[1, 7, 5], # the 1 on this line is what I am after![1, 4, 9]], # irrelevant 1 (out of the "cross")[[4, 0, 1], # irrelevant 1 (is at position 2 rather than 0)[1, 0, 1], # I'm only after the first 1 on this line![6, 2, 1]], # irrelevant 1 (is at p...

Numpy的Python列表理解【代码】

我正在寻找Numpy中的列表理解方法或类似方法来消除for循环的使用,例如. index_values是列表的Python字典列表(每个列表包含不同数量的索引值),s是一个numpy向量:for i in range(33):s[index_values[i]] += 4.1有没有可用的方法可以消除for循环?解决方法:我不完全了解index_values是什么类型的对象.但如果它是一个ndarray,或者可以转换为ndarray,你可以这样做:>>> s = numpy.arange(20) >>> index_values = (numpy.random.random(...

Python尝试乘以适当维度的两个numpy矩阵时返回错误【代码】

我的代码非常简单,但是当我尝试乘以32和21矩阵时,我得到以下错误(对我来说,没有意义):ValueError: operands could not be broadcast together with shapes (3,2) (2,1) 在这个程序中,我做的第一件事是在域[-1,1] x [-1,1]中随机生成两个点,并使用变量slope和y_int通过这些点定义一条线.然后,我创建形式为{x_0,x_1,x_2}的N个随机x值,其中x_0始终为1,x_1,x_2是[-1,1]范围内随机生成的数字.这些N值包含代码中的x_matrix. y_matrix是值...