Python sklearn.utils.extmath.safe_sparse_dot() Examples
The following are 26
code examples of sklearn.utils.extmath.safe_sparse_dot().
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 also want to check out all available functions/classes of the module
sklearn.utils.extmath
, or try the search function
.
Example #1
Source File: test_sparse.py From twitter-stock-recommendation with MIT License | 6 votes |
def test_sparse_decision_function(): #Test decision_function #Sanity check, test that decision_function implemented in python #returns the same as the one in libsvm # multi class: svc = svm.SVC(kernel='linear', C=0.1, decision_function_shape='ovo') clf = svc.fit(iris.data, iris.target) dec = safe_sparse_dot(iris.data, clf.coef_.T) + clf.intercept_ assert_array_almost_equal(dec, clf.decision_function(iris.data)) # binary: clf.fit(X, Y) dec = np.dot(X, clf.coef_.T) + clf.intercept_ prediction = clf.predict(X) assert_array_almost_equal(dec.ravel(), clf.decision_function(X)) assert_array_almost_equal( prediction, clf.classes_[(clf.decision_function(X) > 0).astype(np.int).ravel()]) expected = np.array([-1., -0.66, -1., 0.66, 1., 1.]) assert_array_almost_equal(clf.decision_function(X), expected, 2)
Example #2
Source File: test_sparse.py From Mastering-Elasticsearch-7.0 with MIT License | 6 votes |
def test_sparse_decision_function(): # Test decision_function # Sanity check, test that decision_function implemented in python # returns the same as the one in libsvm # multi class: svc = svm.SVC(kernel='linear', C=0.1, decision_function_shape='ovo') clf = svc.fit(iris.data, iris.target) dec = safe_sparse_dot(iris.data, clf.coef_.T) + clf.intercept_ assert_array_almost_equal(dec, clf.decision_function(iris.data)) # binary: clf.fit(X, Y) dec = np.dot(X, clf.coef_.T) + clf.intercept_ prediction = clf.predict(X) assert_array_almost_equal(dec.ravel(), clf.decision_function(X)) assert_array_almost_equal( prediction, clf.classes_[(clf.decision_function(X) > 0).astype(np.int).ravel()]) expected = np.array([-1., -0.66, -1., 0.66, 1., 1.]) assert_array_almost_equal(clf.decision_function(X), expected, 2)
Example #3
Source File: random_layer.py From SVM-CNN with Apache License 2.0 | 6 votes |
def _compute_input_activations(self, X): """Compute input activations given X""" n_samples = X.shape[0] mlp_acts = np.zeros((n_samples, self.n_hidden)) if (self._use_mlp_input): b = self.components_['biases'] w = self.components_['weights'] mlp_acts = self.alpha * (safe_sparse_dot(X, w) + b) rbf_acts = np.zeros((n_samples, self.n_hidden)) if (self._use_rbf_input): radii = self.components_['radii'] centers = self.components_['centers'] scale = self.rbf_width * (1.0 - self.alpha) rbf_acts = scale * cdist(X, centers)/radii self.input_activations_ = mlp_acts + rbf_acts
Example #4
Source File: online_sketch.py From flurs with MIT License | 6 votes |
def score(self, user, candidates, context): # i_mat is (n_item_context, n_item) for all possible items # extract only target items i_mat = self.i_mat[:, candidates] n_target = len(candidates) # u_mat will be (n_user_context, n_item) for the target user u_vec = np.concatenate((user.feature, context)) u_vec = np.array([u_vec]).T u_mat = sp.csr_matrix(np.repeat(u_vec, n_target, axis=1)) # stack them into (p, n_item) matrix Y = sp.vstack((u_mat, i_mat)) Y = self.proj.reduce(Y) Y = sp.csr_matrix(preprocessing.normalize(Y, norm='l2', axis=0)) X = np.identity(self.k) - np.dot(self.U_r, self.U_r.T) A = safe_sparse_dot(X, Y, dense_output=True) return ln.norm(A, axis=0, ord=2)
Example #5
Source File: pyowl.py From pyowl with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _fit_owl_fista(X, y, w, loss, max_iter=500, max_linesearch=20, eta=2.0, tol=1e-3, verbose=0): # least squares loss def sfunc(coef, grad=False): y_scores = safe_sparse_dot(X, coef) if grad: obj, lp = loss(y, y_scores, return_derivative=True) grad = safe_sparse_dot(X.T, lp) return obj, grad else: return loss(y, y_scores) def nsfunc(coef, L): return prox_owl(coef, w / L) coef = np.zeros(X.shape[1]) return fista(sfunc, nsfunc, coef, max_iter, max_linesearch, eta, tol, verbose)
Example #6
Source File: factorization_machine.py From flurs with MIT License | 5 votes |
def score(self, user, candidates, context): # i_mat is (n_item_context, n_item) for all possible items # extract only target items i_mat = self.i_mat[:, candidates] n_target = len(candidates) u_vec = user.encode(dim=self.n_user, index=self.use_index, feature=True, vertical=True) u_vec = np.concatenate((u_vec, np.array([context]).T)) u_mat = sp.csr_matrix(np.repeat(u_vec, n_target, axis=1)) mat = sp.vstack((u_mat, i_mat)) # Matrix A and B should be dense (numpy array; rather than scipy CSR matrix) because V is dense. V = sp.csr_matrix(self.V) A = safe_sparse_dot(V.T, mat) A.data[:] = A.data ** 2 sq_mat = mat.copy() sq_mat.data[:] = sq_mat.data ** 2 sq_V = V.copy() sq_V.data[:] = sq_V.data ** 2 B = safe_sparse_dot(sq_V.T, sq_mat) interaction = (A - B).sum(axis=0) interaction /= 2. # (1, n_item); numpy matrix form pred = self.w0 + safe_sparse_dot(self.w, mat, dense_output=True) + interaction return np.abs(1. - np.ravel(pred))
Example #7
Source File: test_sparse.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_svc_with_custom_kernel(): kfunc = lambda x, y: safe_sparse_dot(x, y.T) clf_lin = svm.SVC(kernel='linear').fit(X_sp, Y) clf_mylin = svm.SVC(kernel=kfunc).fit(X_sp, Y) assert_array_equal(clf_lin.predict(X_sp), clf_mylin.predict(X_sp))
Example #8
Source File: factorization_machine.py From polylearn with BSD 2-Clause "Simplified" License | 5 votes |
def _get_output(self, X): y_pred = _poly_predict(X, self.P_[0, :, :], self.lams_, kernel='anova', degree=self.degree) if self.fit_linear: y_pred += safe_sparse_dot(X, self.w_) if self.fit_lower == 'explicit' and self.degree == 3: # degree cannot currently be > 3 y_pred += _poly_predict(X, self.P_[1, :, :], self.lams_, kernel='anova', degree=2) return y_pred
Example #9
Source File: kernels.py From polylearn with BSD 2-Clause "Simplified" License | 5 votes |
def _D(X, P, degree=2): """The "replacement" part of the homogeneous polynomial kernel. D[i, j] = sum_k [(X_ik * P_jk) ** degree] """ return safe_sparse_dot(safe_power(X, degree), P.T ** degree)
Example #10
Source File: elm.py From SVM-CNN with Apache License 2.0 | 5 votes |
def _get_predictions(self): """get predictions using internal least squares/supplied regressor""" if (self.regressor is None): preds = safe_sparse_dot(self.hidden_activations_, self.coefs_) else: preds = self.regressor.predict(self.hidden_activations_) return preds
Example #11
Source File: elm.py From SVM-CNN with Apache License 2.0 | 5 votes |
def _fit_regression(self, y): """ fit regression using pseudo-inverse or supplied regressor """ if (self.regressor is None): self.coefs_ = safe_sparse_dot(pinv2(self.hidden_activations_), y) else: self.regressor.fit(self.hidden_activations_, y) self.fitted_ = True
Example #12
Source File: community_weighting.py From reveal-graph-embedding with Apache License 2.0 | 5 votes |
def chi2_contingency_matrix(X_train, y_train): X = X_train.copy() X.data = np.ones_like(X.data) X = check_array(X, accept_sparse='csr') if np.any((X.data if issparse(X) else X) < 0): raise ValueError("Input X must be non-negative.") Y = LabelBinarizer().fit_transform(y_train) if Y.shape[1] == 1: Y = np.append(1 - Y, Y, axis=1) observed = safe_sparse_dot(Y.T, X) # n_classes * n_features # feature_count = check_array(X.sum(axis=0)) # class_prob = check_array(Y.mean(axis=0)) feature_count = X.sum(axis=0).reshape(1, -1) class_prob = Y.mean(axis=0).reshape(1, -1) expected = np.dot(class_prob.T, feature_count) observed = np.asarray(observed, dtype=np.float64) k = len(observed) # Reuse observed for chi-squared statistics contingency_matrix = observed contingency_matrix -= expected contingency_matrix **= 2 expected[expected == 0.0] = 1.0 contingency_matrix /= expected # weights = contingency_matrix.max(axis=0) return contingency_matrix
Example #13
Source File: vectorize.py From marseille with BSD 3-Clause "New" or "Revised" License | 5 votes |
def fit_transform(self, X, y=None, **fit_params): self.vect_ = TfidfVectorizer(vocabulary=self.embed_vocab, analyzer=_lower_words_getter, norm='l1', use_idf=False) return safe_sparse_dot(self.vect_.fit_transform(X), self.embeds)
Example #14
Source File: vectorize.py From marseille with BSD 3-Clause "New" or "Revised" License | 5 votes |
def transform(self, X, y=None): return safe_sparse_dot(self.vect_.transform(X), self.embeds)
Example #15
Source File: struct_models.py From marseille with BSD 3-Clause "New" or "Revised" License | 5 votes |
def joint_feature(self, x, y): if isinstance(y, DocLabel): Y_prop, Y_link, compat, second_order = self._marg_rounded(x, y) else: Y_prop, Y_link, compat, second_order = self._marg_fractional(x, y) prop_acc = safe_sparse_dot(Y_prop.T, x.X_prop) # node_cls * node_feats link_acc = safe_sparse_dot(Y_link.T, x.X_link) # link_cls * link_feats f_sec_ord = [] if len(second_order): second_order = second_order.reshape(-1, len(x.second_order)) if self.coparents: f_sec_ord.append(safe_sparse_dot(second_order[0], x.X_sec_ord)) second_order = second_order[1:] if self.grandparents: f_sec_ord.append(safe_sparse_dot(second_order[0], x.X_sec_ord)) second_order = second_order[1:] if self.siblings: f_sec_ord.append(safe_sparse_dot(second_order[0], x.X_sec_ord)) elif self.n_second_order_factors_: # document has no second order factors so the joint feature # must be filled with zeros manually f_sec_ord = [np.zeros(self.n_second_order_features_) for _ in range(self.n_second_order_factors_)] jf = np.concatenate([prop_acc.ravel(), link_acc.ravel(), compat.ravel()] + f_sec_ord) return jf # basically reversing the joint feature
Example #16
Source File: projection.py From flurs with MIT License | 5 votes |
def reduce(self, Y): return safe_sparse_dot(self.W1, Y) * safe_sparse_dot(self.W2, Y) / np.sqrt(self.k)
Example #17
Source File: projection.py From flurs with MIT License | 5 votes |
def reduce(self, Y): return safe_sparse_dot(self.R, Y)
Example #18
Source File: projection.py From flurs with MIT License | 5 votes |
def reduce(self, Y): return safe_sparse_dot(self.E, Y)
Example #19
Source File: RBM.py From char-rbm with MIT License | 5 votes |
def _fit(self, v_pos): """Inner fit for one mini-batch. Adjust the parameters to maximize the likelihood of v using Stochastic Maximum Likelihood (SML). v_pos : array-like, shape (n_samples, n_features) The data to use for training. """ h_pos = self._mean_hiddens(v_pos) # TODO: Worth trying with visible probabilities rather than binary states. # PG: it is common to use p_i instead of sampling a binary value'... 'it reduces # sampling noise this allowing faster learning. There is some evidence that it leads # to slightly worse density models' # I'm confounded by the fact that we seem to get more effective models WITHOUT # softmax visible units. The only explanation I can think of is that it's like # a pseudo-version of using visible probabilities. Without softmax, v_neg # can have multiple 1s per one-hot vector, which maybe somehow accelerates learning? # Need to think about this some more. v_neg = self._sample_visibles(self.h_samples_) h_neg = self._mean_hiddens(v_neg) lr = float(self.learning_rate) / v_pos.shape[0] update = safe_sparse_dot(v_pos.T, h_pos, dense_output=True).T update -= np.dot(h_neg.T, v_neg) / self.fantasy_to_batch # L2 weight penalty update -= self.components_ * self.weight_cost self.components_ += lr * update self.intercept_hidden_ += lr * (h_pos.sum(axis=0) - h_neg.sum(axis=0)/self.fantasy_to_batch) self.intercept_visible_ += lr * (np.asarray( v_pos.sum(axis=0)).squeeze() - v_neg.sum(axis=0)/self.fantasy_to_batch) h_neg[self.rng_.uniform(size=h_neg.shape) < h_neg] = 1.0 # sample binomial self.h_samples_ = np.floor(h_neg, h_neg)
Example #20
Source File: RBM.py From char-rbm with MIT License | 5 votes |
def _free_energy(self, v): """Computes the free energy F(v) = - log sum_h exp(-E(v,h)). v : array-like, shape (n_samples, n_features) Values of the visible layer. Returns ------- free_energy : array-like, shape (n_samples,) The value of the free energy. """ return (- safe_sparse_dot(v, self.intercept_visible_) - np.logaddexp(0, safe_sparse_dot(v, self.components_.T) + self.intercept_hidden_).sum(axis=1))
Example #21
Source File: RBM.py From char-rbm with MIT License | 5 votes |
def _mean_hiddens(self, v, temperature=1.0): """Computes the probabilities P(h=1|v). v : array-like, shape (n_samples, n_features) Values of the visible layer. Returns ------- h : array-like, shape (n_samples, n_components) Corresponding mean field values for the hidden layer. """ p = safe_sparse_dot(v, self.components_.T/temperature) p += self.intercept_hidden_/(min(1.0, temperature) if BIASED_PRIOR else temperature) return expit(p, out=p)
Example #22
Source File: transforms.py From ristretto with GNU General Public License v3.0 | 5 votes |
def sparse_johnson_lindenstrauss(A, l, density=None, axis=1, random_state=None): """ Given an m x n matrix A, and an integer l, this scheme computes an m x l orthonormal matrix Q whose range approximates the range of A Parameters ---------- density : sparse matrix density """ random_state = check_random_state(random_state) A = np.asarray(A) if A.ndim != 2: raise ValueError('A must be a 2D array, not %dD' % A.ndim) if axis not in (0, 1): raise ValueError('If supplied, axis must be in (0, 1)') if density is None: density = log(A.shape[0]) / A.shape[0] # construct sparse sketch Omega = _sketches.sparse_random_map(A, l, axis, density, random_state) # project A onto Omega if axis == 0: return safe_sparse_dot(Omega.T, A) return safe_sparse_dot(A, Omega)
Example #23
Source File: nonnegative.py From civisml-extensions with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _rescale_data(X, y, sample_weight): """Rescale data so as to support sample_weight""" n_samples = X.shape[0] sample_weight = sample_weight * np.ones(n_samples) sample_weight = np.sqrt(sample_weight) sw_matrix = sparse.dia_matrix((sample_weight, 0), shape=(n_samples, n_samples)) X = safe_sparse_dot(sw_matrix, X) y = safe_sparse_dot(sw_matrix, y) return X, y
Example #24
Source File: test_sparse.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_svc_with_custom_kernel(): def kfunc(x, y): return safe_sparse_dot(x, y.T) clf_lin = svm.SVC(kernel='linear').fit(X_sp, Y) clf_mylin = svm.SVC(gamma='scale', kernel=kfunc).fit(X_sp, Y) assert_array_equal(clf_lin.predict(X_sp), clf_mylin.predict(X_sp))
Example #25
Source File: pyowl.py From pyowl with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _decision_function(self, X): return safe_sparse_dot(X, self.coef_)
Example #26
Source File: struct_models.py From marseille with BSD 3-Clause "New" or "Revised" License | 4 votes |
def _get_potentials(self, x, w): # check sizes? n_node_coefs = self.n_prop_states * self.n_prop_features n_link_coefs = self.n_link_states * self.n_link_features n_compat_coefs = self.n_prop_states ** 2 * self.n_link_states if self.compat_features: n_compat_coefs *= self.n_compat_features_ assert w.size == (n_node_coefs + n_link_coefs + n_compat_coefs + self.n_second_order_features_ * self.n_second_order_factors_) w_node = w[:n_node_coefs] w_node = w_node.reshape(self.n_prop_states, self.n_prop_features) w_link = w[n_node_coefs:n_node_coefs + n_link_coefs] w_link = w_link.reshape(self.n_link_states, self.n_link_features) # for readability, consume w. This is not inplace, don't worry. w = w[n_node_coefs + n_link_coefs:] w_compat = w[:n_compat_coefs] if self.compat_features: w_compat = w_compat.reshape((self.n_compat_features_, -1)) w_compat = np.dot(x.X_compat, w_compat) compat_potentials = w_compat.reshape((-1, self.n_prop_states, self.n_prop_states, self.n_link_states)) else: compat_potentials = w_compat.reshape(self.n_prop_states, self.n_prop_states, self.n_link_states) w = w[n_compat_coefs:] coparent_potentials = grandparent_potentials = sibling_potentials = [] if self.coparents: w_coparent = w[:self.n_second_order_features_] coparent_potentials = safe_sparse_dot(x.X_sec_ord, w_coparent) w = w[self.n_second_order_features_:] if self.grandparents: w_grandparent = w[:self.n_second_order_features_] grandparent_potentials = safe_sparse_dot(x.X_sec_ord, w_grandparent) w = w[self.n_second_order_features_:] if self.siblings: w_sibling = w[:self.n_second_order_features_] sibling_potentials = safe_sparse_dot(x.X_sec_ord, w_sibling) prop_potentials = safe_sparse_dot(x.X_prop, w_node.T) link_potentials = safe_sparse_dot(x.X_link, w_link.T) return (prop_potentials, link_potentials, compat_potentials, coparent_potentials, grandparent_potentials, sibling_potentials)