Exemple #1
0
 def _fit_(self, fit_kwargs={}, **kwargs):
     """Fit the genotype-phenotype map for epistasis.
     """
     # Fit with a linear epistasis model first, and use those values as initial guesses.
     super(NonlinearEpistasisModel, self).fit()
     # Expose the linear stuff to user and fill in important stuff.
     self.linear.epistasis.values = self.epistasis.values
     linear_phenotypes = np.dot(self.X, self.epistasis.values)
     if self.linear.log_transform:
         linear_phenotypes = 10**linear_phenotypes
     self.linear.phenotypes = linear_phenotypes
     # Construct an array of guesses, using the scale specified by user.
     guess = np.ones(self.parameters.n)
     # Add model's extra parameter guesses to input array
     for kw in kwargs:
         index = self.parameters._mapping[kw]
         guess[index] = kwargs[kw]
     # Curve fit the data using a nonlinear least squares fit
     popt, pcov = curve_fit(self.function,
                            self.statistics.linear(),
                            self.phenotypes,
                            p0=guess,
                            **fit_kwargs)
     for i in range(0, self.parameters.n):
         self.parameters._set_param(i, popt[i])
     # Score the fit
     y_pred = self.statistics.predict()
     self._score = pearson(self.phenotypes, y_pred)**2
Exemple #2
0
    def _fit_float_linear(self, guess_coeffs=None, fit_kwargs={}, **kwargs):
        """Fit using nonlinear least squares regression.

        Parameters
        ----------
        guess : array-like
            array of guesses
        fit_kwargs : dict
            specific keyword arguments for scipy's curve_fit function. This is
            not for parameters.
        **kwargs :
            guess values for `parameters` in the nonlinear function passed in as
            keyword arguments
        """
        # Construct an array of guesses, using the scale specified by user.
        guess = np.ones(self.epistasis.n + self.parameters.n)
        # Add model's extra parameter guesses to input array
        for kw in kwargs:
            index = self.parameters._mapping[kw]
            guess[self.epistasis.n + index] = kwargs[kw]
        # Create an initial guess array using fit values
        if guess_coeffs is None:
            # Fit with a linear epistasis model first, and use those values as initial guesses.
            super(NonlinearEpistasisModel, self).fit()
            # Add linear guesses to guess array
            guess[:self.epistasis.n] = self.epistasis.values
        else:
            # Add user guesses for interactions
            guess[:self.epistasis.n] = guess_coeffs

        self._wrapped_function = self._nonlinear_function_wrapper(
            self.function)
        # Curve fit the data using a nonlinear least squares fit
        popt, pcov = curve_fit(self._wrapped_function,
                               self.X,
                               self.phenotypes,
                               p0=guess,
                               **fit_kwargs)
        # Set the Interaction values
        self.epistasis.values = popt[0:len(self.epistasis.labels)]
        # Set the other parameters from nonlinear function to fit results
        for i in range(0, self.parameters.n):
            self.parameters._set_param(i, popt[len(self.epistasis.labels) + i])
        # Expose the linear stuff to user and fill in important stuff.
        self.linear.epistasis.values = self.epistasis.values
        linear_phenotypes = np.dot(self.X, self.epistasis.values)
        if self.linear.log_transform:
            linear_phenotypes = 10**linear_phenotypes
        self.linear.phenotypes = linear_phenotypes
        # Score the fit
        y_pred = self._wrapped_function(self.X, *popt)
        self._score = pearson(self.phenotypes, y_pred)**2
Exemple #3
0
    def score(self, X=None, y=None):
        """Score the model (using pearson coefficient).

        Parameters
        ----------
        X : array
            array of genotypes.

        y : array
            array of phentoypes.
        """
        ypred = self.predict(X=X)
        return pearson(y, ypred)**2
Exemple #4
0
 def score(self, X=None, y=None, **kwargs):
     """Calculate the pearson coefficient between the models predictions and
     a given y array.
     """
     return pearson(self.gpm.phenotypes, self.predict(X=X))**2
Exemple #5
0
 def score(self, X=None, y=None):
     x = self.Additive.predict(X=X)
     ypred = self.minimizer.predict(x)
     return pearson(y, ypred)**2
Exemple #6
0
 def score(self, X=None, y=None):
     y_pred = X.dot(self.coef_)
     return pearson(y, y_pred)**2
Exemple #7
0
 def score(self, X=None, y=None):
     """ Calculates the squared-pearson coefficient between the model's
     predictions and data.
     """
     y_pred = self.predict(X)
     return pearson(y, y_pred)**2