fit unstructured data
488268
def polyfit2d(x, y, z, order=3 #bounds=None
              ):
    '''
     
    '''
    ncols = (order + 1)**2
    G = np.zeros((x.size, ncols))
    ij = itertools.product(list(range(order+1)), list(range(order+1)))
    for k, (i,j) in enumerate(ij):
        G[:,k] = x**i * y**j
    m = np.linalg.lstsq(G, z)[0]
    return m
591945
def to_fits(self):
        

        max_length_coord = 1
        coord_x = []
        coord_y = []
        shapes = []
        radius = []
        rotangle_deg = []
        components = []

        reg_reverse_mapping = {value: key for key, value in
                               reg_mapping['FITS_REGION'].items()}
        reg_reverse_mapping['rectangle'] = 'ROTBOX'
        reg_reverse_mapping['circleannulus'] = 'ANNULUS'
        reg_reverse_mapping['ellipseannulus'] = 'ELLIPTANNULUS'

        for num, shape in enumerate(self):
            shapes.append(reg_reverse_mapping[shape.region_type])
            if shape.region_type == 'polygon':
                max_length_coord = max(len(shape.coord)/2, max_length_coord)
                coord = [x.value for x in shape.coord]
                coord_x.append(coord[::2])
                coord_y.append(coord[1::2])
                radius.append(0)
                rotangle_deg.append(0)
            else:
                coord_x.append(shape.coord[0].value)
                coord_y.append(shape.coord[1].value)
                if shape.region_type in ['circle', 'circleannulus', 'point']:
                    radius.append([float(val) for val in shape.coord[2:]])
                    rotangle_deg.append(0)
                else:
                    radius.append([float(x) for x in shape.coord[2:-1]])
                    rotangle_deg.append(shape.coord[-1].to('deg').value)

            tag = shape.meta.get('tag', '')
            if tag.isdigit():
                components.append(int(tag))
            else:
                components.append(num + 1)

        # padding every value with zeros at the end to make sure that all values
        # in the column have same length.
        for i in range(len(self)):
            if np.isscalar(coord_x[i]):
                coord_x[i] = np.array([coord_x[i]])
            if np.isscalar(coord_y[i]):
                coord_y[i] = np.array([coord_y[i]])
            if np.isscalar(radius[i]):
                radius[i] = np.array([radius[i]])

            coord_x[i] = np.pad(coord_x[i], (0, int(max_length_coord - len(coord_x[i]))),
                                'constant', constant_values=(0, 0))
            coord_y[i] = np.pad(coord_y[i], (0, int(max_length_coord - len(coord_y[i]))),
                                'constant', constant_values=(0, 0))
            radius[i] = np.pad(radius[i], (0, 4 - len(radius[i])), 'constant',
                               constant_values=(0, 0))

        table = Table([coord_x, coord_y, shapes, radius, rotangle_deg, components],
                       names=('X', 'Y', 'SHAPE', 'R', 'ROTANG', 'COMPONENT'))
        table['X'].unit = 'pix'
        table['Y'].unit = 'pix'
        table['R'].unit = 'pix'
        table['ROTANG'].unit = 'deg'

        return table
999608
public void fitLabelledDocuments(JavaRDD<LabelledDocument> documentsRdd) {

        validateConfiguration();

        broadcastEnvironment(new JavaSparkContext(documentsRdd.context()));

        JavaRDD<Sequence<VocabWord>> sequenceRDD =
                        documentsRdd.map(new DocumentSequenceConvertFunction(configurationBroadcast));

        super.fitSequences(sequenceRDD);
    }
439764
function fitWidth(s, len)
{
    s = s.trim();
    if(s.length <= len) return [s];

    var result = [];
    while(s.length > len)
    {
        var i = len
        for(; s[i] != ' ' && i >= 0; i--) 
            /* empty loop */ ;

        if(i == -1)
        {
            for(i = len + 1; s[i] != ' ' && i < s.length; i++) 
                /* empty loop */ ;
            
            if(i == s.length)
            {
                result.push(s);
                return result;
            }
        }
        result.push(s.substr(0, i));
        s = s.substr(i).trimLeft();
    }
    result.push(s);
    return result;
}
667216
def fetch_rrlyrae_fitdata(**kwargs):
    
    save_loc = _get_download_or_cache('table3.dat.gz', **kwargs)

    dtype = [('id', 'i'), ('RA', 'f'), ('DEC', 'f'), ('rExt', 'f'),
             ('d', 'f'), ('RGC', 'f'),
             ('u', 'f'), ('g', 'f'), ('r', 'f'),
             ('i', 'f'), ('z', 'f'), ('V', 'f'),
             ('ugmin', 'f'), ('ugmin_err', 'f'),
             ('grmin', 'f'), ('grmin_err', 'f')]

    return np.loadtxt(save_loc, dtype=dtype)
882213
def fit(self, X, y=None):
        
        for columns, transformer in self.mapping:
            if transformer is not None:
                transformer.fit(self._get_columns(X, columns))
        return self
112257
def fit_cluster(data, assignments, k, P_init, R_init, means):
    
    for c in range(k):
        if data[:,assignments==c].shape[1] == 0:
            _, assignments = kmeans_pp(data, k)
    genes, cells = data.shape
    nb_gene_indices = np.array([True for i in range(genes)])
    for c in range(k):
        c_data = data[:,assignments==c]
        nb_gene_indices = nb_gene_indices & find_nb_genes(c_data)
    for c in range(k):
        c_data = data[:,assignments==c]
        nb_genes = c_data[nb_gene_indices,:]
        poisson_genes = c_data[~nb_gene_indices, :]
        P_init[nb_gene_indices, c], R_init[nb_gene_indices, c] = nb_fit(nb_genes)
        means[~nb_gene_indices, c] = poisson_genes.mean(1)
    return nb_gene_indices
758850
def fit(self, x, y):
        
        verbose = self.verbose
        # Create temporary files
        data_file = tempfile.NamedTemporaryFile("w+b", delete=False)
        label_file = tempfile.NamedTemporaryFile("w+b", delete=False)

        start = time.time()
        raw_rules = categorical2pysbrl_data(x, y, data_file.name, label_file.name, supp=self.min_support,
                                            zmin=self.min_rule_len, zmax=self.max_rule_len, method=self.fim_method)
        if verbose > 1:
            print("Info: sbrl data files saved to %s and %s temporarily" % (data_file.name, label_file.name))
        data_file.close()
        label_file.close()
        cat_time = time.time() - start
        if verbose:
            print("Info: time for rule mining: %.4fs" % cat_time)
        n_labels = int(np.max(y)) + 1
        start = time.time()
        _model = train_sbrl(data_file.name, label_file.name, self.lambda_, eta=self.eta,
                            max_iters=self.iters, n_chains=self.n_chains, seed=self.seed,
                            alpha=self.alpha, verbose=verbose)
        train_time = time.time() - start
        if verbose:
            print("Info: training time: %.4fs" % train_time)

        # update model parameters
        self._n_classes = n_labels
        self._n_features = x.shape[1]

        # convert the raw parameters to rules
        self.from_raw(_model[0], _model[1], raw_rules)
        self._supports = self.compute_support(x, y)

        # Close the temp files
        os.unlink(data_file.name)
        os.unlink(label_file.name)
1085220
def smart_fitting_predicate(
    page_width,
    ribbon_frac,
    min_nesting_level,
    max_width,
    triplestack
):
    
    chars_left = max_width

    while chars_left >= 0:
        if not triplestack:
            return True

        indent, mode, doc = triplestack.pop()

        if doc is NIL:
            continue
        elif isinstance(doc, str):
            chars_left -= len(doc)
        elif isinstance(doc, Concat):
            # Recursive call in Strictly Pretty: docs within Concat
            # are processed in order, with keeping the current
            # indentation and mode.
            # We want the leftmost element at the top of the stack,
            # so we append the concatenated documents in reverse order.
            triplestack.extend(
                (indent, mode, doc)
                for doc in reversed(doc.docs)
            )
        elif isinstance(doc, Annotated):
            triplestack.append((indent, mode, doc.doc))
        elif isinstance(doc, Fill):
            # Same as the Concat case.
            triplestack.extend(
                (indent, mode, doc)
                for doc in reversed(doc.docs)
            )
        elif isinstance(doc, Nest):
            # Nest is a combination of an indent and a doc.
            # Increase indentation, then add the doc for processing.
            triplestack.append((indent + doc.indent, mode, doc.doc))
        elif isinstance(doc, AlwaysBreak):
            return False
        elif doc is HARDLINE:
            # In the fast algorithm, when we see a line,
            # we return True. Here, as long as the minimum indentation
            # level is satisfied, we continue processing the next line.
            # This causes the longer runtime.
            if indent > min_nesting_level:
                chars_left = page_width - indent
            else:
                return True
        elif isinstance(doc, FlatChoice):
            if mode is FLAT_MODE:
                triplestack.append((indent, mode, doc.when_flat))
            elif mode is BREAK_MODE:
                triplestack.append((indent, mode, doc.when_broken))
            else:
                raise ValueError
        elif isinstance(doc, Group):
            # Group just changes the mode.
            triplestack.append((indent, FLAT_MODE, doc.doc))
        elif isinstance(doc, Contextual):
            ribbon_width = max(0, min(page_width, round(ribbon_frac * page_width)))

            evaluated_doc = doc.fn(
                indent=indent,
                column=max_width - chars_left,
                page_width=page_width,
                ribbon_width=ribbon_width,
            )
            normalized = normalize_doc(evaluated_doc)
            triplestack.append((indent, mode, normalized))
        elif isinstance(doc, SAnnotationPush):
            continue
        elif isinstance(doc, SAnnotationPop):
            continue
        else:
            raise ValueError((indent, mode, doc))

    return False
855343
def fit(self, X, y=None):
        
        self.columns_ = X.columns
        columns = self.columns
        if columns is None:
            columns = X.select_dtypes(include=["category"]).columns
        else:
            for column in columns:
                assert is_categorical_dtype(X[column]), "Must be categorical"

        self.categorical_columns_ = columns
        self.non_categorical_columns_ = X.columns.drop(self.categorical_columns_)

        if _HAS_CTD:
            self.dtypes_ = {col: X[col].dtype for col in self.categorical_columns_}
        else:
            self.dtypes_ = {
                col: (X[col].cat.categories, X[col].cat.ordered)
                for col in self.categorical_columns_
            }

        return self
651371
def fit(self, col):
        
        dates = self.safe_datetime_cast(col)
        self.default_val = dates.groupby(dates).count().index[0].timestamp() * 1e9
1363649
def create_from_fits(cls, fitsfile, norm_type='flux'):
        
        tsmap = WcsNDMap.read(fitsfile)

        tab_e = Table.read(fitsfile, 'EBOUNDS')
        tab_s = Table.read(fitsfile, 'SCANDATA')
        tab_f = Table.read(fitsfile, 'FITDATA')

        tab_e = convert_sed_cols(tab_e)
        tab_s = convert_sed_cols(tab_s)
        tab_f = convert_sed_cols(tab_f)

        emin = np.array(tab_e['e_min'])
        emax = np.array(tab_e['e_max'])
        try:
            if str(tab_e['e_min'].unit) == 'keV':
                emin /= 1000.
        except:
            pass
        try:
            if str(tab_e['e_max'].unit) == 'keV':
                emax /= 1000.
        except:
            pass

        nebins = len(tab_e)
        npred = tab_e['ref_npred']

        ndim = len(tsmap.data.shape)

        if ndim == 2:
            cube_shape = (tsmap.data.shape[0],
                          tsmap.data.shape[1], nebins)
        elif ndim == 1:
            cube_shape = (tsmap.data.shape[0], nebins)
        else:
            raise RuntimeError("Counts map has dimension %i" % (ndim))

        refSpec = ReferenceSpec.create_from_table(tab_e)
        nll_vals = -np.array(tab_s["dloglike_scan"])
        norm_vals = np.array(tab_s["norm_scan"])

        axis = MapAxis.from_edges(np.concatenate((emin, emax[-1:])),
                                  interp='log')
        geom_3d = tsmap.geom.to_cube([axis])
        tscube = WcsNDMap(geom_3d,
                          np.rollaxis(tab_s["ts"].reshape(cube_shape), 2, 0))

        ncube = WcsNDMap(geom_3d,
                         np.rollaxis(tab_s["norm"].reshape(cube_shape), 2, 0))
        nmap = WcsNDMap(tsmap.geom,
                        tab_f['fit_norm'].reshape(tsmap.data.shape))

        ref_colname = 'ref_%s' % norm_type
        norm_vals *= tab_e[ref_colname][np.newaxis, :, np.newaxis]

        return cls(tsmap, nmap, tscube, ncube,
                   norm_vals, nll_vals, refSpec,
                   norm_type)
444075
def resize_to_fit(cols, rows = nil)
      rows ||= cols
      change_geometry(Geometry.new(cols, rows)) do |ncols, nrows|
        resize(ncols, nrows)
      end
    end
175818
def fit(self, data):
        
        jmodel = callMLlibFunc("fitPCA", self.k, data)
        return PCAModel(jmodel)
1063033
def fit(self, X, y=None):
        
        self.columns_ = X.columns
        columns = self.columns
        if columns is None:
            columns = X.select_dtypes(include=["category"]).columns
        else:
            for column in columns:
                assert is_categorical_dtype(X[column]), "Must be categorical"

        self.categorical_columns_ = columns
        self.non_categorical_columns_ = X.columns.drop(self.categorical_columns_)

        if _HAS_CTD:
            self.dtypes_ = {col: X[col].dtype for col in self.categorical_columns_}
        else:
            self.dtypes_ = {
                col: (X[col].cat.categories, X[col].cat.ordered)
                for col in self.categorical_columns_
            }

        left = len(self.non_categorical_columns_)
        self.categorical_blocks_ = {}
        for col in self.categorical_columns_:
            right = left + len(X[col].cat.categories)
            if self.drop_first:
                right -= 1
            self.categorical_blocks_[col], left = slice(left, right), right

        if isinstance(X, pd.DataFrame):
            sample = X.iloc[:1]
        else:
            sample = X._meta_nonempty

        self.transformed_columns_ = pd.get_dummies(
            sample, drop_first=self.drop_first
        ).columns
        return self
314916
def fit(self, text, epochs=EPOCHS):
        
        logger.debug("Extracting features and classifications.")
        Phi = []
        Y = []
        for (L, P, R, gold, _) in Detector.candidates(text):
            Phi.append(self.extract_one(L, P, R))
            Y.append(gold)
        self.classifier.fit(Y, Phi, epochs)
        logger.debug("Fitting complete.")
1023330
def fitNullTraitByTrait(self, verbose=False, cache=False, out_dir='./cache', fname=None, rewrite=False):
        
        read_from_file = False
        if cache:
            assert fname is not None, 'MultiTraitSetTest:: specify fname'
            if not os.path.exists(out_dir): os.makedirs(out_dir)
            out_file = os.path.join(out_dir,fname)
            read_from_file = os.path.exists(out_file) and not rewrite

        RV = {}
        if read_from_file:
            f = h5py.File(out_file,'r')
            for p in range(self.P):
                trait_id = self.traitID[p]
                g = f[trait_id]
                RV[trait_id] = {}
                for key in list(g.keys()):
                    RV[trait_id][key] = g[key][:]
            f.close()
            self.nullST=RV
        else:
            """ create stSet and fit null column by column returns all info """
            if self.stSet is None:
                y = sp.zeros((self.N,1))
                self.stSet = MTSet(Y=y, S_R=self.S_R, U_R=self.U_R, F=self.F)
            RV = {}
            for p in range(self.P):
                trait_id = self.traitID[p]
                self.stSet.Y = self.Y[:,p:p+1]
                RV[trait_id] = self.stSet.fitNull()
            self.nullST = RV
            if cache:
                f = h5py.File(out_file,'w')
                smartDumpDictHdf5(RV,f)
                f.close()
        return RV