a typical bayes net has the following representation: Graphical Model Notation Nodes:…
- User
a typical bayes net has the following representation: Graphical Model Notation
Nodes: variables (with domains)
Can be assigned (observed) or unassigned (unobserved)
Arcs: interactions
Similar to CSP constraints
Indicate “direct influence” between variables
Formally: encode conditional independence (more later)
For now: imagine that arrows mean direct causation (in general, they don’t!)
with reference to this representation, tell if the following code follows it (used to solve the problem given as: Consider the dataset available here. Refer to the train and test files. This is a multi-label dataset with sparse features. In both train and test files, the first line contains three numbers that denote the number of data points, feature dimensionality, and number of labels in the dataset respectively. Each line from line-2 onwards corresponds to a data point. At the beginning of each line, the comma-separated indices denote the label indices of that data point, followed by a space, followed by the indices of the feature vector with non-zero feature values and the corresponding values (separated by a ‘:’). Task: Learn a Bayes net on the features and use it for predicting the label with the maximum confidence score. Use average “Precision@1” on the test set as the evaluation metric. Note that there may be one or more labels in the ground-truth label-set of a given test data point. Your program should take the train and test files as input, and display average precision@1.)
code: import sys import math from collections import defaultdict
def parse_file(filename): """ Parses the given dataset file. Returns: num_data: int num_features: int num_labels: int data: list of tuples (set of labels, dict of features) """ data = [] with open(filename, 'r') as f: first_line = f.readline() num_data, num_features, num_labels = map(int, first_line.strip().split()) for line in f: line = line.strip() if not line: continue labels_part, features_part = line.split(' ', 1) labels = set(map(int, labels_part.split(','))) features = {} for item in features_part.split(' '): if ':' in item: idx, val = item.split(':', 1) features[int(idx)] = float(val) data.append((labels, features)) return num_data, num_features, num_labels, data
def train_naive_bayes(train_data, num_features, num_labels, alpha=1.0): """ Trains a multi-label Naive Bayes classifier. Returns: log_prior: list of log P(label) log_likelihood: list of dicts containing log P(feature|label) """ label_counts = [0] * num_labels feature_counts = [defaultdict(float) for _ in range(num_labels)]
for labels, features in train_data: for label in labels: label_counts[label] += 1 for f in features: feature_counts[label][f] += 1.0 total_labels = sum(label_counts) log_prior = [] log_likelihood = [] for l in range(num_labels): # Prior with Laplace smoothing prior = (label_counts[l] + alpha) / (total_labels + alpha * num_labels) log_prior.append(math.log(prior)) # Likelihood with Laplace smoothing feature_likelihood = {} denominator = label_counts[l] + alpha * num_features for f in range(num_features): count = feature_counts[l].get(f, 0.0) prob = (count + alpha) / denominator feature_likelihood[f] = math.log(prob) log_likelihood.append(feature_likelihood) print(len(log_likelihood)) return log_prior, log_likelihood - Assistant