computationalanalysissocialroles.pdfPDFI'm implementing this paper on my data. Help me…
- User
computational_analysis_social_roles.pdfPDFI'm implementing this paper on my data.
Help me complete this function.
Use numpy linalg, and inv:
def calc_curr_semantic_content_new(discussion_data: pd.DataFrame, time: int, vectorizer=None) -> np.ndarray:
""" The projection of the contributions made at time t onto the given space G_t. :param discussion_data: :param time: :param vectorizer: :return: """ given_space, vectorizer = span_given(discussion_data, time, vectorizer) # Get the contributions made at time t current_contributions = discussion_data[discussion_data['contribution_id'] == time]['text'].tolist() cur_embedding = vectorizer.transform(current_contributions) if cur_embedding.shape[1] > 100: # only if high-dimensional svd = TruncatedSVD(n_components=100, random_state=SEED) cur_embedding = svd.fit_transform(cur_embedding) else: cur_embedding = cur_embedding.toarray() # Calculate the projection onto the given space - Assistant
- User
help me correct this if needed:
def calc_curr_semantic_content_given(discussion_data: pd.DataFrame, time: int, vectorizer=None) -> np.ndarray: """ The projection of the contributions made at time t onto the given space G_t.
:param discussion_data: :param time: :param vectorizer: :return: """ given_space, vectorizer = span_given(discussion_data, time, vectorizer) # Get the contributions made at time t current_contributions = discussion_data[discussion_data['contribution_id'] == time]['text'].tolist() cur_embedding = vectorizer.transform(current_contributions) if cur_embedding.shape[1] > 100: # only if high-dimensional svd = TruncatedSVD(n_components=100, random_state=SEED) cur_embedding = svd.fit_transform(cur_embedding) else: cur_embedding = cur_embedding.toarray() # Calculate the projection onto the given space # Standard projection formula: Proj_G(v) = G (G^T G)^(-1) G^T v # where G has given vectors as columns try: current_vector = cur_embedding[0] # Take first (and typically only) contribution # Transpose given_space so vectors are columns: G = given_space.T G = given_space.T # Shape: (n_features, n_previous_contributions) # Compute G^T G (Gram matrix) gram_matrix = np.dot(G.T, G) # Shape: (n_previous_contributions, n_previous_contributions) # Add small regularization for numerical stability reg_param = 1e-8 gram_matrix += reg_param * np.eye(gram_matrix.shape[0]) # Compute G^T * current_vector Gt_v = np.dot(G.T, current_vector) # Shape: (n_previous_contributions,) # Solve (G^T G) * alpha = G^T * v alpha = np.linalg.solve(gram_matrix, Gt_v) # Compute projection: G * alpha projection = np.dot(G, alpha) return projection except np.linalg.LinAlgError: # Fallback to pseudo-inverse if regular solve fails G = given_space.T G_pinv = np.linalg.pinv(G) projection = np.dot(G, np.dot(G_pinv, current_vector)) return projection - Assistant
- User
Are there Python packages for Linear Algebra? I would like to use an already implemented function to calculate the projection and the span
- Assistant
- User
Does this align with the paper's definitions?
def span_given(discussion_data: pd.DataFrame, time: int, vectorizer=None) -> tuple[np.ndarray, TfidfVectorizer]: """ Given a time t, return the set of embeddings of all contributions made until that time. This represents the 'given' space G_t in the paper. Args: time: The time index to filter contributions
Returns: list[list[float]]: Matrix of embeddings for contributions made until time t :param vectorizer: :param time: :param discussion_data: """ # Filter contributions up to the given time contributions = discussion_data[discussion_data['contribution_id'] < time]['text'].tolist() # compute the tf-idf embeddings for the contributions if vectorizer is None: vectorizer = TfidfVectorizer() tfidf_matrix = vectorizer.fit_transform(contributions) else: tfidf_matrix = vectorizer.transform(contributions) if tfidf_matrix.shape[1] > 100: # only if high-dimensional svd = TruncatedSVD(n_components=100, random_state=SEED) tfidf_matrix = svd.fit_transform(tfidf_matrix) else: tfidf_matrix = tfidf_matrix.toarray() return tfidf_matrix, vectorizer - Assistant
- User
I don't want to use the transformer argumtnt. Assume that I have a funcion get_embedding that maps text to their tf-idf representations
- Assistant