python类isreal()的实例源码

redlich_kwong.py 文件源码 项目:pythermophy 作者: j-jith 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def get_Z(self, T, p):
        #A = self.a * p / self.R**2 / T**2.5
        #B = self.b * p / self.R / T
        A = self.get_A(T, p)
        B = self.get_B(T, p)

        # Solve the cubic equation for compressibility factor z
        # Z^3 - Z^2 + (A-B-B**2)*Z - A*B = 0
        coeffs = [1, -1, A-B-B**2, -A*B]
        roots = np.roots(coeffs)

        real_roots = roots[np.isreal(roots)].real
        valid_roots = real_roots[real_roots > p*self.b/self.R/T]

        return valid_roots

    # dZ/dT at const. p
_sourcetracker.py 文件源码 项目:sourcetracker2 作者: biota 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def validate_gibbs_parameters(alpha1, alpha2, beta, restarts,
                              draws_per_restart, burnin, delay):
    '''Return `True` if params numerically acceptable. See `gibbs` for docs.'''
    real_vals = [alpha1, alpha2, beta]
    int_vals = [restarts, draws_per_restart, burnin, delay]
    # Check everything is real.
    if all(np.isreal(val) for val in real_vals + int_vals):
        # Check that integer values are some type of int.
        int_check = all(isinstance(val, (int, np.int32, np.int64)) for val in
                        int_vals)
        # All integer values must be > 0.
        pos_int = all(val > 0 for val in int_vals)
        # All real values must be non-negative.
        non_neg = all(val >= 0 for val in real_vals)
        return int_check and pos_int and non_neg and real_vals
    else:  # Failed to be all numeric values.
        False
attribute_widgets.py 文件源码 项目:pyrpl 作者: lneuhaus 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _set_widget_value(self, new_value, transform_magnitude=lambda data :
    20. * np.log10(np.abs(data) + sys.float_info.epsilon)):
        if new_value is None:
            return
        x, y = new_value
        shape = np.shape(y)
        if len(shape) > 2:
            raise ValueError("Data cannot be larger than 2 "
                             "dimensional")
        if len(shape) == 1:
            y = [y]
        self._set_real(np.isreal(y).all())
        for i, values in enumerate(y):
            self._display_curve_index(x, values, i, transform_magnitude=transform_magnitude)
        while (i + 1 < len(self.curves)):  # delete remaining curves
            i += 1
            self.curves[i].hide()
peng_robinson.py 文件源码 项目:pythermophy 作者: j-jith 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def get_Z(self, T, p):

        kappa = 0.37464 + 1.54226*self.acentric - 0.26992*self.acentric**2

        Tr = T/self.T_crit
        alpha = (1 + kappa*(1 - Tr**0.5))**2

        A = alpha * self.a * p / self.R**2 / T**2
        B = self.b * p / self.R / T

        # Solve the cubic equation for compressibility factor z
        coeffs = [1, -(1-B), A-2*B-3*B**2, -(A*B-B**2-B**3)]
        #print(coeffs)
        roots = np.roots(coeffs)

        real_roots = roots[np.isreal(roots)].real
        valid_roots = real_roots[real_roots > p*self.b/self.R/T]

        return valid_roots
Math.py 文件源码 项目:pyberny 作者: azag0 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def fit_cubic(y0, y1, g0, g1):
    """Fit cubic polynomial to function values and derivatives at x = 0, 1.

    Returns position and function value of minimum if fit succeeds. Fit does
    not succeeds if

    1. polynomial doesn't have extrema or
    2. maximum is from (0,1) or
    3. maximum is closer to 0.5 than minimum
    """
    a = 2*(y0-y1)+g0+g1
    b = -3*(y0-y1)-2*g0-g1
    p = np.array([a, b, g0, y0])
    r = np.roots(np.polyder(p))
    if not np.isreal(r).all():
        return None, None
    r = sorted(x.real for x in r)
    if p[0] > 0:
        maxim, minim = r
    else:
        minim, maxim = r
    if 0 < maxim < 1 and abs(minim-0.5) > abs(maxim-0.5):
        return None, None
    return minim, np.polyval(p, minim)
algorithms.py 文件源码 项目:chebpy 作者: chebpy 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def vals2coeffs2(vals):
    """Map function values at Chebyshev points of 2nd kind to
    first-kind Chebyshev polynomial coefficients"""
    n = vals.size
    if n <= 1:
        coeffs = vals
        return coeffs
    tmp = np.append( vals[::-1], vals[1:-1] )
    if np.isreal(vals).all():
        coeffs = ifft(tmp)
        coeffs = np.real(coeffs)
    elif np.isreal( 1j*vals ).all():
        coeffs = ifft(np.imag(tmp))
        coeffs = 1j * np.real(coeffs)
    else:
        coeffs = ifft(tmp)
    coeffs = coeffs[:n]
    coeffs[1:n-1] = 2*coeffs[1:n-1]
    return coeffs
algorithms.py 文件源码 项目:chebpy 作者: chebpy 项目源码 文件源码 阅读 47 收藏 0 点赞 0 评论 0
def coeffs2vals2(coeffs):
    """Map first-kind Chebyshev polynomial coefficients to
    function values at Chebyshev points of 2nd kind"""
    n = coeffs.size
    if n <= 1:
        vals = coeffs
        return vals
    coeffs = coeffs.copy()
    coeffs[1:n-1] = .5 * coeffs[1:n-1]
    tmp = np.append( coeffs, coeffs[n-2:0:-1] )
    if np.isreal(coeffs).all():
        vals = fft(tmp)
        vals = np.real(vals)
    elif np.isreal(1j*coeffs).all():
        vals = fft(np.imag(tmp))
        vals = 1j * np.real(vals)
    else:
        vals = fft(tmp)
    vals = vals[n-1::-1]
    return vals
table_formatters.py 文件源码 项目:PyBloqs 作者: manahl 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _modify_dataframe(self, df):
        """Add row to dataframe, containing numbers aggregated with self.operator."""
        if self.total_columns == []:
            columns = df.columns
        else:
            columns = self.total_columns
        if self.operator is not OP_NONE:
            last_row = self.operator(df[df.applymap(np.isreal)])
            last_row = last_row.fillna(0.)
            last_row[~last_row.index.isin(columns)] = ''
        else:
            last_row = pd.Series('', index=df.columns)
        last_row.name = self.row_name
        # Appending kills index name, save now and restore after appending
        index_name = df.index.name
        df = df.append(last_row)
        df.index.name = index_name
        return df
_write.py 文件源码 项目:aiida-vasp 作者: DropD 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def _incarify(value):
    """Format value of any compatible type into the string forat appropriate for INCAR files"""
    result = None
    if isinstance(value, (str, unicode)):
        result = value
    elif not np.isscalar(value):
        value_array = np.array(value)
        shape = value_array.shape
        dim = len(shape)
        if dim == 1:
            result = ' '.join([_incarify(i) for i in value])
        elif dim == 2:
            result = '\n'.join([_incarify(i) for i in value])
        elif dim > 2:
            raise TypeError('you are trying to input a more ' +
                            'than 2-dimensional array to VASP.' +
                            'Not sure what to do...')
    elif isinstance(value, bool):
        result = '.True.' if value else '.False.'
    elif np.isreal(value):
        result = '{}'.format(value)
    return result
distribution.py 文件源码 项目:treetime 作者: neherlab 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def __call__(self, x):

        if isinstance(x, Iterable):
            valid_idxs = (x > self._xmin-TINY_NUMBER) & (x < self._xmax+TINY_NUMBER)
            res = np.ones_like (x, dtype=float) * (BIG_NUMBER+self.peak_val)
            tmp_x = np.copy(x[valid_idxs])
            tmp_x[tmp_x<self._xmin+TINY_NUMBER] = self._xmin+TINY_NUMBER
            tmp_x[tmp_x>self._xmax-TINY_NUMBER] = self._xmax-TINY_NUMBER
            res[valid_idxs] = self._peak_val + self._func(tmp_x)
            return res

        elif np.isreal(x):
            if x < self._xmin or x > self._xmax:
                return BIG_NUMBER+self.peak_val
            # x is within interpolation range
            elif self._delta == True:
                return self._peak_val
            else:
                return self._peak_val + self._func(x)
        else:
            raise TypeError("Wrong type: should be float or array")
mklmm.py 文件源码 项目:MKLMM 作者: omerwe 项目源码 文件源码 阅读 40 收藏 0 点赞 0 评论 0
def getPosteriorMeanAndVar(self, diagKTestTest, KtrainTest, post, intercept=0):
        L = post['L']
        if (np.size(L) == 0): raise Exception('L is an empty array') #possible to compute it here
        Lchol = np.all((np.all(np.tril(L, -1)==0, axis=0) & (np.diag(L)>0)) & np.isreal(np.diag(L)))
        ns = diagKTestTest.shape[0]
        nperbatch = 5000
        nact = 0

        #allocate mem
        fmu = np.zeros(ns)  #column vector (of length ns) of predictive latent means
        fs2 = np.zeros(ns)  #column vector (of length ns) of predictive latent variances
        while (nact<(ns-1)):
            id = np.arange(nact, np.minimum(nact+nperbatch, ns))
            kss = diagKTestTest[id]     
            Ks = KtrainTest[:, id]
            if (len(post['alpha'].shape) == 1):
                try: Fmu = intercept[id] + Ks.T.dot(post['alpha'])
                except: Fmu = intercept + Ks.T.dot(post['alpha'])
                fmu[id] = Fmu
            else:
                try: Fmu = intercept[id][:, np.newaxis] + Ks.T.dot(post['alpha'])
                except: Fmu = intercept + Ks.T.dot(post['alpha'])
                fmu[id] = Fmu.mean(axis=1)
            if Lchol:
                V = la.solve_triangular(L, Ks*np.tile(post['sW'], (id.shape[0], 1)).T, trans=1, check_finite=False, overwrite_b=True)
                fs2[id] = kss - np.sum(V**2, axis=0)                       #predictive variances                        
            else:
                fs2[id] = kss + np.sum(Ks * (L.dot(Ks)), axis=0)           #predictive variances
            fs2[id] = np.maximum(fs2[id],0)  #remove numerical noise i.e. negative variances        
            nact = id[-1]    #set counter to index of last processed data point

        return fmu, fs2
filtering.py 文件源码 项目:mitre 作者: gerberlab 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def discard_where_data_missing(data, field):
    """ Discard subjects where data for a particular field is missing. 

    Assumes the missing data value is NaN. Non-numeric values
    are never considered missing, even the empty string.

    """
    keep_indices = []
    for i, value in enumerate(data.subject_data[field].values):
        if not (np.isreal(value) and np.isnan(value)):
            keep_indices.append(i)
    return select_subjects(data, keep_indices)
attribute_widgets.py 文件源码 项目:pyrpl 作者: lneuhaus 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _set_widget_value(self, new_value):
        x, y, name = self.get_xy_data(new_value)
        if x is not None:
            if not np.isreal(y).all():
                self.curve.setData(x, self._magnitude(y))
                self.curve_phase.setData(x, self._phase(y))
                self.plot_item_phase.show()
                self.plot_item.setTitle(name + " - Magnitude (dB)")
            else:
                self.curve.setData(x, np.real(y))
                self.plot_item_phase.hide()
                self.plot_item.setTitle(name)
cubic_parent.py 文件源码 项目:pythermophy 作者: j-jith 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def get_Z(self, T, p):
        """
        Returns the compressibility factor of a real gas.

        :param T: temperature (K)
        :type T: float
        :param p: pressure (Pa)
        :type p: float

        :return: compressibility factor
        :rtype: float
        """
        # a = self.get_a(T)
        # coeffs = [
        #         1,
        #         (-self.R*T - self.b*p + self.c*p + self.d*p)/(self.R*T),
        #         (-self.R*T*self.d*p + a*p - self.b*self.d*p**2 + self.c*self.d*p**2 + self.e*p**2)/(self.R**2*T**2),
        #         (-self.R*T*self.e*p**2 - a*self.b*p**2 + a*self.c*p**2 - self.b*self.e*p**3 + self.c*self.e*p**3)/(self.R**3*T**3)
        # ]
        coeffs = [1, self.get_A(T, p), self.get_B(T, p), self.get_C(T, p)]
        #print(coeffs)

        roots = np.roots(coeffs)
        real_roots = roots[np.isreal(roots)].real

        if len(real_roots) == 1:
            real_roots = real_roots[0]

        return real_roots

    # Partial derivative of Z wrt T at constant p
soave_redlich_kwong.py 文件源码 项目:pythermophy 作者: j-jith 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def get_Z(self, T, p):

        A = self.get_A(T, p)
        B = self.get_B(T, p)

        # Solve the cubic equation for compressibility factor z
        coeffs = [1, -1, A-B-B**2, -A*B]
        roots = np.roots(coeffs)

        real_roots = roots[np.isreal(roots)].real
        valid_roots = real_roots[real_roots > p*self.b/self.R/T]

        return valid_roots
test_mcmc.py 文件源码 项目:ESPEI 作者: PhasesResearchLab 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def test_lnprob_calculates_probability_for_success(datasets_db):
    """lnprob() successfully calculates the probability for equilibrium """
    datasets_db.insert(zpf_json)
    res = lnprob([10], comps=['CU','MG', 'VA'], dbf=dbf,
                 phases=['LIQUID', 'FCC_A1', 'HCP_A3', 'LAVES_C15', 'CUMG2'],
                 datasets=datasets_db, symbols_to_fit=['VV0001'], phase_models=None, scheduler=None)
    assert np.isreal(res)
    assert np.isclose(res, -5740.542839073727)
Math.py 文件源码 项目:pyberny 作者: azag0 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def fit_quartic(y0, y1, g0, g1):
    """Fit constrained quartic polynomial to function values and erivatives at x = 0,1.

    Returns position and function value of minimum or None if fit fails or has
    a maximum. Quartic polynomial is constrained such that it's 2nd derivative
    is zero at just one point. This ensures that it has just one local
    extremum.  No such or two such quartic polynomials always exist. From the
    two, the one with lower minimum is chosen.
    """
    def g(y0, y1, g0, g1, c):
        a = c+3*(y0-y1)+2*g0+g1
        b = -2*c-4*(y0-y1)-3*g0-g1
        return np.array([a, b, c, g0, y0])

    def quart_min(p):
        r = np.roots(np.polyder(p))
        is_real = np.isreal(r)
        if is_real.sum() == 1:
            minim = r[is_real][0].real
        else:
            minim = r[(r == max(-abs(r))) | r == -max(-abs(r))][0].real
        return minim, np.polyval(p, minim)

    D = -(g0+g1)**2-2*g0*g1+6*(y1-y0)*(g0+g1)-6*(y1-y0)**2  # discriminant of d^2y/dx^2=0
    if D < 1e-11:
        return None, None
    else:
        m = -5*g0-g1-6*y0+6*y1
        p1 = g(y0, y1, g0, g1, .5*(m+np.sqrt(2*D)))
        p2 = g(y0, y1, g0, g1, .5*(m-np.sqrt(2*D)))
        if p1[0] < 0 and p2[0] < 0:
            return None, None
        [minim1, minval1] = quart_min(p1)
        [minim2, minval2] = quart_min(p2)
        if minval1 < minval2:
            return minim1, minval1
        else:
            return minim2, minval2
delayseq.py 文件源码 项目:piradar 作者: scivision 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def delayseq(x, delay_sec:float, fs:int):
    """
    x: input 1-D signal
    delay_sec: amount to shift signal [seconds]
    fs: sampling frequency [Hz]

    xs: time-shifted signal
    """

    assert x.ndim == 1, 'only 1-D signals for now'

    delay_samples = delay_sec*fs
    delay_int = round(delay_samples)

    nfft = nextpow2(x.size+delay_int)

    fbins = 2*pi*ifftshift((arange(nfft)-nfft//2))/nfft

    X = fft(x,nfft)
    Xs = ifft(X*exp(-1j*delay_samples*fbins))

    if isreal(x[0]):
        Xs = Xs.real

    xs = zeros_like(x)
    xs[delay_int:] = Xs[delay_int:x.size]

    return xs
algorithms.py 文件源码 项目:chebpy 作者: chebpy 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def coeffmult(fc, gc):
    """Coefficient-Space multiplication of equal-length first-kind
    Chebyshev series."""
    Fc = np.append( 2.*fc[:1], (fc[1:], fc[:0:-1]) )
    Gc = np.append( 2.*gc[:1], (gc[1:], gc[:0:-1]) )
    ak = ifft( fft(Fc) * fft(Gc) )
    ak = np.append( ak[:1], ak[1:] + ak[:0:-1] ) * .25
    ak = ak[:fc.size]
    inputcfs = np.append(fc, gc)
    out = np.real(ak) if np.isreal(inputcfs).all() else ak
    return out
preview.py 文件源码 项目:dynamic-walking 作者: stephane-caron 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def get_omega2(self, step=None):
        if isreal(self.omega2):
            return self.omega2
        return self.__get('omega2', step)
preview.py 文件源码 项目:dynamic-walking 作者: stephane-caron 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def discretize(self, nb_steps):
        """
        Discretize the remaining preview with a uniform timestep.

        Parameter
        ---------
        nb_steps : integer
            Number of discretization time steps.

        Returns
        -------
        X : array, shape=(N + 1, 9)
            Series of discretized states (com, comd, zmp).
        contacts : list of Contacts
            List of N + 1 contacts corresponding to each step k from 0 to N.
        """
        assert isreal(self.omega2), "Discretization only works on FIP previews"
        X = []
        contacts = []
        input_step = self.cur_step
        com, comd = self.P[0], self.V[0]
        output_timestep = self.rem_duration / nb_steps
        rem_dT = self.rem_dT
        omega2 = self.omega2
        for _ in xrange(nb_steps):
            X.append(hstack([com, comd, self.get_zmp(input_step)]))
            contacts.append(self.get_contact(input_step))
            time_to_fill = output_timestep
            while time_to_fill > 1e-10:
                if rem_dT < 1e-10:
                    input_step += 1
                    rem_dT = self.get_dT(input_step)
                zmp = self.get_zmp(input_step)
                dt = min(time_to_fill, rem_dT)
                com, comd = integrate_fip(com, comd, zmp, dt, omega2)
                time_to_fill -= dt
                rem_dT -= dt
        cp = com + comd / sqrt(omega2) + gravity / omega2
        X.append(hstack([com, comd, cp]))
        contacts.append(contacts[-1])
        return array(X), contacts
line_search.py 文件源码 项目:smt 作者: SMTorg 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _compute_minimum(self, a1, p1, dp1, a2, p2, dp2):
        cubic_mtx = np.zeros((4, 4))
        cubic_mtx[0, :] = [1., a1, a1 ** 2, a1 ** 3]
        cubic_mtx[1, :] = [1., a2, a2 ** 2, a2 ** 3]
        cubic_mtx[2, :] = [0., 1., 2 * a1, 3 * a1 ** 2]
        cubic_mtx[3, :] = [0., 1., 2 * a2, 3 * a2 ** 2]
        c0, c1, c2, c3 = np.linalg.solve(cubic_mtx, [p1, p2, dp1, dp2])

        d0 = c1
        d1 = 2 * c2
        d2 = 3 * c3
        r1, r2 = np.roots([d2, d1, d0])

        a = None
        p = max(p1, p2)
        if (a1 <= r1 <= a2 or a2 <= r1 <= a1) and np.isreal(r1):
            px = self._phi(r1)
            if px < p:
                a = r1
                p = px
                dp = self._dphi(r1)
        if (a1 <= r2 <= a2 or a2 <= r2 <= a1) and np.isreal(r2):
            px = self._phi(r2)
            if px < p:
                a = r2
                p = px
                dp = self._dphi(r2)

        return a, p, dp
math.py 文件源码 项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda 作者: SignalMedia 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def is_psd(m):
    eigvals = linalg.eigvals(m)
    return np.isreal(eigvals).all() and (eigvals >= 0).all()
table_formatters.py 文件源码 项目:PyBloqs 作者: manahl 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def _modify_dataframe(self, df):
        """Add row to dataframe, containing numbers aggregated with self.operator."""
        if self.total_rows == []:
            rows = df.index.tolist()
        else:
            rows = self.total_rows
        if self.operator is not OP_NONE:
            new_column = self.operator(df[df.applymap(np.isreal)], axis=1)
            new_column = new_column.fillna(0.)
            new_column[~new_column.index.isin(rows)] = ''
        else:
            new_column = pd.Series('', index=df.index)
        df_mod = df.copy()
        df_mod[self.column_name] = new_column
        return df_mod
table_formatters.py 文件源码 项目:PyBloqs 作者: manahl 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def _modify_dataframe(self, df):
        """Create single index dataframe inserting grouping rows for higher levels."""

        if self.total_columns == []:
            columns = df.columns
        else:
            columns = self.total_columns

        flat_row_list = []
        n_ix_levels = len(df.index.levels)

        # For each row compare index tuple to previous one and see if it changed on any level.
        previous_tuple = [''] * n_ix_levels
        for level_k, index_tuple in enumerate(df.index):
            for level_i, sub_index in enumerate(index_tuple):
                if index_tuple[:level_i + 1] != previous_tuple[:level_i + 1]:
                    if level_i == n_ix_levels - 1:
                        # If we are on lowest level, add entire row to flat_df
                        data_rows = df.iloc[[level_k], :]
                    else:
                        # If we are on higher level, add row filled with operator on lower level data
                        if self.operator is OP_NONE:
                            # For operator None fill row with empty string for each column
                            data_rows = pd.DataFrame('', columns=df.columns, index=[sub_index])
                        else:
                            df_subset = df.loc[index_tuple[:level_i + 1]]
                            data_rows = self.operator(df_subset[df_subset.applymap(np.isreal)]).to_frame().T
                            data_rows = data_rows.fillna(0.)
                            data_rows.loc[:, ~data_rows.columns.isin(columns)] = ''
                    n_rows = len(data_rows)
                    data_rows.index = [sub_index] * n_rows
                    data_rows.loc[:, ORG_ROW_NAMES] = pd.Series([index_tuple[:level_i + 1]], index=data_rows.index)
                    flat_row_list.append(data_rows)
                    # Need to address index_level with i instead of sub_index, because sub_index can repeat many times.
                    self.index_level += [level_i] * n_rows
            previous_tuple = index_tuple
        flat_df = pd.concat(flat_row_list)
        flat_df.index.name = ''
        return flat_df
test_eigenfunctions.py 文件源码 项目:pyinduct 作者: pyinduct 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_it(self):
        self.assertTrue(np.isreal(ef.return_real_part(1)))
        self.assertTrue(np.isreal(ef.return_real_part(1+0j)))
        self.assertTrue(np.isreal(ef.return_real_part(1+1e-20j)))
        self.assertRaises(TypeError, ef.return_real_part, None)
        self.assertRaises(TypeError, ef.return_real_part, (1, 2., 2+2j))
        self.assertRaises(TypeError, ef.return_real_part, [None, 2., 2+2j])
        self.assertRaises(ValueError, ef.return_real_part, [1, 2., 2+2j])
        self.assertRaises(ValueError, ef.return_real_part, 1+1e-10j)
        self.assertRaises(ValueError, ef.return_real_part, 1j)
util.py 文件源码 项目:pyhiro 作者: wanweiwei07 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def is_empty(self):
        if len(self.data) == 0: 
            return True
        for v in self.data.values():
            if is_sequence(v):
                if len(v) > 0:
                    return False
            else:
                if bool(np.isreal(v)):
                    return False
        return True
titanic.py 文件源码 项目:v_examples 作者: peterhurford 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def impute(col):
  if col.apply(numpy.isreal).all(axis = 0):
    value = numpy.nanmedian(col)
  else:
    value = col.mode().iloc[0]
  return col.fillna(value)
example.py 文件源码 项目:v_examples 作者: peterhurford 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def impute(col):
  if col.apply(numpy.isreal).all(axis = 0):
    value = numpy.nanmedian(col)
  else:
    value = col.mode().iloc[0]
  return col.fillna(value)
predict.py 文件源码 项目:porn_sieve 作者: PornSieve 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def fmt_numerical(self, data):
        """
        There are two categories of data Porn Sieve gathers:
          1.Tag data, represented as a binary, mostly zero array of numbers
          2. Data which is continuous, such as duration, average review, etc.

        For the tags, I can just use CountVectorizer out-of-the-box, but for
        the other data, we need to put it all together in a list on our own.
        """
        nums = []
        # sorted to ensure the data is always in the same order
        for k in sorted(data.keys()):
            if k in ["feedback", "img"]:
                pass

            elif type(data[k]) == list:
                pass

            elif data[k] == None:
                nums.append(0)

            elif (k == "scrape_date") and (type(data[k]) != float):
                stamp = datetime.strptime(data[k], "%Y-%m-%d %H:%M:%S.%f")
                epoch = datetime.utcfromtimestamp(0)
                nums.append((stamp - epoch).total_seconds())

            elif np.isreal(data[k]):
                nums.append(data[k])
        return nums


问题


面经


文章

微信
公众号

扫码关注公众号