def main(): filename = '../../../dataset/tj_stream/tj_stream.csv' data = np.recfromcsv(filename) data_tuplelist = data.tolist() data_list = [list(i) for i in data_tuplelist] nop = 200 nod = shape(data_list)[1] print nod sigmai = [0.1] * nod chunk_size = 100 old_index = np.random.normal(loc=0, scale=math.pow(sigmai[1], 1), size=(nop, nod)) old_param = np.random.normal(loc=0, scale=sigmai[1], size=(1, nod)) # print old_param # print old_index chunk_accuracy_list = [] for i in range(0, len(data_list), chunk_size): print i chunk_data = data_list[i:i + chunk_size] chunk_data = [[1] + x for x in chunk_data] [chunk_params, current_parameters] = compute_chunk_n(chunk_data, nop, sigmai, old_param, old_index) # print chunk_params # print 'gg' #print current_parameters old_param = [chunk_params] old_index = current_parameters #print old_param #print current_parameters #print chunk_params #print chunk_params chunk_accuracy_list.append(compute_accuracy(chunk_data, chunk_params)) plot_accuracy(chunk_accuracy_list)
def __new__(class_object, data): print(ma.shape(data)) if data.shape[2] != 2: raise NumpyShapeError('(parray, point, x/y)', data.shape) obj = super(PointArray2D, class_object) \ .__new__(class_object, data, dtype='uint8') return obj
def plotBestFit(weights): import matplotlib.pyplot as plt dataMat, labelMat = loadDataSet() dataArr = array(dataMat) n = shape(dataArr)[0] xcord1 = [] ycord1 = [] xcord2 = [] ycord2 = [] for i in range(n): if int(labelMat[i]) == 1: xcord1.append(dataArr[i, 1]) ycord1.append(dataArr[i, 2]) else: xcord2.append(dataArr[i, 1]) ycord2.append(dataArr[i, 2]) fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(xcord1, ycord1, s=30, c='red', marker='s') ax.scatter(xcord2, ycord2, s=30, c='green') x = arange(-3.0, 3.0, 0.1) y = (-weights[0] - weights[1] * x) / weights[2] ax.plot(x, y) plt.xlabel('X') plt.ylabel('Y') plt.show()
def autoNorm(dataSet): """ 归一化特征值,消除属性之间量级不同导致的影响 :param dataSet: 数据集 :return: 归一化后的数据集normDataSet,ranges和minVals即最小值与范围,并没有用到 归一化公式: Y = (X-Xmin)/(Xmax-Xmin) 其中的 min 和 max 分别是数据集中的最小特征值和最大特征值。该函数可以自动将数字特征值转化为0到1的区间。 """ # 计算每种属性的最大值、最小值、范围 minVals = dataSet.min(0) maxVals = dataSet.max(0) # 极差 ranges = maxVals - minVals # -------第一种实现方式---start------------------------- normDataSet = zeros(shape(dataSet)) m = dataSet.shape[0] # 生成与最小值之差组成的矩阵 normDataSet = dataSet - tile(minVals, (m, 1)) # 将最小值之差除以范围组成矩阵 normDataSet = normDataSet / tile(ranges, (m, 1)) # element wise divide # -------第一种实现方式---end--------------------------------------------- # # -------第二种实现方式---start--------------------------------------- # norm_dataset = (dataset - minvalue) / ranges # # -------第二种实现方式---end--------------------------------------------- return normDataSet, ranges, minVals
def test_testAverage2(self): # More tests of average. w1 = [0, 1, 1, 1, 1, 0] w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]] x = arange(6) assert_(allclose(average(x, axis=0), 2.5)) assert_(allclose(average(x, axis=0, weights=w1), 2.5)) y = array([arange(6), 2.0 * arange(6)]) assert_(allclose(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)) assert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.)) assert_(allclose(average(y, axis=1), [average(x, axis=0), average(x, axis=0)*2.0])) assert_(allclose(average(y, None, weights=w2), 20. / 6.)) assert_(allclose(average(y, axis=0, weights=w2), [0., 1., 2., 3., 4., 10.])) assert_(allclose(average(y, axis=1), [average(x, axis=0), average(x, axis=0)*2.0])) m1 = zeros(6) m2 = [0, 0, 1, 1, 0, 0] m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]] m4 = ones(6) m5 = [0, 1, 1, 1, 1, 1] assert_(allclose(average(masked_array(x, m1), axis=0), 2.5)) assert_(allclose(average(masked_array(x, m2), axis=0), 2.5)) assert_(average(masked_array(x, m4), axis=0) is masked) assert_equal(average(masked_array(x, m5), axis=0), 0.0) assert_equal(count(average(masked_array(x, m4), axis=0)), 0) z = masked_array(y, m3) assert_(allclose(average(z, None), 20. / 6.)) assert_(allclose(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])) assert_(allclose(average(z, axis=1), [2.5, 5.0])) assert_(allclose(average(z, axis=0, weights=w2), [0., 1., 99., 99., 4.0, 10.0])) a = arange(6) b = arange(6) * 3 r1, w1 = average([[a, b], [b, a]], axis=1, returned=1) assert_equal(shape(r1), shape(w1)) assert_equal(r1.shape, w1.shape) r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1) assert_equal(shape(w2), shape(r2)) r2, w2 = average(ones((2, 2, 3)), returned=1) assert_equal(shape(w2), shape(r2)) r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1) assert_(shape(w2) == shape(r2)) a2d = array([[1, 2], [0, 4]], float) a2dm = masked_array(a2d, [[0, 0], [1, 0]]) a2da = average(a2d, axis=0) assert_(eq(a2da, [0.5, 3.0])) a2dma = average(a2dm, axis=0) assert_(eq(a2dma, [1.0, 3.0])) a2dma = average(a2dm, axis=None) assert_(eq(a2dma, 7. / 3.)) a2dma = average(a2dm, axis=1) assert_(eq(a2dma, [1.5, 4.0]))
def test_testAverage2(self): # More tests of average. w1 = [0, 1, 1, 1, 1, 0] w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]] x = arange(6) assert_(allclose(average(x, axis=0), 2.5)) assert_(allclose(average(x, axis=0, weights=w1), 2.5)) y = array([arange(6), 2.0 * arange(6)]) assert_(allclose(average(y, None), np.add.reduce(np.arange(6)) * 3. / 12.)) assert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.)) assert_(allclose(average(y, axis=1), [average(x, axis=0), average(x, axis=0)*2.0])) assert_(allclose(average(y, None, weights=w2), 20. / 6.)) assert_(allclose(average(y, axis=0, weights=w2), [0., 1., 2., 3., 4., 10.])) assert_(allclose(average(y, axis=1), [average(x, axis=0), average(x, axis=0)*2.0])) m1 = zeros(6) m2 = [0, 0, 1, 1, 0, 0] m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]] m4 = ones(6) m5 = [0, 1, 1, 1, 1, 1] assert_(allclose(average(masked_array(x, m1), axis=0), 2.5)) assert_(allclose(average(masked_array(x, m2), axis=0), 2.5)) assert_(average(masked_array(x, m4), axis=0) is masked) assert_equal(average(masked_array(x, m5), axis=0), 0.0) assert_equal(count(average(masked_array(x, m4), axis=0)), 0) z = masked_array(y, m3) assert_(allclose(average(z, None), 20. / 6.)) assert_(allclose(average(z, axis=0), [0., 1., 99., 99., 4.0, 7.5])) assert_(allclose(average(z, axis=1), [2.5, 5.0])) assert_(allclose(average(z, axis=0, weights=w2), [0., 1., 99., 99., 4.0, 10.0])) a = arange(6) b = arange(6) * 3 r1, w1 = average([[a, b], [b, a]], axis=1, returned=True) assert_equal(shape(r1), shape(w1)) assert_equal(r1.shape, w1.shape) r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=True) assert_equal(shape(w2), shape(r2)) r2, w2 = average(ones((2, 2, 3)), returned=True) assert_equal(shape(w2), shape(r2)) r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=True) assert_(shape(w2) == shape(r2)) a2d = array([[1, 2], [0, 4]], float) a2dm = masked_array(a2d, [[0, 0], [1, 0]]) a2da = average(a2d, axis=0) assert_(eq(a2da, [0.5, 3.0])) a2dma = average(a2dm, axis=0) assert_(eq(a2dma, [1.0, 3.0])) a2dma = average(a2dm, axis=None) assert_(eq(a2dma, 7. / 3.)) a2dma = average(a2dm, axis=1) assert_(eq(a2dma, [1.5, 4.0]))
def autoNorm(dataSet): minVals = dataSet.min(0) maxVals = dataSet.max(0) ranges = maxVals - minVals normDataSet = zeros(shape(dataSet)) m = dataSet.shape[0] normDataSet = dataSet - tile(minVals, (m, 1)) normDataSet = normDataSet / tile(ranges, (m, 1)) return normDataSet, ranges, minVals
def stocGradAscent0(dataMatrix, classLabels): m, n = shape(dataMatrix) alpha = 0.01 weights = ones(n) for i in range(m): h = sigmoid(sum(dataMatrix[i] * weights)) error = classLabels[i] - h weights = weights + alpha * error * dataMatrix[i] return weights
def gradAscent(dataMatIn, classLabels): dataMatrix = np.mat(dataMatIn) labelMat = np.mat(classLabels).transpose() m, n = shape(dataMatrix) alpha = 0.001 maxCycles = 500 weights = np.ones((n, 1)) for k in range(maxCycles): h = sigmoid(dataMatrix * weights) error = (labelMat - h) weights = weights + alpha * dataMatrix.transpose() * error return weights
def stocGradAscent1(dataMatrix, classLabels, numIter=150): m, n = shape(dataMatrix) weights = ones(n) for j in range(numIter): dataIndex = range(m) for i in range(m): alpha = 0.01 + 4 / (1.0 + j + i) randIndex = int(random.uniform(0, len(dataIndex))) h = sigmoid(sum(dataMatrix[randIndex] * weights)) error = classLabels[randIndex] - h weights = weights + alpha * error * dataMatrix[randIndex] return weights
def mask(x, xm, x0, x1): if numpy.ndim(xm) != 1: print "utility.mask(): array to used to derive mask must be 1D" return (numpy.array([])) xmask = ma.masked_outside(xm, x0, x1) tmask = ma.getmask(xmask) if numpy.ndim(x) == 1: xnew = ma.array(x, mask=tmask) return (xnew.compressed()) if numpy.ndim(x) == 2: for i in range(0, numpy.shape(x)[0]): xnew = ma.array(x[i, :], mask=tmask) xcmp = ma.compressed(xnew) if i == 0: print ma.shape(xcmp)[0] print numpy.shape(x)[0] xout = numpy.zeros((numpy.shape(x)[0], ma.shape(xcmp)[0])) xout[i, :] = xcmp return (xout) else: print "Utility.Mask: dimensions of input arrays are not acceptable" return (numpy.array([]))
def test_testBasic1d(self): # Test of basic array creation and properties in 1 dimension. (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d assert_(not isMaskedArray(x)) assert_(isMaskedArray(xm)) assert_equal(shape(xm), s) assert_equal(xm.shape, s) assert_equal(xm.dtype, x.dtype) assert_equal(xm.size, reduce(lambda x, y:x * y, s)) assert_equal(count(xm), len(m1) - reduce(lambda x, y:x + y, m1)) assert_(eq(xm, xf)) assert_(eq(filled(xm, 1.e20), xf)) assert_(eq(x, xm))
def mask(x, xm, x0, x1): if numpy.ndim(xm) != 1: print "utility.mask(): array to used to derive mask must be 1D" return(numpy.array([])) xmask = ma.masked_outside(xm, x0, x1) tmask =ma.getmask(xmask) if numpy.ndim(x) == 1: xnew = ma.array(x, mask=tmask) return(xnew.compressed()) if numpy.ndim(x) == 2: for i in range(0, numpy.shape(x)[0]): xnew= ma.array(x[i,:], mask=tmask) xcmp = ma.compressed(xnew) if i == 0: print ma.shape(xcmp)[0] print numpy.shape(x)[0] xout = numpy.zeros((numpy.shape(x)[0], ma.shape(xcmp)[0])) xout[i,:] = xcmp return(xout) else: print "Utility.Mask: dimensions of input arrays are not acceptable" return(numpy.array([]))
def woe(factor, sites, unit_cell=1): '''Weight of evidence method (multiclass form). @param factor Multiclass pattern array used for prediction of point objects (sites). @param sites Array layer consisting of the locations at which the point objects are known to occur. @param unit_cell Method parameter, pixelsize of resampled rasters. @return masked array Array of total weights of each factor. ''' # Get list of categories from the factor raster categories = get_gradations(factor.compressed()) # Try to binarize sites: sCategories = get_gradations(sites.compressed()) if len(sCategories) != 2: raise WoeError('Site raster must be binary!') sites = binaryzation(sites, [sCategories[1]]) # List of the weights of evidence: # weights[0] is (wPlus, wMinus) for the first category, weights[1] is (wPlus, wMinus) for the second category, ... weights = [] if len(categories) >= 2: for cat in categories: fct = binaryzation(factor, [cat]) weights.append(_binary_woe(fct, sites, unit_cell)) else: raise WoeError('Wrong count of categories in the factor raster!') wTotalMin = sum([w[1] for w in weights]) # List of total weights of evidence of the categories: # wMap[0] is the total weight of the first category, wMap[1] is the total weight of the second category, ... wMap = [w[0] + wTotalMin - w[1] for w in weights] # If len(categories) = 2, then [w[0] + wTotalMin - w[1] for w in weights] increases the answer. # In this case: if len(categories) == 2: wMap = [w/2 for w in wMap] resultMap =np.zeros(ma.shape(factor)) for i,cat in enumerate(categories): resultMap[factor==cat] = wMap[i] resultMap = ma.array(data=resultMap, mask=factor.mask) result = {'map': resultMap, 'categories': categories, 'weights': wMap} return result
def autoNorm(dataSet): ''' 归一化数值 :param dataSet: 集合 :return: ''' minVals = dataSet.min(0) maxVals = dataSet.max(0) ranges = maxVals - minVals # 获取dataset的维度 normDataSet = zeros(shape(dataSet)) # 集合的行数 m = dataSet.shape[0] normDataSet = dataSet - tile(minVals, (m, 1)) normDataSet = normDataSet / tile(ranges, (m, 1)) return normDataSet, ranges, minVals
def woe(factor, sites, unit_cell=1): '''Weight of evidence method (multiclass form). @param factor Multiclass pattern array used for prediction of point objects (sites). @param sites Array layer consisting of the locations at which the point objects are known to occur. @param unit_cell Method parameter, pixelsize of resampled rasters. @return masked array Array of total weights of each factor. ''' # Get list of categories from the factor raster categories = get_gradations(factor.compressed()) # Try to binarize sites: sCategories = get_gradations(sites.compressed()) if len(sCategories) != 2: raise WoeError('Site raster must be binary!') sites = binaryzation(sites, [sCategories[1]]) # List of the weights of evidence: # weights[0] is (wPlus, wMinus) for the first category, weights[1] is (wPlus, wMinus) for the second category, ... weights = [] if len(categories) >= 2: for cat in categories: fct = binaryzation(factor, [cat]) weights.append(_binary_woe(fct, sites, unit_cell)) else: raise WoeError('Wrong count of categories in the factor raster!') wTotalMin = sum([w[1] for w in weights]) # List of total weights of evidence of the categories: # wMap[0] is the total weight of the first category, wMap[1] is the total weight of the second category, ... wMap = [w[0] + wTotalMin - w[1] for w in weights] # If len(categories) = 2, then [w[0] + wTotalMin - w[1] for w in weights] increases the answer. # In this case: if len(categories) == 2: wMap = [w / 2 for w in wMap] resultMap = np.zeros(ma.shape(factor)) for i, cat in enumerate(categories): resultMap[factor == cat] = wMap[i] resultMap = ma.array(data=resultMap, mask=factor.mask) result = {'map': resultMap, 'categories': categories, 'weights': wMap} return result
def make_noise_factorizable(noise, weight_prior=1.e3): r"""Convert noise diag such that the factor into a function a frequency times a function of pixel by taking means over the original weights. input noise_diag; output weight weight_prior used to be 10^-30 before prior applied """ print "making the noise factorizable" #noise[noise < weight_prior] = 1.e-30 #noise = 1. / noise #noise[noise < 5.e-5] = 0. if noise[noise != 0].min() > 1. / weight_prior: logger.warning('Noise Too High, ignore weight_prior %3.2e' % weight_prior) else: noise[noise > 1. / weight_prior] = 1.e30 noise = ma.array(noise) # Get the freqency averaged noise per pixel. Propagate mask in any # frequency to all frequencies. for noise_index in range(ma.shape(noise)[0]): if np.all(noise[noise_index, ...] > 1.e20): noise[noise_index, ...] = ma.masked noise_fmean = ma.mean(noise, 0) noise_fmean[noise_fmean > 1.e20] = ma.masked # Get the pixel averaged noise in each frequency. noise[noise > 1.e20] = ma.masked noise /= noise_fmean noise_pmean = ma.mean(ma.mean(noise, 1), 1) # Combine. noise = noise_pmean[:, None, None] * noise_fmean[None, :, :] #noise[noise == 0] = np.inf noise[noise == 0] = ma.masked weight = (1. / noise) weight = weight.filled(0) cut_l = np.percentile(weight, 10) cut_h = np.percentile(weight, 80) weight[weight < cut_l] = cut_l weight[weight > cut_h] = cut_h return weight
def test_testBasic2d(self): # Test of basic array creation and properties in 2 dimensions. for s in [(4, 3), (6, 2)]: (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d x.shape = s y.shape = s xm.shape = s ym.shape = s xf.shape = s assert_(not isMaskedArray(x)) assert_(isMaskedArray(xm)) assert_equal(shape(xm), s) assert_equal(xm.shape, s) assert_equal(xm.size, reduce(lambda x, y: x * y, s)) assert_equal(count(xm), len(m1) - reduce(lambda x, y: x + y, m1)) assert_(eq(xm, xf)) assert_(eq(filled(xm, 1.e20), xf)) assert_(eq(x, xm)) self.setup()
def test_testBasic2d(self): # Test of basic array creation and properties in 2 dimensions. for s in [(4, 3), (6, 2)]: (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d x.shape = s y.shape = s xm.shape = s ym.shape = s xf.shape = s self.assertFalse(isMaskedArray(x)) self.assertTrue(isMaskedArray(xm)) self.assertEqual(shape(xm), s) self.assertEqual(xm.shape, s) self.assertEqual(xm.size, reduce(lambda x, y: x * y, s)) self.assertEqual(count(xm), len(m1) - reduce(lambda x, y: x + y, m1)) self.assertTrue(eq(xm, xf)) self.assertTrue(eq(filled(xm, 1.0e20), xf)) self.assertTrue(eq(x, xm)) self.setUp()
def woe(factor, sites, unit_cell=1): '''Weight of evidence method (multiclass form). @param factor Multiclass pattern array used for prediction of point objects (sites). @param sites Array layer consisting of the locations at which the point objects are known to occur. @param unit_cell Method parameter, pixelsize of resampled rasters. @return [wMap1, wMap2, ...] Total weights of each factor. ''' result =np.zeros(ma.shape(factor)) # Get list of classes from the factor raster classes = get_gradations(factor.compressed()) # Try to binarize sites: sClasses = get_gradations(sites.compressed()) if len(sClasses) != 2: raise WoeError('Site raster must be binary!') sites = binaryzation(sites, [sClasses[1]]) weights = [] # list of the weights of evidence if len(classes) >= 2: for cl in classes: fct = binaryzation(factor, [cl]) weights.append(_binary_woe(fct, sites, unit_cell)) else: raise WoeError('Wrong count of classes in the factor raster!') wTotalMin = sum([w[1] for w in weights]) wMap = [w[0] + wTotalMin - w[1] for w in weights] # If len(classes) = 2, then [w[0] + wTotalMin - w[1] for w in weights] increases the answer. # In this case: if len(classes) == 2: wMap = [w/2 for w in wMap] for i,cl in enumerate(classes): result[factor==cl] = wMap[i] result = ma.array(data=result, mask=factor.mask) return result
def make_factorizable(noise): r"""factorize the noise""" noise[noise < weight_prior] = 1.e-30 noise = 1. / noise noise = ma.array(noise) # Get the freqency averaged noise per pixel. Propagate mask in any # frequency to all frequencies. for noise_index in range(ma.shape(noise)[0]): if sp.all(noise[noise_index, ...] > 1.e20): noise[noise_index, ...] = ma.masked noise_fmean = ma.mean(noise, 0) noise_fmean[noise_fmean > 1.e20] = ma.masked # Get the pixel averaged noise in each frequency. noise[noise > 1.e20] = ma.masked noise /= noise_fmean noise_pmean = ma.mean(ma.mean(noise, 1), 1) # Combine. noise = noise_pmean[:, None, None] * noise_fmean[None, :, :] noise = (1. / noise).filled(0) return noise
def test_testOddFeatures(self): # Test of other odd features x = arange(20) x = x.reshape(4, 5) x.flat[5] = 12 assert_(x[1, 0] == 12) z = x + 10j * x assert_(eq(z.real, x)) assert_(eq(z.imag, 10 * x)) assert_(eq((z * conjugate(z)).real, 101 * x * x)) z.imag[...] = 0.0 x = arange(10) x[3] = masked assert_(str(x[3]) == str(masked)) c = x >= 8 assert_(count(where(c, masked, masked)) == 0) assert_(shape(where(c, masked, masked)) == c.shape) z = where(c, x, masked) assert_(z.dtype is x.dtype) assert_(z[3] is masked) assert_(z[4] is masked) assert_(z[7] is masked) assert_(z[8] is not masked) assert_(z[9] is not masked) assert_(eq(x, z)) z = where(c, masked, x) assert_(z.dtype is x.dtype) assert_(z[3] is masked) assert_(z[4] is not masked) assert_(z[7] is not masked) assert_(z[8] is masked) assert_(z[9] is masked) z = masked_where(c, x) assert_(z.dtype is x.dtype) assert_(z[3] is masked) assert_(z[4] is not masked) assert_(z[7] is not masked) assert_(z[8] is masked) assert_(z[9] is masked) assert_(eq(x, z)) x = array([1., 2., 3., 4., 5.]) c = array([1, 1, 1, 0, 0]) x[2] = masked z = where(c, x, -x) assert_(eq(z, [1., 2., 0., -4., -5])) c[0] = masked z = where(c, x, -x) assert_(eq(z, [1., 2., 0., -4., -5])) assert_(z[0] is masked) assert_(z[1] is not masked) assert_(z[2] is masked) assert_(eq(masked_where(greater(x, 2), x), masked_greater(x, 2))) assert_(eq(masked_where(greater_equal(x, 2), x), masked_greater_equal(x, 2))) assert_(eq(masked_where(less(x, 2), x), masked_less(x, 2))) assert_(eq(masked_where(less_equal(x, 2), x), masked_less_equal(x, 2))) assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2))) assert_(eq(masked_where(equal(x, 2), x), masked_equal(x, 2))) assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2))) assert_(eq(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4])) assert_(eq(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199])) assert_(eq(masked_inside(array(list(range(5)), mask=[1, 0, 0, 0, 0]), 1, 3).mask, [1, 1, 1, 1, 0])) assert_(eq(masked_outside(array(list(range(5)), mask=[0, 1, 0, 0, 0]), 1, 3).mask, [1, 1, 0, 0, 1])) assert_(eq(masked_equal(array(list(range(5)), mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 0])) assert_(eq(masked_not_equal(array([2, 2, 1, 2, 1], mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 1])) assert_(eq(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]), [99, 99, 3, 4, 5])) atest = ones((10, 10, 10), dtype=np.float32) btest = zeros(atest.shape, MaskType) ctest = masked_where(btest, atest) assert_(eq(atest, ctest)) z = choose(c, (-x, x)) assert_(eq(z, [1., 2., 0., -4., -5])) assert_(z[0] is masked) assert_(z[1] is not masked) assert_(z[2] is masked) x = arange(6) x[5] = masked y = arange(6) * 10 y[2] = masked c = array([1, 1, 1, 0, 0, 0], mask=[1, 0, 0, 0, 0, 0]) cm = c.filled(1) z = where(c, x, y) zm = where(cm, x, y) assert_(eq(z, zm)) assert_(getmask(zm) is nomask) assert_(eq(zm, [0, 1, 2, 30, 40, 50])) z = where(c, masked, 1) assert_(eq(z, [99, 99, 99, 1, 1, 1])) z = where(c, 1, masked) assert_(eq(z, [99, 1, 1, 99, 99, 99]))