Python numpy.nan_to_num() Examples
The following are 30 code examples for showing how to use numpy.nan_to_num(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: tensortrade Author: tensortrade-org File: observation_history.py License: Apache License 2.0 | 6 votes |
def observe(self) -> np.array: """Returns the rows to be observed by the agent.""" rows = self.rows.copy() if len(rows) < self.window_size: size = self.window_size - len(rows) padding = np.zeros((size, rows.shape[1])) padding = pd.DataFrame(padding, columns=self.rows.columns) rows = pd.concat([padding, rows], ignore_index=True, sort=False) if isinstance(rows, pd.DataFrame): rows = rows.fillna(0, axis=1) rows = rows.values rows = np.nan_to_num(rows) return rows
Example 2
Project: deepnlp Author: rockingdingo File: textrank.py License: MIT License | 6 votes |
def tfIdf(dtm): nDoc = dtm.shape[0] nTerm = dtm.shape[1] dtmNorm = dtm/dtm.sum(axis=1, keepdims=True) # Normalize tf to unit weight, tf/line word count dtmNorm = np.nan_to_num(dtmNorm) tfIdfMat = np.zeros((nDoc,nTerm)) for j in range(nTerm): tfVect = dtmNorm[:, j] nExist = np.sum(tfVect > 0.0) # if tfVect is 0.0, word is not in current doc idf = 0.0 # int32 if (nExist > 0): idf = np.log(nDoc/nExist)/np.log(2) # log2() else: idf = 0.0 tfIdfMat[:,j] = tfVect * idf return tfIdfMat
Example 3
Project: deepnlp Author: rockingdingo File: textrank.py License: MIT License | 6 votes |
def pagerank(nDim, adjMat, d, K): ''' Args: d: damping factor, K: iteration Number ''' P = np.ones((nDim, 1)) * (1/nDim) # normalize adjacency Matrix B = adjMat/adjMat.sum(axis=1, keepdims=True) B = np.nan_to_num(B) U = np.ones((nDim, nDim)) * (1/nDim) M = d * B + (1-d) * U for i in range(K): P = np.dot(M.T, P) score = P.tolist() return P
Example 4
Project: metal Author: HazyResearch File: analysis.py License: Apache License 2.0 | 6 votes |
def lf_overlaps(L, normalize_by_coverage=False): """Return the **fraction of items each LF labels that are also labeled by at least one other LF.** Note that the maximum possible overlap fraction for an LF is the LF's coverage, unless `normalize_by_coverage=True`, in which case it is 1. Args: L: an n x m scipy.sparse matrix where L_{i,j} is the label given by the jth LF to the ith candidate normalize_by_coverage: Normalize by coverage of the LF, so that it returns the percent of LF labels that have overlaps. """ overlaps = (L != 0).T @ _overlapped_data_points(L) / L.shape[0] if normalize_by_coverage: overlaps /= lf_coverages(L) return np.nan_to_num(overlaps)
Example 5
Project: metal Author: HazyResearch File: analysis.py License: Apache License 2.0 | 6 votes |
def lf_conflicts(L, normalize_by_overlaps=False): """Return the **fraction of items each LF labels that are also given a different (non-abstain) label by at least one other LF.** Note that the maximum possible conflict fraction for an LF is the LF's overlaps fraction, unless `normalize_by_overlaps=True`, in which case it is 1. Args: L: an n x m scipy.sparse matrix where L_{i,j} is the label given by the jth LF to the ith candidate normalize_by_overlaps: Normalize by overlaps of the LF, so that it returns the percent of LF overlaps that have conflicts. """ conflicts = (L != 0).T @ _conflicted_data_points(L) / L.shape[0] if normalize_by_overlaps: conflicts /= lf_overlaps(L) return np.nan_to_num(conflicts)
Example 6
Project: cwcf Author: jaromiru File: env.py License: MIT License | 6 votes |
def __init__(self, data, hpc_p, costs): self.data_x = data.iloc[:, 0:-1].astype('float32').values self.data_n = np.isnan(self.data_x) self.data_x = np.nan_to_num(self.data_x) self.data_y = data.iloc[:, -1].astype('int32').values self.data_len = len(data) self.hpc_p = hpc_p.values self.costs = costs.values self.mask = np.zeros( (config.AGENTS, config.FEATURE_DIM), dtype=np.float32 ) self.x = np.zeros( (config.AGENTS, config.FEATURE_DIM), dtype=np.float32 ) self.y = np.zeros( config.AGENTS, dtype=np.int64 ) self.p = np.zeros( config.AGENTS, dtype=np.int32 ) self.n = np.zeros( (config.AGENTS, config.FEATURE_DIM), dtype=np.bool )
Example 7
Project: vnpy_crypto Author: birforce File: tools.py License: MIT License | 6 votes |
def nan_dot(A, B): """ Returns np.dot(left_matrix, right_matrix) with the convention that nan * 0 = 0 and nan * x = nan if x != 0. Parameters ---------- A, B : np.ndarrays """ # Find out who should be nan due to nan * nonzero should_be_nan_1 = np.dot(np.isnan(A), (B != 0)) should_be_nan_2 = np.dot((A != 0), np.isnan(B)) should_be_nan = should_be_nan_1 + should_be_nan_2 # Multiply after setting all nan to 0 # This is what happens if there were no nan * nonzero conflicts C = np.dot(np.nan_to_num(A), np.nan_to_num(B)) C[should_be_nan] = np.nan return C
Example 8
Project: gluon-cv Author: dmlc File: voc_detection.py License: Apache License 2.0 | 6 votes |
def _average_precision(self, rec, prec): """ calculate average precision, override the default one, special 11-point metric Params: ---------- rec : numpy.array cumulated recall prec : numpy.array cumulated precision Returns: ---------- ap as float """ if rec is None or prec is None: return np.nan ap = 0. for t in np.arange(0., 1.1, 0.1): if np.sum(rec >= t) == 0: p = 0 else: p = np.max(np.nan_to_num(prec)[rec >= t]) ap += p / 11. return ap
Example 9
Project: FAE Author: salan668 File: Normalizer.py License: GNU General Public License v3.0 | 6 votes |
def Transform(self, data_container, store_folder='', store_key=''): if data_container.IsEmpty(): return data_container new_data_container = deepcopy(data_container) array = new_data_container.GetArray() array -= self._interception array /= self._slop array = np.nan_to_num(array) new_data_container.SetArray(array) new_data_container.UpdateFrameByData() if store_folder: assert(len(store_key) > 0) self.SaveNormalDataContainer(data_container, store_folder, store_key) return new_data_container
Example 10
Project: TCFPN-ISBA Author: Zephyr-D File: metrics.py License: MIT License | 6 votes |
def macro_accuracy(P, Y, n_classes, bg_class=None, return_all=False, **kwargs): def macro_(P, Y, n_classes=None, bg_class=None, return_all=False): conf_matrix = sm.confusion_matrix(Y, P, labels=np.arange(n_classes)) conf_matrix = conf_matrix / (conf_matrix.sum(0)[:, None] + 1e-5) conf_matrix = np.nan_to_num(conf_matrix) diag = conf_matrix.diagonal() * 100. # Remove background score if bg_class is not None: diag = np.array([diag[i] for i in range(n_classes) if i != bg_class]) macro = diag.mean() if return_all: return macro, diag else: return macro if type(P) == list: out = [macro_(P[i], Y[i], n_classes=n_classes, bg_class=bg_class, return_all=return_all) for i in range(len(P))] if return_all: return (np.mean([o[0] for o in out]), np.mean([o[1] for o in out], 0)) else: return np.mean(out) else: return macro_(P, Y, n_classes=n_classes, bg_class=bg_class, return_all=return_all)
Example 11
Project: Modeling-Cloth Author: the3dadvantage File: SurfaceFollow.py License: MIT License | 5 votes |
def rotate_around_axis(coords, Q, origin='empty'): '''Uses standard quaternion to rotate a vector. Q requires a 4-dimensional vector. coords is the 3d location of the point. coords can also be an N x 3 array of vectors. Happens to work with Q as a tuple or a np array shape 4''' if origin == 'empty': vcV = np.cross(Q[1:], coords) RV = np.nan_to_num(coords + vcV * (2*Q[0]) + np.cross(Q[1:],vcV)*2) else: coords -= origin vcV = np.cross(Q[1:],coords) RV = (np.nan_to_num(coords + vcV * (2*Q[0]) + np.cross(Q[1:],vcV)*2)) + origin coords += origin #undo in-place offset return RV
Example 12
Project: Modeling-Cloth Author: the3dadvantage File: SurfaceFollow.py License: MIT License | 5 votes |
def barycentric_generate(hits, tris): '''Create scalars to be used by points and triangles''' # where the hit lands on the two tri vecs tv = tris[:, 1] - tris[:, 0] hv = hits - tris[:, 0] d1a = np.einsum('ij, ij->i', hv, tv) d1b = np.einsum('ij, ij->i', tv, tv) scalar1 = np.nan_to_num(d1a / d1b) t2v = tris[:, 2] - tris[:, 0] d2a = np.einsum('ij, ij->i', hv, t2v) d2b = np.einsum('ij, ij->i', t2v, t2v) scalar2 = np.nan_to_num(d2a / d2b) # closest point on edge segment between the two points created above cp1 = tv * np.expand_dims(scalar1, axis=1) cp2 = t2v * np.expand_dims(scalar2, axis=1) cpvec = cp2 - cp1 cp1_at = tris[:,0] + cp1 hcp = hits - cp1_at # this is cp3 above. Not sure what's it's for yet dhcp = np.einsum('ij, ij->i', hcp, cpvec) d3b = np.einsum('ij, ij->i', cpvec, cpvec) hcp_scalar = np.nan_to_num(dhcp / d3b) hcp_vec = cpvec * np.expand_dims(hcp_scalar, axis=1) # base of tri on edge between first two points d3 = np.einsum('ij, ij->i', -cp1, cpvec) scalar3 = np.nan_to_num(d3 / d3b) base_cp_vec = cpvec * np.expand_dims(scalar3, axis=1) base_on_span = cp1_at + base_cp_vec # Where the point occurs on the edge between the base of the triangle # and the cpoe of the base of the triangle on the cpvec base_vec = base_on_span - tris[:,0] dba = np.einsum('ij, ij->i', hv, base_vec) dbb = np.einsum('ij, ij->i', base_vec, base_vec) scalar_final = np.nan_to_num(dba / dbb) p_on_bv = base_vec * np.expand_dims(scalar_final, axis=1) perp = (p_on_bv) - (cp1 + base_cp_vec) return scalar1, scalar2, hcp_scalar, scalar3, scalar_final
Example 13
Project: Modeling-Cloth Author: the3dadvantage File: SurfaceFollow.py License: MIT License | 5 votes |
def project_points(points, tri_coords): '''Using this to get the points off the surface Takes the average length of two vecs off triangles and applies it to the length of the normals. This way the normal scales with the mesh and with changes to the individual triangle vectors''' t0 = tri_coords[:, 0] t1 = tri_coords[:, 1] t2 = tri_coords[:, 2] tv1 = t1 - t0 tv2 = t2 - t0 cross = np.cross(tv1, tv2) # get the average length of the two vectors and apply it to the cross product sq = np.sqrt(np.einsum('ij,ij->i', cross, cross)) x1 = np.einsum('ij,ij->i', tv1, tv1) x2 = np.einsum('ij,ij->i', tv2, tv2) av_root = np.sqrt((x1 + x2) / 2) cr_root = (cross / np.expand_dims(sq, axis=1)) * np.expand_dims(av_root, axis=1) v1 = points - t0 v1_dots = np.einsum('ij,ij->i', cr_root, v1) n_dots = np.einsum('ij,ij->i', cr_root, cr_root) scale = np.nan_to_num(v1_dots / n_dots) offset = cr_root * np.expand_dims(scale, axis=1) drop = points - offset # The drop is used by the barycentric generator as points in the triangles return drop, scale
Example 14
Project: ZIFA Author: epierson9 File: ZIFA.py License: MIT License | 5 votes |
def decayCoefObjectiveFn(x, Y, EX2): """ Computes the objective function for terms involving lambda in the M-step. Checked. Input: x: value of lambda Y: the matrix of observed values EX2: the matrix of values of EX2 estimated in the E-step. Returns: obj: value of objective function grad: gradient """ with warnings.catch_warnings(): warnings.simplefilter("ignore") y_squared = Y ** 2 Y_is_zero = np.abs(Y) < 1e-6 exp_Y_squared = np.exp(-x * y_squared) log_exp_Y = np.nan_to_num(np.log(1 - exp_Y_squared)) exp_ratio = np.nan_to_num(exp_Y_squared / (1 - exp_Y_squared)) obj = sum(sum(Y_is_zero * (-EX2 * x) + (1 - Y_is_zero) * log_exp_Y)) grad = sum(sum(Y_is_zero * (-EX2) + (1 - Y_is_zero) * y_squared * exp_ratio)) if (type(obj) is not np.float64) or (type(grad) is not np.float64): raise Exception("Unexpected behavior in optimizing decay coefficient lambda. Please contact emmap1@cs.stanford.edu.") if type(obj) is np.float64: obj = -np.array([obj]) if type(grad) is np.float64: grad = -np.array([grad]) return obj, grad
Example 15
Project: lightnn Author: l11x0m7 File: losses.py License: Apache License 2.0 | 5 votes |
def forward(y_hat, y): y_hat = _cutoff(y_hat) y = _cutoff(y) return -np.mean(np.sum(np.nan_to_num(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat)), axis=1))
Example 16
Project: lightnn Author: l11x0m7 File: losses.py License: Apache License 2.0 | 5 votes |
def forward(y_hat, y): assert (np.abs(np.sum(y_hat, axis=1) - 1.) < cutoff).all() assert (np.abs(np.sum(y, axis=1) - 1.) < cutoff).all() y_hat = _cutoff(y_hat) y = _cutoff(y) return -np.mean(np.sum(np.nan_to_num(y * np.log(y_hat)), axis=1))
Example 17
Project: lightnn Author: l11x0m7 File: losses.py License: Apache License 2.0 | 5 votes |
def forward(y_hat, y): assert (np.abs(np.sum(y_hat, axis=1) - 1.) < cutoff).all() assert (np.abs(np.sum(y, axis=1) - 1.) < cutoff).all() y_hat = _cutoff(y_hat) y = _cutoff(y) return -np.mean(np.sum(np.nan_to_num(y * np.power((1 - y_hat), gama) * np.log(y_hat)), axis=1))
Example 18
Project: chainerrl Author: chainer File: acer.py License: MIT License | 5 votes |
def compute_importance(pi, mu, x): return np.nan_to_num(float(pi.prob(x).array) / float(mu.prob(x).array))
Example 19
Project: chainerrl Author: chainer File: acer.py License: MIT License | 5 votes |
def compute_full_importance(pi, mu): pimu = pi.all_prob.array / mu.all_prob.array # NaN occurs when inf/inf or 0/0 pimu[np.isnan(pimu)] = 1 pimu = np.nan_to_num(pimu) return pimu
Example 20
Project: tartarus Author: sergiooramas File: threshold.py License: MIT License | 5 votes |
def get_threshold(model_id): trained_models = pd.read_csv(common.DEFAULT_TRAINED_MODELS_FILE, sep='\t') model_config = trained_models[trained_models["model_id"] == model_id] if model_config.empty: raise ValueError("Can't find the model %s in %s" % (model_id, common.DEFAULT_TRAINED_MODELS_FILE)) model_config = model_config.to_dict(orient="list") model_settings=eval(model_config['dataset_settings'][0]) Y_test = np.load(common.DATASETS_DIR+'/item_factors_test_%s_%s_%s.npy' % (model_settings['fact'],model_settings['dim'],model_settings['dataset'])) Y_pred = np.load(common.FACTORS_DIR+'/factors_%s.npy' % model_id) good_scores = Y_pred[Y_test==1] th = good_scores.mean() std = good_scores.std() print 'Mean th',th print 'Std',std p, r, thresholds = precision_recall_curve(Y_test.flatten(), Y_pred.flatten()) f = np.nan_to_num((2 * (p*r) / (p+r)) * (p>r)) print f max_f = np.argmax(f) fth = thresholds[max_f] print f[max_f],p[max_f],r[max_f] print 'F th %.2f' % fth plt.plot(r, p, label='Precision-recall curve of class {0}') plt.xlim([0.0, 1.0]) plt.ylim([0.0, 1.05]) plt.xlabel('Recall') plt.ylabel('Precision') plt.title('Extension of Precision-Recall curve to multi-class') plt.savefig("pr_curve.png")
Example 21
Project: simnibs Author: simnibs File: mesh_io.py License: GNU General Public License v3.0 | 5 votes |
def gaussian_curvature(self): ''' Calculates the Gaussian curvature at each node Returns --------- nd: NodeData NodeData structure with gaussian curvature for each surface node Rerefereces ------------- https://computergraphics.stackexchange.com/a/1721 ''' nodes_areas = self.nodes_areas()[:] triangles_angles = np.deg2rad( np.nan_to_num(self.triangle_angles()[self.elm.triangles]) ) triangle_nodes = self.elm[self.elm.triangles] - 1 triangle_nodes = triangle_nodes[:, :3] node_angles = np.bincount( triangle_nodes.reshape(-1), triangles_angles.reshape(-1), self.nodes.nr ) nodes_areas[nodes_areas == 0] = .1 gaussian_curvature = (2*np.pi - node_angles)/nodes_areas return NodeData(gaussian_curvature, 'gaussian_curvature')
Example 22
Project: tangent Author: google File: utils.py License: Apache License 2.0 | 5 votes |
def _assert_allclose(a, b, tol=1e-5): if isinstance(a, (tuple, list)) and isinstance(b, (tuple, list)): for ia, ib in zip(a, b): _assert_allclose(ia, ib, tol) else: try: a = np.nan_to_num(a) b = np.nan_to_num(b) assert np.allclose(a, b, tol), ('Expected: %s\nGot: %s' % (b, a)) except TypeError: raise TypeError('Could not compare values %s and %s' % (a, b))
Example 23
Project: mdentropy Author: msmbuilder File: base.py License: MIT License | 5 votes |
def partial_transform(self, traj, shuffle=0, verbose=False): """Transform a single mdtraj.Trajectory into an array of metric scores. Parameters ---------- traj : mdtraj.Trajectory Trajectory to transform shuffle : int Number of shuffle iterations (default: 0) verbose : bool Whether to display performance Returns ------- result : np.ndarray Scoring matrix """ self._before_exec(traj) result = self._floored_exec() correction = np.zeros_like(result) for i in range(shuffle): with Timing(i, verbose=verbose): self._shuffle() correction += self._floored_exec() return floor_threshold(result - np.nan_to_num(correction / shuffle))
Example 24
Project: OpenNE Author: thunlp File: lap.py License: MIT License | 5 votes |
def getLap(self): degree_mat = np.diagflat(np.sum(self.adj_mat, axis=1)) deg_trans = np.diagflat(np.reciprocal(np.sqrt(np.sum(self.adj_mat, axis=1)))) deg_trans = np.nan_to_num(deg_trans) L = degree_mat-self.adj_mat # eye = np.eye(self.node_size) norm_lap_mat = np.matmul(np.matmul(deg_trans, L), deg_trans) return norm_lap_mat
Example 25
Project: astropy-healpix Author: astropy File: test_healpy.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_vec2ang(vectors, lonlat, ndim): vectors = np.broadcast_to(vectors, (2,) * ndim + (3,)) theta1, phi1 = hp_compat.vec2ang(vectors, lonlat=lonlat) theta2, phi2 = hp.vec2ang(vectors, lonlat=lonlat) # Healpy sometimes returns NaNs for phi (somewhat incorrectly) phi2 = np.nan_to_num(phi2) assert_allclose(theta1, theta1, atol=1e-10) assert_allclose(phi1, phi2, atol=1e-10)
Example 26
Project: highdimensional-decision-boundary-plot Author: tmadl File: uci_loader.py License: MIT License | 5 votes |
def getdataset(datasetname, onehot_encode_strings=True): # load dataset = fetch_openml(datasetname) # get X and y X = dshape(dataset.data) try: target = dshape(dataset.target) except: print("WARNING: No target found. Taking last column of data matrix as target") target = X[:, -1] X = X[:, :-1] if ( len(target.shape) > 1 and target.shape[1] > X.shape[1] ): # some mldata sets are mixed up... X = target target = dshape(dataset.data) if len(X.shape) == 1 or X.shape[1] <= 1: for k in dataset.keys(): if k != "data" and k != "target" and len(dataset[k]) == X.shape[1]: X = np.hstack((X, dshape(dataset[k]))) # one-hot for categorical values if onehot_encode_strings: cat_ft = [ i for i in range(X.shape[1]) if "str" in str(type(unpack(X[0, i]))) or "unicode" in str(type(unpack(X[0, i]))) ] if len(cat_ft): for i in cat_ft: X[:, i] = tonumeric(X[:, i]) X = OneHotEncoder(categorical_features=cat_ft).fit_transform(X) # if sparse, make dense try: X = X.toarray() except: pass # convert y to monotonically increasing ints y = tonumeric(target).astype(int) return np.nan_to_num(X.astype(float)), y
Example 27
Project: freqtrade-strategies Author: freqtrade File: CombinedBinHAndCluc.py License: GNU General Public License v3.0 | 5 votes |
def bollinger_bands(stock_price, window_size, num_of_std): rolling_mean = stock_price.rolling(window=window_size).mean() rolling_std = stock_price.rolling(window=window_size).std() lower_band = rolling_mean - (rolling_std * num_of_std) return np.nan_to_num(rolling_mean), np.nan_to_num(lower_band)
Example 28
Project: freqtrade-strategies Author: freqtrade File: BinHV45.py License: GNU General Public License v3.0 | 5 votes |
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: mid, lower = bollinger_bands(dataframe['close'], window_size=40, num_of_std=2) dataframe['mid'] = np.nan_to_num(mid) dataframe['lower'] = np.nan_to_num(lower) dataframe['bbdelta'] = (dataframe['mid'] - dataframe['lower']).abs() dataframe['pricedelta'] = (dataframe['open'] - dataframe['close']).abs() dataframe['closedelta'] = (dataframe['close'] - dataframe['close'].shift()).abs() dataframe['tail'] = (dataframe['close'] - dataframe['low']).abs() return dataframe
Example 29
Project: deepchem Author: deepchem File: transformers.py License: MIT License | 5 votes |
def transform_array(self, X, y, w): """Transform the data in a set of (X, y, w) arrays.""" if self.transform_X: X = np.nan_to_num((X - self.X_min) / (self.X_max - self.X_min)) elif self.transform_y: y = np.nan_to_num((y - self.y_min) / (self.y_max - self.y_min)) return (X, y, w)
Example 30
Project: deepchem Author: deepchem File: transformers.py License: MIT License | 5 votes |
def transform_array(self, X, y, w): """Transform the data in a set of (X, y, w) arrays.""" if self.transform_X: if not hasattr(self, 'move_mean') or self.move_mean: X = np.nan_to_num((X - self.X_means) / self.X_stds) else: X = np.nan_to_num(X / self.X_stds) if self.transform_y: if not hasattr(self, 'move_mean') or self.move_mean: y = np.nan_to_num((y - self.y_means) / self.y_stds) else: y = np.nan_to_num(y / self.y_stds) return (X, y, w)