当前位置: 首页>>代码示例>>Python>>正文


Python DataMatrix.getRow方法代码示例

本文整理汇总了Python中pysgpp.DataMatrix.getRow方法的典型用法代码示例。如果您正苦于以下问题:Python DataMatrix.getRow方法的具体用法?Python DataMatrix.getRow怎么用?Python DataMatrix.getRow使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pysgpp.DataMatrix的用法示例。


在下文中一共展示了DataMatrix.getRow方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: checkPositivity

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
def checkPositivity(grid, alpha):
    # define a full grid of maxlevel of the grid
    gs = grid.getStorage()
    fullGrid = Grid.createLinearGrid(gs.dim())
    fullGrid.createGridGenerator().full(gs.getMaxLevel())
    fullHashGridStorage = fullGrid.getStorage()
    A = DataMatrix(fullHashGridStorage.size(), fullHashGridStorage.dim())
    p = DataVector(gs.dim())
    for i in xrange(fullHashGridStorage.size()):
        fullHashGridStorage.get(i).getCoords(p)
        A.setRow(i, p)

    res = evalSGFunctionMulti(grid, alpha, A)
    ymin, ymax, cnt = 0, -1e10, 0
    for i, yi in enumerate(res.array()):
        if yi < 0. and abs(yi) > 1e-13:
            cnt += 1
            ymin = min(ymin, yi)
            ymax = max(ymax, yi)
            A.getRow(i, p)
            print "  %s = %g" % (p, yi)
    if cnt > 0:
        print "warning: function is not positive"
        print "%i/%i: [%g, %g]" % (cnt, fullHashGridStorage.size(), ymin, ymax)
    return cnt == 0
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:27,代码来源:sparse_grid.py

示例2: testSave

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
    def testSave(self):
        filename = pathlocal + '/datasets/saving.arff.gz'
        testPoints = [[0.307143,0.130137,0.050000],
                      [0.365584,0.105479,0.050000],
                      [0.178571,0.201027,0.050000],
                      [0.272078,0.145548,0.050000],
                      [0.318831,0.065411,0.050000],
                      [0.190260,0.086986,0.050000],
                      [0.190260,0.062329,0.072500],
                      [0.120130,0.068493,0.072500],
                      [0.225325,0.056164,0.072500],
                      [0.213636,0.050000,0.072500]
                     ]
        testValues = [-1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, -1.000000, -1.000000, -1.000000, -1.000000]
        attributes = {
                      "x0":"NUMERIC",
                      "x1":"NUMERIC",
                      "x2":"NUMERIC",
                      "class":"NUMERIC",
                      }
        size = len(testPoints)
        dim = len(testPoints[0])
        point = DataVector(dim)
        points = DataMatrix(size, dim)

        for row in xrange(size):
            for col in xrange(dim):
                point[col] = testPoints[row][col]
            points.setRow(row, point)

        adapter = ARFFAdapter(filename)
        adapter.save(points, testValues, attributes)

        (points, values) = adapter.loadData().getPointsValues()
        size = len(testPoints)
        dim = len(testPoints[0])
        testVector = DataVector(dim)
        for rowIdx in xrange(size):
            points.getRow(rowIdx, testVector)
            for colIdx in xrange(dim):
                if cvar.USING_DOUBLE_PRECISION:
                    self.assertEqual(testVector[colIdx], testPoints[rowIdx][colIdx])
                else:
                    self.assertAlmostEqual(testVector[colIdx], testPoints[rowIdx][colIdx])
            self.assertEqual(values[rowIdx], testValues[rowIdx])

        os.remove(filename)
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:49,代码来源:test_ARFFAdapter.py

示例3: computeErrors

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
def computeErrors(jgrid, jalpha,
                  grid1, alpha1,
                  grid2, alpha2,
                  n=200):
    """
    Compute some errors to estimate the quality of the
    interpolation.
    @param jgrid: Grid, new discretization
    @param jalpha: DataVector, new surpluses
    @param grid1: Grid, old discretization
    @param alpha1: DataVector, old surpluses
    @param grid2: Grid, old discretization
    @param alpha2: DataVector, old surpluses
    @return: tuple(<float>, <float>), maxdrift, l2norm
    """
    jgs = jgrid.getStorage()

    # create control samples
    samples = DataMatrix(np.random.rand(n, jgs.dim()))

    # evaluate the sparse grid functions
    jnodalValues = evalSGFunctionMulti(jgrid, jalpha, samples)

    # eval grids
    nodalValues1 = evalSGFunctionMulti(grid1, alpha1, samples)
    nodalValues2 = evalSGFunctionMulti(grid2, alpha2, samples)

    # compute errors
    p = DataVector(jgs.dim())
    err = DataVector(n)
    for i in xrange(n):
        samples.getRow(i, p)
        y = nodalValues1[i] * nodalValues2[i]
        if abs(jnodalValues[i]) > 1e100:
            err[i] = 0.0
        else:
            err[i] = abs(y - jnodalValues[i])

    # get error statistics
    # l2
    l2norm = err.l2Norm()
    # maxdrift
    err.abs()
    maxdrift = err.max()

    return maxdrift, l2norm
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:48,代码来源:discretizeProduct.py

示例4: computeErrors

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
def computeErrors(jgrid, jalpha, grid, alpha, f, n=200):
    """
    Compute some errors to estimate the quality of the
    interpolation.
    @param jgrid: Grid, new discretization
    @param jalpha: DataVector, new surpluses
    @param grid: Grid, old discretization
    @param alpha: DataVector, old surpluses
    @param f: function, to be interpolated
    @param n: int, number of Monte Carlo estimates for error estimation
    @return: tuple(<float>, <float>), maxdrift, l2norm
    """
    jgs = jgrid.getStorage()

    # create control samples
    samples = DataMatrix(np.random.rand(n, jgs.dim()))

    # evaluate the sparse grid functions
    jnodalValues = evalSGFunctionMulti(jgrid, jalpha, samples)
    nodalValues = evalSGFunctionMulti(grid, alpha, samples)

    # compute errors
    p = DataVector(jgs.dim())
    err = DataVector(n)
    for i in xrange(n):
        samples.getRow(i, p)
        y = f(p.array(), nodalValues[i])
        err[i] = abs(y - jnodalValues[i])

    # get error statistics
    # l2
    l2norm = err.l2Norm()
    # maxdrift
    err.abs()
    maxdrift = err.max()

    return maxdrift, l2norm
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:39,代码来源:discretization.py

示例5: computeCoefficients

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
def computeCoefficients(jgrid, grid, alpha, f):
    """
    Interpolate function f, which depends on some sparse grid function
    (grid, alpha) on jgrid
    @param jgrid: Grid, new discretization
    @param grid: Grid, old discretization
    @param alpha: DataVector, surpluses for grid
    @param f: function, to be interpolated
    @return: DataVector, surpluses for jgrid
    """
    jgs = jgrid.getStorage()

    # dehierarchization
    p = DataVector(jgs.dim())
    A = DataMatrix(jgs.size(), jgs.dim())
    for i in xrange(jgs.size()):
        jgs.get(i).getCoords(p)
        A.setRow(i, p)

    nodalValues = evalSGFunctionMulti(grid, alpha, A)

    # apply f to all grid points
    jnodalValues = DataVector(jgs.size())
    for i in xrange(len(nodalValues)):
        A.getRow(i, p)
#         print i, p.array(), nodalValues[i], alpha.min(), alpha.max()
#         if nodalValues[i] < -1e20 or nodalValues[i] > 1e20:
#             from pysgpp.extensions.datadriven.uq.operations import evalSGFunction, evalSGFunctionMultiVectorized
#             print alpha.min(), alpha.max()
#             print evalSGFunction(grid, alpha, p)
#             print evalSGFunctionMulti(grid, alpha, DataMatrix([p.array()]))
#             print evalSGFunctionMultiVectorized(grid, alpha, DataMatrix([p.array()]))
#             import ipdb; ipdb.set_trace()
        jnodalValues[i] = f(p.array(), nodalValues[i])

    jalpha = hierarchize(jgrid, jnodalValues)
    return jalpha
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:39,代码来源:discretization.py

示例6: TestWeightedRefinementOperator

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
class TestWeightedRefinementOperator(unittest.TestCase):


    def setUp(self):

        #
        # Grid
        #

        DIM = 2
        LEVEL = 2

        self.grid = Grid.createLinearGrid(DIM)
        self.grid_gen = self.grid.createGridGenerator()
        self.grid_gen.regular(LEVEL)

        #
        # trainData, classes, errors
        #

        xs = []
        DELTA = 0.05
        DELTA_RECI = int(1/DELTA)

        for i in xrange(DELTA_RECI):
            for j in xrange(DELTA_RECI):
                xs.append([DELTA*i, DELTA*j])

        random.seed(1208813)
        ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)]

        # print xs
        # print ys

        self.trainData = DataMatrix(xs)
        self.classes = DataVector(ys)
        self.alpha = DataVector([3, 6, 7, 9, -1])

        self.errors = DataVector(DELTA_RECI**2)
        coord = DataVector(DIM)

        for i in xrange(self.trainData.getNrows()):
            self.trainData.getRow(i, coord)
            self.errors.__setitem__ (i, self.classes[i] - self.grid.eval(self.alpha, coord))

        #print "Errors:"
        #print self.errors

        #
        # Functor
        #

        self.functor = WeightedErrorRefinementFunctor(self.alpha, self.grid)
        self.functor.setTrainDataset(self.trainData)
        self.functor.setClasses(self.classes)
        self.functor.setErrors(self.errors)

    def test_1(self):
        storage = self.grid.getStorage()
        coord = DataVector(storage.dim())
        num_coeff = self.alpha.__len__()

        values = [self.functor.__call__(storage,i) for i in xrange(storage.size())]
        expect = []
        for i in xrange(num_coeff):
            # print i
            val = 0
            single = DataVector(num_coeff)
            single.__setitem__(i, self.alpha.__getitem__(i))
            for j in xrange(self.trainData.getNrows()):
                self.trainData.getRow(j, coord)
                val += abs( self.grid.eval(single, coord) * (self.errors.__getitem__(j)**2) )
            expect.append(val)

        # print values
        # print expect

        # print [ values[i]/expect[i] for i in xrange(values.__len__())]
        
        self.assertEqual(values, expect)
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:82,代码来源:test_WeightedRefinementOperator.py

示例7: TestOnlinePredictiveRefinementDimension

# 需要导入模块: from pysgpp import DataMatrix [as 别名]
# 或者: from pysgpp.DataMatrix import getRow [as 别名]
class TestOnlinePredictiveRefinementDimension(unittest.TestCase):

    def setUp(self):

        #
        # Grid
        #

        DIM = 2
        LEVEL = 2

        self.grid = Grid.createLinearGrid(DIM)
        self.grid_gen = self.grid.createGridGenerator()
        self.grid_gen.regular(LEVEL)


        #
        # trainData, classes, errors
        #

        xs = []
        DELTA = 0.05
        DELTA_RECI = int(1/DELTA)

        for i in xrange(DELTA_RECI):
            for j in xrange(DELTA_RECI):
                xs.append([DELTA*i, DELTA*j])

        random.seed(1208813)
        ys = [ random.randint(-10, 10) for i in xrange(DELTA_RECI**2)]

        self.trainData = DataMatrix(xs)
        self.classes = DataVector(ys)
        self.alpha = DataVector([3, 6, 7, 9, -1])
        self.multEval = createOperationMultipleEval(self.grid, self.trainData)

        self.errors = DataVector(DELTA_RECI**2)
        coord = DataVector(DIM)

        for i in xrange(self.trainData.getNrows()):
            self.trainData.getRow(i, coord)
            self.errors.__setitem__ (i, abs(self.classes[i] - self.grid.eval(self.alpha, coord)))

        #
        # OnlinePredictiveRefinementDimension
        #

        hash_refinement = HashRefinement();
        self.strategy = OnlinePredictiveRefinementDimension(hash_refinement)
        self.strategy.setTrainDataset(self.trainData)
        self.strategy.setClasses(self.classes)
        self.strategy.setErrors(self.errors)

    def test_1(self):

        storage = self.grid.getStorage()
        gridSize = self.grid.getSize()
        numDim = storage.dim()

        print "######"
        print "Expected result:"
        print "######"

        expected = {}

        for j in xrange(gridSize):

            HashGridIndex = storage.get(j)
            HashGridIndex.setLeaf(False)
                
            print "Point: ", j, " (", HashGridIndex.toString(), ")"

            for d in xrange(numDim):

                #
                # Get left and right child
                #

                leftChild = HashGridIndex(HashGridIndex)
                rightChild = HashGridIndex(HashGridIndex)

                storage.left_child(leftChild, d)
                storage.right_child(rightChild, d)

                #
                # Check if point is refinable
                #

                if storage.has_key(leftChild) or storage.has_key(rightChild):
                    continue

                #
                # Insert children temporarily
                #

                storage.insert(leftChild) 
                storage.insert(rightChild) 

                val1 = self.calc_indicator_value(leftChild)
                val2 = self.calc_indicator_value(rightChild)
#.........这里部分代码省略.........
开发者ID:ABAtanasov,项目名称:Sparse-Grids,代码行数:103,代码来源:test_OnlinePredictiveRefinementDimension.py


注:本文中的pysgpp.DataMatrix.getRow方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。