def test_partial_dependence_helpers(est, method, target_feature):
    # Check that what is returned by _partial_dependence_brute or
    # _partial_dependence_recursion is equivalent to manually setting a target
    # feature to a given value, and computing the average prediction over all
    # samples.
    # This also checks that the brute and recursion methods give the same
    # output.

    X, y = make_regression(random_state=0)
    # The 'init' estimator for GBDT (here the average prediction) isn't taken
    # into account with the recursion method, for technical reasons. We set
    # the mean to 0 to that this 'bug' doesn't have any effect.
    y = y - y.mean()
    est.fit(X, y)

    # target feature will be set to .5 and then to 123
    features = np.array([target_feature], dtype=np.int32)
    grid = np.array([[.5],
                     [123]])

    if method == 'brute':
        pdp = _partial_dependence_brute(est, grid, features, X,
                                        response_method='auto')
    else:
        pdp = _partial_dependence_recursion(est, grid, features)

    mean_predictions = []
    for val in (.5, 123):
        X_ = X.copy()
        X_[:, target_feature] = val
        mean_predictions.append(est.predict(X_).mean())

    pdp = pdp[0]  # (shape is (1, 2) so make it (2,))
    assert_allclose(pdp, mean_predictions, atol=1e-3)
Example #2
0
    def get_tracedata(self, format = 'AmpPha', single=False):
        '''
        Get the data of the current trace

        Input:
            format (string) : 'AmpPha': Amp in dB and Phase, 'RealImag',

        Output:
            'AmpPha':_ Amplitude and Phase
        '''
        #data = self._visainstrument.ask_for_values(':FORMAT REAL,32;*CLS;CALC1:DATA:NSW? SDAT,1;*OPC',format=1)      
        data = self._visainstrument.ask_for_values('FORM:DATA REAL; FORM:BORD SWAPPED; CALC%i:SEL:DATA:SDAT?'%(self._ci), format = visa.double)      
        data_size = numpy.size(data)
        datareal = numpy.array(data[0:data_size:2])
        dataimag = numpy.array(data[1:data_size:2])
          
        if format.upper() == 'REALIMAG':
          if self._zerospan:
            return numpy.mean(datareal), numpy.mean(dataimag)
          else:
            return datareal, dataimag
        elif format.upper() == 'AMPPHA':
          if self._zerospan:
            datareal = numpy.mean(datareal)
            dataimag = numpy.mean(dataimag)
            dataamp = numpy.sqrt(datareal*datareal+dataimag*dataimag)
            datapha = numpy.arctan(dataimag/datareal)
            return dataamp, datapha
          else:
            dataamp = numpy.sqrt(datareal*datareal+dataimag*dataimag)
            datapha = numpy.arctan2(dataimag,datareal)
            return dataamp, datapha
        else:
          raise ValueError('get_tracedata(): Format must be AmpPha or RealImag') 
Example #3
0
def test_RadiusNeighborsRegressor_multioutput_with_uniform_weight():
    """Test radius neighbors in multi-output regression (uniform weight)"""

    rng = check_random_state(0)
    n_features = 5
    n_samples = 40
    n_output = 4

    X = rng.rand(n_samples, n_features)
    y = rng.rand(n_samples, n_output)
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

    for algorithm, weights in product(ALGORITHMS, [None, 'uniform']):

        rnn = neighbors. RadiusNeighborsRegressor(weights=weights,
                                                  algorithm=algorithm)
        rnn.fit(X_train, y_train)

        neigh_idx = rnn.radius_neighbors(X_test, return_distance=False)
        y_pred_idx = np.array([np.mean(y_train[idx], axis=0)
                               for idx in neigh_idx])

        y_pred_idx = np.array(y_pred_idx)
        y_pred = rnn.predict(X_test)

        assert_equal(y_pred_idx.shape, y_test.shape)
        assert_equal(y_pred.shape, y_test.shape)
        assert_array_almost_equal(y_pred, y_pred_idx)
 def test_continuum_seismicity(self):
     '''
     Tests the function hmtk.strain.shift.Shift.continuum_seismicity - 
     the python implementation of the Subroutine Continuum Seismicity from
     the Fortran 90 code GSRM.f90
     '''
     self.strain_model = GeodeticStrain()
     # Define a simple strain model
     test_data = {'longitude': np.zeros(3, dtype=float),
                  'latitude': np.zeros(3, dtype=float),
                  'exx': np.array([1E-9, 1E-8, 1E-7]),
                  'eyy': np.array([5E-10, 5E-9, 5E-8]),
                  'exy': np.array([2E-9, 2E-8, 2E-7])}
     self.strain_model.get_secondary_strain_data(test_data)
     self.model = Shift([5.66, 6.66])
     threshold_moment = moment_function(np.array([5.66, 6.66]))
     
     expected_rate = np.array([[-14.43624419, -22.48168502],
                               [-13.43624419, -21.48168502],
                               [-12.43624419, -20.48168502]]) 
     np.testing.assert_array_almost_equal(
         expected_rate,
         np.log10(self.model.continuum_seismicity(
             threshold_moment,
             self.strain_model.data['e1h'],
             self.strain_model.data['e2h'],
             self.strain_model.data['err'],
             BIRD_GLOBAL_PARAMETERS['OSRnor'])))
    def testCNCS(self):
        # CNCS_7860 is not an incoherent scatterer but for this test
        # it doesn't matter
        SA, Flux = MDNormSCDPreprocessIncoherent(Filename='CNCS_7860',
                                                 MomentumMin=1,
                                                 MomentumMax=1.5)

        # Just compare 10 points of the Flux
        flux_cmp = np.array([0.00000000e+00, 7.74945234e-04, 4.96143098e-03,
                             1.18914010e-02, 1.18049991e-01, 7.71872176e-01,
                             9.93078957e-01, 9.96312349e-01, 9.98450129e-01,
                             1.00000002e+00])
        np.testing.assert_allclose(Flux.extractY()[0][::1000], flux_cmp)
        self.assertEqual(Flux.getXDimension().name, 'Momentum')
        self.assertEqual(Flux.getXDimension().getUnits(), 'Angstrom^-1')
        self.assertEqual(Flux.blocksize(), 10000)
        self.assertEqual(Flux.getNumberHistograms(), 1)

        # Compare every 20-th bin of row 64
        SA_cmp = np.array([0.11338311, 0.18897185, 0.15117748, 0.11338311, 0.03779437,
                           0.07558874, 0.15117748, 0.18897185, 0.03779437, 0.15117748,
                           0.11338311, 0.07558874, 0.03779437, 0.        , 0.56691555,
                           0.26456059, 0.11338311, 0.07558874, 0.11338311, 0.])
        np.testing.assert_allclose(SA.extractY().reshape((-1,128))[::20,64], SA_cmp)
        self.assertEqual(SA.getXDimension().name, 'Momentum')
        self.assertEqual(SA.getXDimension().getUnits(), 'Angstrom^-1')
        self.assertEqual(SA.blocksize(), 1)
        self.assertEqual(SA.getNumberHistograms(), 51200)
        self.assertEqual(SA.getNEvents(), 51200)
Example #6
0
    def __init__(self,data,p,q,formula):

        # Initialize TSM object
        super(EGARCHMReg,self).__init__('EGARCHMReg')

        # Latent variables
        self.p = p
        self.q = q
        self.max_lag = max(self.p,self.q)  
        self.z_no = self.p + self.q + 2
        self._z_hide = 0 # Whether to cutoff variance latent variables from results
        self.supported_methods = ["MLE","PML","Laplace","M-H","BBVI"]
        self.default_method = "MLE"
        self.multivariate_model = False
        self.leverage = False
        self.model_name = "EGARCHMReg(" + str(self.p) + "," + str(self.q) + ")"

        # Format the data
        self.is_pandas = True # This is compulsory for this model type
        self.data_original = data
        self.formula = formula
        self.y, self.X = dmatrices(formula, data)
        self.z_no += self.X.shape[1]*2
        self.y_name = self.y.design_info.describe()
        self.data_name = self.y_name
        self.X_names = self.X.design_info.describe().split(" + ")
        self.y = np.array([self.y]).ravel()
        self.data = self.y
        self.X = np.array([self.X])[0]
        self.index = data.index
        self.initial_values = np.zeros(self.z_no)

        self._create_latent_variables()
Example #7
0
def mult_leave_out (random, percent):
    i = 1
    J = [[(5)]]
    for row in random:
        test,train = np.vsplit(random, np.array([i]))
        if i == 1:
            y_all, new_feature_number = ypred_leave_one_out(train, test, percent)
        if i != 1:
            if i < sample_n:
                train2,test = np.vsplit(test, np.array([i-1])) 
                train = np.vstack((train2,train))
                y_all, new_feature_number= ypred_leave_one_out(train, test, percent)
            if i > training_n:
                train,test = np.vsplit(random, np.array([training_n]))
                y_all, new_feature_number = ypred_leave_one_out(train, test, percent)
    
        J = np.append(J, np.array(y_all))
        i = i + 1
    J = np.delete(J,0,0)
    ground_truth = random[:,0]
    
    #print J, ground_truth
    slope, intercept, r_value, p_value, std_err = stats.linregress(ground_truth,J)
    #print r_value
    #print np.square(r_value)
    return r_value, std_err, p_value, slope, intercept, ground_truth, J, new_feature_number
Example #8
0
def vertex_transform1(vertex):
    """
    This transform was applied on the original surface.
    """
    return np.dot(rotation_matrix(np.array([0.0, 0.0, 1.0]), math.pi),
                  np.dot(rotation_matrix(np.array([1.0, 0.0, 0.0]), -math.pi / 1.6),
                         np.array([float(x) / 1.5 for x in vertex[:3]]) + np.array([0.0, -40.0, 20.0])))
Example #9
0
def init():
    global theMesh,  theLight, theCamera, \
           theScreen,    resolution
    initializeVAO()
    glEnable(GL_CULL_FACE)
    glEnable(GL_DEPTH_TEST)

    # Add our object
    # LIGHT
    theLight = N.array((-0.577, 0.577, 0.577, 0.0),dtype=N.float32)
    # OBJECT
    phongshader = makeShader("phongshader.vert","phongshader.frag")
    verts, elements = readOBJ("suzanne.obj")
    suzanneVerts = getArrayBuffer(verts)
    suzanneElements = getElementBuffer(elements)
    suzanneNum = len(elements)
    theMesh = coloredMesh(N.array((1.0, 0.5, 1.0, 1.0), dtype=N.float32),
                          suzanneVerts,
                          suzanneElements,
                          suzanneNum,
                          phongshader)

    # CAMERA
    width,height = theScreen.get_size()
    aspectRatio = float(width)/float(height)
    near = 0.01
    far = 100.0
    lens = 4.0  # "longer" lenses mean more telephoto
    theCamera = Camera(lens, near, far, aspectRatio)
    theCamera.moveBack(6)
	def calculate_user_similarity(user_rating_dict,user_list,restaurant_list,score_matrix,user_mean):
		similarity_matrix = []
		for row in range(len(user_list)):
			similarity_vector = []
			list1 = user_rating_dict[user_list[row]].keys()
			mean1 = user_mean[row]
			for col in range(row,len(user_list)):		
				list2 = user_rating_dict[user_list[col]].keys()
				mean2 = user_mean[col]
				join_list = list(set(list1+list2))
				rating_vector1 = []
				rating_vector2 = []
				for item in join_list:
					if item in list1:
						rating_vector1.append(user_rating_dict[user_list[row]][item]-mean1)
					else:
						rating_vector1.append(score_matrix[row,restaurant_list.index(item)]-mean1)
					if item in list2:
						rating_vector2.append(user_rating_dict[user_list[col]][item]-mean2)
					else:
						rating_vector2.append(score_matrix[col,restaurant_list.index(item)]-mean2)
				similarity = numpy.sum(numpy.array(rating_vector1)*numpy.array(rating_vector2))/sqrt(numpy.sum(numpy.square(rating_vector1))*numpy.sum(numpy.square(rating_vector2)))
				similarity_vector.append(similarity)
			similarity_matrix.append(similarity_vector)
		similarity_matrix = numpy.array(similarity_matrix)
		for col in range(len(user_list)):
			for row in range(col,len(user_list)):
				similarity_matrix[row,col] = similarity_matrix[col,row]
		return similarity_matrix
    def test_array_richcompare_legacy_weirdness(self):
        # It doesn't really work to use assert_deprecated here, b/c part of
        # the point of assert_deprecated is to check that when warnings are
        # set to "error" mode then the error is propagated -- which is good!
        # But here we are testing a bunch of code that is deprecated *because*
        # it has the habit of swallowing up errors and converting them into
        # different warnings. So assert_warns will have to be sufficient.
        assert_warns(FutureWarning, lambda: np.arange(2) == "a")
        assert_warns(FutureWarning, lambda: np.arange(2) != "a")
        # No warning for scalar comparisons
        with warnings.catch_warnings():
            warnings.filterwarnings("error")
            assert_(not (np.array(0) == "a"))
            assert_(np.array(0) != "a")
            assert_(not (np.int16(0) == "a"))
            assert_(np.int16(0) != "a")

        for arg1 in [np.asarray(0), np.int16(0)]:
            struct = np.zeros(2, dtype="i4,i4")
            for arg2 in [struct, "a"]:
                for f in [operator.lt, operator.le, operator.gt, operator.ge]:
                    if sys.version_info[0] >= 3:
                        # py3
                        with warnings.catch_warnings() as l:
                            warnings.filterwarnings("always")
                            assert_raises(TypeError, f, arg1, arg2)
                            assert_(not l)
                    else:
                        # py2
                        assert_warns(DeprecationWarning, f, arg1, arg2)
    def test(self):

        with self.test_session() as sess:

            m = tf.constant(np.array([
                [1.0, 2.0],
                [2.0, 0.0]
            ], dtype=np.float32))

            l = linear(m, 4)

            result = sess.run(l, {
                'SimpleLinear/Matrix:0': np.array([
                    [1.0, 2.0],
                    [1.0, 2.0],
                    [1.0, 2.0],
                    [1.0, 2.0],
                ]),
                'SimpleLinear/Bias:0': np.array([
                    0.0,
                    1.0,
                    2.0,
                    3.0,
                ]),
            })

            self.assertAllClose(result, np.array([
                [5.0, 6.0, 7.0, 8.0],
                [2.0, 3.0, 4.0, 5.0],
            ]))
            print(result)
Example #13
0
def loadTrajectoryData(inFile = UJILocDataFile):

	with open(UJILocDataFile, 'r') as dataFile: 
		data = dataFile.read()

	# 9-axis IMU data
	# trajectory: dictionary with three elements
	# N is number of samples in the trajectory (data taken at 10Hz)
	# mag: Nx3 numpy array where each line has XYZ mag data
	# gyro: Nx3 numpy array where each line has XYZ gyro vel data
	# accel: Nx3 numpy array where each line has XYZ lin accelerometer data
	segments = data.split("<", 2)
	IMUDataStr = segments[0].split('\n')[:-1]
	magArr = []
	oriArr = []	
	accelArr = []

	for i, lineStr in enumerate(IMUDataStr): 

		lineStr = lineStr.split(' ', 10)[:-1]
		lineStr = [float(x) for x in lineStr]
		magArr.append(lineStr[1:4]) # xyz mag data for sample
		accelArr.append(lineStr[4:7]) # xyz accelerometer data for single samp
		oriArr.append(lineStr[7:10]) # xyz gyro data for sample

	# values initially are given as euler angles which are not good for imu-type calculations. 
	# so we fix em! 	
	gyroArr = rawSensorStateProc.orientationToGyro(oriArr) 
	initOrientationMatrix = rawSensorStateProc.calcInitialOrientation(oriArr[0])

	# IMUData = [{'mag': magArr, 'gyro': gyroArr, 'accel': accelArr}]
	
	# process waypoint data
	# each waypoint consists of a latitude coordinate, longitude coordinate,
	# and index (what IMU dataopoint it represents)
	waypoints = []
	waypointStr = segments[1].split(">", 2)
	numWaypoints = int(waypointStr[0])
	waypointLns = waypointStr[1].lstrip().split('\n')

	for i, lineStr in enumerate(waypointLns): 

		line = lineStr.split(' ', WAYPOINTS_ELEMS_PER_LINE)
		line = [float(x) for x in line]
		
		if i == 0:
			waypoints.append({'lat': line[0], 'long': line[1], 'index': line[4]}) 
		
		waypoints.append({'lat': line[2], 'long': line[3], 'index': line[5]})

		seqLen = line[5]

	
	traj = ({'waypoints': np.array(waypoints), 'mag': np.array(magArr), 'gyro': np.array(gyroArr), 
			 'accel': np.array(accelArr), 'orientSensed': np.array(oriArr), 
			 'initOrient': initOrientationMatrix, 'seqLen': seqLen})

	return traj

# loadTrajectoryData()
Example #14
0
 def test_2d_complex_same(self):
     a = array([[1+2j,3+4j,5+6j],[2+1j,4+3j,6+5j]])
     c = signal.fftconvolve(a,a)
     d = array([[-3+4j,-10+20j,-21+56j,-18+76j,-11+60j],\
                [10j,44j,118j,156j,122j],\
                [3+4j,10+20j,21+56j,18+76j,11+60j]])
     assert_array_almost_equal(c,d)
Example #15
0
    def test__build_row_representation(self):
        id_cols = [
            array([2,4,6]),
            array([1,2,3])]
        attribute_cols = {
            'col1':array([3,2,1]),
            'col2':array([2,1,0]),
            'col3':array([4,5,6])                  
        }
        
        expected = {
            (2,1):{'col1':3,
                   'col2':2,
                   'col3':4},
            (4,2):{'col1':2,
                   'col2':1,
                   'col3':5},
            (6,3):{'col1':1,
                   'col2':0,
                   'col3':6},            
        }
        
        dataset = self._get_dataset(dataset_name = 'test', 
                                    cache_directory = self.temp_cache_path, 
                                    year = 1980)
        dataset_junior = DatasetJunior(dataset=dataset,
                                       name = 'test')

        
        dataset_junior._build_row_representation(id_cols = id_cols, 
                                                   attribute_cols = attribute_cols) 
        output = dataset_junior.row_representation
          
        self.assertEqual(expected,output)
Example #16
0
    def setUp(self):
        x = numpy.array([ 8.375, 7.545, 8.828, 8.5  , 1.757, 5.928,
                          8.43 , 7.78 , 9.865, 5.878, 8.979, 4.732,
                          3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
                          6.04 , 9.63 , 7.712, 3.382, 4.489, 6.479,
                          7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
                          7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
        X = x.reshape(6, 6)
        XX = x.reshape(3, 2, 2, 3)

        m = numpy.array([0, 1, 0, 1, 0, 0,
                         1, 0, 1, 1, 0, 1,
                         0, 0, 0, 1, 0, 1,
                         0, 0, 0, 1, 1, 1,
                         1, 0, 0, 1, 0, 0,
                         0, 0, 1, 0, 1, 0])
        mx = array(data=x, mask=m)
        mX = array(data=X, mask=m.reshape(X.shape))
        mXX = array(data=XX, mask=m.reshape(XX.shape))

        m2 = numpy.array([1, 1, 0, 1, 0, 0,
                          1, 1, 1, 1, 0, 1,
                          0, 0, 1, 1, 0, 1,
                          0, 0, 0, 1, 1, 1,
                          1, 0, 0, 1, 1, 0,
                          0, 0, 1, 0, 1, 1])
        m2x = array(data=x, mask=m2)
        m2X = array(data=X, mask=m2.reshape(X.shape))
        m2XX = array(data=XX, mask=m2.reshape(XX.shape))
        self.d = (x, X, XX, m, mx, mX, mXX)
Example #17
0
def lars_regression_noise_ipyparallel(pars): 
    import numpy as np
    import os
    import sys
    import gc
        
    
    Y_name,C_name,noise_sn,idxs_C, idxs_Y=pars
    Y=np.load(Y_name,mmap_mode='r')
    Y=np.array(Y[idxs_Y,:])
    C=np.load(C_name,mmap_mode='r')
    C=np.array(C)
    _,T=np.shape(C)
    #sys.stdout = open(str(os.getpid()) + ".out", "w")
    st=time.time()
    As=[]    
    #print "*****************:" + str(idxs_Y[0]) + ',' + str(idxs_Y[-1])
    sys.stdout.flush()    
    for y,px in zip(Y,idxs_Y):  
        #print str(time.time()-st) + ": Pixel" + str(px)
        sys.stdout.flush()    
        c=C[idxs_C[px],:]
        if np.size(c)>0:             
            sn=noise_sn[px]**2*T            
            _,_,a,_,_=lars_regression_noise(y, c.T, 1, sn)
            if not np.isscalar(a):                
                a=a.T  
                 
            As.append((px,idxs_C[px],a))
    
    del Y
    del C
    gc.collect()
    
    return As#As
Example #18
0
 def test_readXMLBIF(self):
     bn = XMLBIFParser.parse("primo2/tests/slippery.xbif")
     
     nodes = bn.get_all_nodes()        
     self.assertTrue("slippery_road" in nodes)
     self.assertTrue("sprinkler" in nodes)
     self.assertTrue("rain" in nodes)
     self.assertTrue("wet_grass" in nodes)
     self.assertTrue("winter" in nodes)
     self.assertEqual(len(nodes), 5)
     slipperyNode = bn.get_node("slippery_road")
     self.assertTrue("rain" in slipperyNode.parents)
     sprinklerNode = bn.get_node("sprinkler")
     self.assertTrue("winter" in sprinklerNode.parents)
     rainNode = bn.get_node("rain")
     self.assertTrue("winter" in rainNode.parents)
     cpt = np.array([[0.8,0.1],[0.2,0.9]])        
     np.testing.assert_array_almost_equal(rainNode.cpd, cpt)
     
     wetNode = bn.get_node("wet_grass")
     self.assertTrue("sprinkler" in wetNode.parents)
     self.assertTrue("rain" in wetNode.parents)
     self.assertTrue("true" in wetNode.values)
     cpt = np.array([[[0.95, 0.8],[0.1,0.0]], [[0.05, 0.2],[0.9, 1.0]]])
     self.assertEqual(wetNode.get_probability("false", {"rain":["true"], "sprinkler":["false"]}),0.2)
     self.assertEqual(wetNode.get_probability("true", {"rain":["false"], "sprinkler":["true"]}),0.1)
Example #19
0
    def test3(self):
        convert_nbody = nbody_system.nbody_to_si(1.0 | units.MSun, 149.5e6 | units.km)

        instance = Huayno(convert_nbody)
        instance.initialize_code()
        instance.parameters.epsilon_squared = 0.00001 | units.AU**2
        
        stars = datamodel.Stars(2)
        star1 = stars[0]
        star2 = stars[1]

        star1.mass = units.MSun(1.0)
        star1.position = units.AU(numpy.array((-1.0,0.0,0.0)))
        star1.velocity = units.AUd(numpy.array((0.0,0.0,0.0)))
        star1.radius = units.RSun(1.0)

        star2.mass = units.MSun(1.0)
        star2.position = units.AU(numpy.array((1.0,0.0,0.0)))
        star2.velocity = units.AUd(numpy.array((0.0,0.0,0.0)))
        star2.radius = units.RSun(100.0)
        
        instance.particles.add_particles(stars)
    
        for x in range(1,2000,10):
            instance.evolve_model(x | units.day)
            instance.particles.copy_values_of_all_attributes_to(stars)
            stars.savepoint()
 def measure_objects(self, operand, workspace):
     '''Performs the measurements on the requested objects'''
     objects = workspace.get_objects(operand.operand_objects.value)
     if objects.has_parent_image:
         area_occupied = np.sum(objects.segmented[objects.parent_image.mask]>0)
         perimeter = np.sum(outline(np.logical_and(objects.segmented != 0,objects.parent_image.mask)))
         total_area = np.sum(objects.parent_image.mask)
     else:
         area_occupied = np.sum(objects.segmented > 0)
         perimeter = np.sum(outline(objects.segmented) > 0)
         total_area = np.product(objects.segmented.shape)
     m = workspace.measurements
     m.add_image_measurement(F_AREA_OCCUPIED%(operand.operand_objects.value),
                             np.array([area_occupied], dtype=float ))
     m.add_image_measurement(F_PERIMETER%(operand.operand_objects.value),
                             np.array([perimeter], dtype=float ))
     m.add_image_measurement(F_TOTAL_AREA%(operand.operand_objects.value),
                             np.array([total_area], dtype=float))
     if operand.should_save_image.value:
         binary_pixels = objects.segmented > 0
         output_image = cpi.Image(binary_pixels,
                                  parent_image = objects.parent_image)
         workspace.image_set.add(operand.image_name.value,
                                 output_image)
     return[[operand.operand_objects.value,
             str(area_occupied),str(perimeter),str(total_area)]]
Example #21
0
def condition_on_grades(user="c6961489"):
	c = new_conn.cursor()
	models = [None, None, None, None, None, None]
	for i in range(6):
		c.execute('SELECT easiness, ret_reps, ret_reps_since_lapse, lapses, pred_grade, acq_reps from discrete_log where user_id="%s" and grade=%d' % (user, i))
		x_train = np.array(c.fetchall())
		c.execute('SELECT interval_bucket from discrete_log where user_id="%s" and grade=%d' % (user, i))
		y_train = np.array(c.fetchall())[:,0]
		clf = SVC()
		clf.fit(x_train, y_train)
		print clf.score(x_train, y_train)
		models[i] = clf
	print "====================="
	c.execute('SELECT user_id from (select user_id, count(distinct grade) as cnt from discrete_log group by user_id) where cnt = 6 limit 5')
	users = [row[0] for row in c.fetchall()]
	scores = [0, 0, 0, 0, 0, 0]
	for user in users:
		for i in range(6):
			c.execute('SELECT easiness, ret_reps, ret_reps_since_lapse, lapses, pred_grade, acq_reps from discrete_log where user_id="%s" and grade=%d' % (user, i))
			x_train = np.array(c.fetchall())
			c.execute('SELECT interval_bucket from discrete_log where user_id="%s" and grade=%d' % (user, i))
			y_train = np.array(c.fetchall())[:,0]
			scores[i] += models[i].score(x_train, y_train)
	for i in range(6):
		scores[i] /= len(users);
		print scores[i]
Example #22
0
    def __init__(self, num_neurons, prev_layer=None):
        """Constructs a layer with given number of neurons.

        Args:
            num_neurons: Number of neurons in this layer.
            prev_layer: Previous layer which acts as input to this
                        layer. None for input layer.
        """

        # x : Activation vector of the neurons.
        # nets : Vector of weighted sum of inputs of the neurons.
        # deltas : Delta error vector, used to adjust the weights.
        self.x = np.array([0] * num_neurons)
        self.nets = np.array([0] * num_neurons)
        self.deltas = np.array([0] * num_neurons)

        self.prev_layer = prev_layer

        # If previous layer exists, create a weight matrix
        # with random values.
        if prev_layer:
            self.weights = []
            for i in range(num_neurons):

                # Each neuron is connected to all neurons of previous layer
                # plus a constant input of '1' (the weight of which is
                # bias). So total number of weights = num_inputs + 1.

                prev_x_len = len(prev_layer.x) + 1
                w = [get_random_weight(prev_x_len) for _ in range(prev_x_len)]
                self.weights.append(w)

            self.weights = np.matrix(self.weights)
Example #23
0
 def test_basic_instantiation(self):
     '''
     Tests the basic instantiation of the SHIFT class
     '''
     # Instantiatiation with float
     self.model = Shift(5.0)
     np.testing.assert_array_almost_equal(self.model.target_magnitudes,
                                          np.array([5.0]))
     self.assertEqual(self.model.number_magnitudes, 1)
     # Instantiation with a numpy array
     self.model = Shift(np.arange(5., 8., 0.5))
     np.testing.assert_array_almost_equal(self.model.target_magnitudes,
                                          np.arange(5., 8., 0.5))
     self.assertEqual(self.model.number_magnitudes, 6)
     # Instantiation with  list
     self.model = Shift([5., 6., 7., 8.])
     np.testing.assert_array_almost_equal(self.model.target_magnitudes,
                                          np.array([5., 6., 7., 8.]))
     self.assertEqual(self.model.number_magnitudes, 4)
     # Otherwise raise an error
     with self.assertRaises(ValueError) as ae:
         self.model = Shift(None)
     self.assertEqual(ae.exception.message,
                      'Minimum magnitudes must be float, list or array')
     # Check regionalisation - assuming defaults
     self.model = Shift(5.0)
     for region in self.model.regionalisation.keys():
         self.assertDictEqual(BIRD_GLOBAL_PARAMETERS[region],
                              self.model.regionalisation[region])
     np.testing.assert_array_almost_equal(np.log10(self.model.base_rate),
                                          np.array([-20.74610902]))
Example #24
0
    def test_emcee_lnpost(self):
        # check ln likelihood is calculated correctly. It should be
        # -0.5 * chi**2.
        result = self.mini.minimize()

        # obtain the numeric values
        # note - in this example all the parameters are varied
        fvars = np.array([par.value for par in result.params.values()])

        # calculate the cost function with scaled values (parameters all have
        # lower and upper bounds.
        scaled_fvars = []
        for par, fvar in zip(result.params.values(), fvars):
            par.value = fvar
            scaled_fvars.append(par.setup_bounds())

        val = self.mini.penalty(np.array(scaled_fvars))

        # calculate the log-likelihood value
        bounds = np.array([(par.min, par.max)
                           for par in result.params.values()])
        val2 = _lnpost(fvars,
                       self.residual,
                       result.params,
                       result.var_names,
                       bounds,
                       userargs=(self.x, self.data))

        assert_almost_equal(-0.5 * val, val2)
Example #25
0
  def __init__(self):
    """
    Setup tri33 cell.
    """
    vertices = numpy.array([[-1.0, -1.0],
                            [+1.0, -1.0],
                            [-1.0, +1.0]])
    quadPts = vertices[:]
    quadWts = numpy.array( [2.0/3.0, 2.0/3.0, 2.0/3.0])

    # Compute basis fns and derivatives at quadrature points
    basis = numpy.zeros( (3, 3), dtype=numpy.float64)
    basisDeriv = numpy.zeros( (3, 3, 2), dtype=numpy.float64)
    iQuad = 0
    for q in quadPts:
      basis[iQuad] = numpy.array([self.N0(q), self.N1(q), self.N2(q)],
                                 dtype=numpy.float64).reshape( (3,) )
      deriv = numpy.array([[self.N0p(q), self.N0q(q)],
                           [self.N1p(q), self.N1q(q)],
                           [self.N2p(q), self.N2q(q)]])      
      basisDeriv[iQuad] = deriv.reshape((3, 2))
      iQuad += 1

    self.cellDim = 2
    self.numCorners = len(vertices)
    self.numQuadPts = len(quadPts)
    self.vertices = vertices
    self.quadPts = quadPts
    self.quadWts = quadWts
    self.basis = basis
    self.basisDeriv = basisDeriv
    return
Example #26
0
def load_adm_sat_school_data(return_X_y=False):

    with open("./merged_adm_sat_data.csv") as csv_file:
        data_file = csv.reader(csv_file)
        temp = next(data_file)
        n_samples = int(temp[0])
        n_features = int(temp[1])
        target_names = np.array(temp[2:])


    df = pd.read_csv("./merged_adm_sat_data.csv", sep=",", usecols=(0, 1, 2, 3), skiprows=0)
    data = np.empty((n_samples, n_features), dtype=int)
    target = np.ma.empty((n_samples,), dtype=int)

    for index, row in df.iterrows():
        data[index] = np.asarray([df.iloc[index][0], df.iloc[index][1], df.iloc[index][2]], dtype=np.float)
        target[index] = np.asarray(df.iloc[index][3], dtype=np.int)

    feature_names = np.array(['ACT_AVG','SAT_AVG','GRAD_DEBT','REGION'])

    if return_X_y:
        return data, target

    return datasets.base.Bunch(data=data, target=target,
                 target_names=target_names,
                 DESCR='School Data set',
                 feature_names=feature_names)
Example #27
0
def test_kneighbors_classifier_predict_proba():
    """Test KNeighborsClassifier.predict_proba() method"""
    X = np.array([[0, 2, 0],
                  [0, 2, 1],
                  [2, 0, 0],
                  [2, 2, 0],
                  [0, 0, 2],
                  [0, 0, 1]])
    y = np.array([4, 4, 5, 5, 1, 1])
    cls = neighbors.KNeighborsClassifier(n_neighbors=3, p=1)  # cityblock dist
    cls.fit(X, y)
    y_prob = cls.predict_proba(X)
    real_prob = np.array([[0, 2. / 3, 1. / 3],
                          [1. / 3, 2. / 3, 0],
                          [1. / 3, 0, 2. / 3],
                          [0, 1. / 3, 2. / 3],
                          [2. / 3, 1. / 3, 0],
                          [2. / 3, 1. / 3, 0]])
    assert_array_equal(real_prob, y_prob)
    # Check that it also works with non integer labels
    cls.fit(X, y.astype(str))
    y_prob = cls.predict_proba(X)
    assert_array_equal(real_prob, y_prob)
    # Check that it works with weights='distance'
    cls = neighbors.KNeighborsClassifier(
        n_neighbors=2, p=1, weights='distance')
    cls.fit(X, y)
    y_prob = cls.predict_proba(np.array([[0, 2, 0], [2, 2, 2]]))
    real_prob = np.array([[0, 1, 0], [0, 0.4, 0.6]])
    assert_array_almost_equal(real_prob, y_prob)
Example #28
0
def rng_target_dist_field(batch_size, gtG, rng, max_dist, max_dist_to_compute,
                          nodes=None, compute_path=False):
  # Sample a single node, compute distance to all nodes less than max_dist,
  # sample nodes which are a particular distance away.
  dists = []; pred_maps = []; paths = []; start_node_ids = []
  end_node_ids = rng.choice(gtG.num_vertices(), size=(batch_size,),
                            replace=False).tolist()

  for i in range(batch_size):
    dist, pred_map = gt.topology.shortest_distance(
        gt.GraphView(gtG, reversed=True), source=gtG.vertex(end_node_ids[i]),
        target=None, max_dist=max_dist_to_compute, pred_map=True)
    dist = np.array(dist.get_array())
    pred_map = np.array(pred_map.get_array())
    dists.append(dist)
    pred_maps.append(pred_map)

    # Randomly sample nodes which are withing max_dist
    near_ids = np.where(dist <= max_dist)[0]
    start_node_id = rng.choice(near_ids, size=(1,), replace=False)[0]
    start_node_ids.append(start_node_id)

    path = None
    if compute_path:
      path = get_path_ids(start_node_ids[i], end_node_ids[i], pred_map)
    paths.append(path)

  return start_node_ids, end_node_ids, dists, pred_maps, paths
Example #29
0
def lstm(trainData, trainMark, testData, embedding_dim, embedding_matrix, maxlen, output_len):
    # 填充数据,将每个序列长度保持一致
    trainData = list(sequence.pad_sequences(trainData, maxlen=maxlen,
                                            dtype='float64'))  # sequence返回的是一个numpy数组,pad_sequences用于填充指定长度的序列,长则阶段,短则补0,由于下面序号为0时,对应值也为0,因此可以这样
    testData = list(sequence.pad_sequences(testData, maxlen=maxlen,
                                           dtype='float64'))  # sequence返回的是一个numpy数组,pad_sequences用于填充指定长度的序列,长则阶段,短则补0

    # 建立lstm神经网络模型
    model = Sequential()  # 多个网络层的线性堆叠,可以通过传递一个layer的list来构造该模型,也可以通过.add()方法一个个的加上层
    # model.add(Dense(256, input_shape=(train_total_vova_len,)))   #使用全连接的输入层
    model.add(Embedding(len(embedding_matrix), embedding_dim, weights=[embedding_matrix], mask_zero=False,
                        input_length=maxlen))  # 指定输入层,将高维的one-hot转成低维的embedding表示,第一个参数大或等于0的整数,输入数据最大下标+1,第二个参数大于0的整数,代表全连接嵌入的维度
    # lstm层,也是比较核心的层
    model.add(LSTM(256))  # 256对应Embedding输出维度,128是输入维度可以推导出来
    model.add(Dropout(0.5))  # 每次在参数更新的时候以一定的几率断开层的链接,用于防止过拟合
    model.add(Dense(output_len))  # 全连接,这里用于输出层,1代表输出层维度,128代表LSTM层维度可以自行推导出来
    model.add(Activation('softmax'))  # 输出用sigmoid激活函数
    # 编译该模型,categorical_crossentropy(亦称作对数损失,logloss),adam是一种优化器,class_mode表示分类模式
    model.compile(loss='categorical_crossentropy', optimizer='sgd')

    # 正式运行该模型,我知道为什么了,因为没有补0!!每个array的长度是不一样的,因此才会报错
    X = np.array(list(trainData))  # 输入数据
    print("X:", X)
    Y = np.array(list(trainMark))  # 标签
    print("Y:", Y)
    # batch_size:整数,指定进行梯度下降时每个batch包含的样本数
    # nb_epoch:整数,训练的轮数,训练数据将会被遍历nb_epoch次
    model.fit(X, Y, batch_size=200, nb_epoch=10)  # 该函数的X、Y应该是多个输入:numpy list(其中每个元素为numpy.array),单个输入:numpy.array

    # 进行预测
    A = np.array(list(testData))  # 输入数据
    print("A:", A)
    classes = model.predict(A)  # 这个是预测的数据
    return classes
Example #30
0
def generate_test_states(data, scaler):
    """
        Function that returns the next (s) pair from the input data one by one every time you call it. Requires a 
        scaler to have been computed so that we can approximate the vaues for the 'NA' pairs in the data.
    """
    # Get the known states
    known_states = [state for state in get_known_states(data)]

    event_length = 9
    state_length = 9 
    num_states = 1
    test_s = []
    for episode in data:
        # Start at the beginning and keep looking at a net length of len(s)  points
        # Each time, we increment our start position by s = 9 points
        curr_state = 0
        while curr_state < num_states:
            start_idx = curr_state * state_length
            end_idx = start_idx + event_length
            datum = episode[start_idx:end_idx]
            # If its normal data without 'NA', proceed as before except we 'scale' the values to mean-0 and variance-1
            try:
                s = np.array(datum[:9].astype(np.float))
                s = scaler.transform(s)
                test_s.append(s)
            # IF there was a value error it means there was a 'NA' field somewhere. 
            except ValueError:
                # ONLY S AND S' have these 'NA' fields (I've confirmed). Therefore we go through them and replace any
                # fields that have 'NA' with the mean of the corresponding feature, and then apply the scaler.
#                s = np.array([elem if elem!='NA' else scaler.mean_[i].astype(np.float) for i, elem in enumerate(datum[:9])]).astype(np.float)
                s = np.array([elem if elem!='NA' else 0.0 for i, elem in enumerate(datum[:9])]).astype(np.float)
                s = scaler.transform(s)
                test_s.append(s)
            curr_state += 1
    return test_s