A toxicity score is a scalar estimate of how strongly a comment resembles toxic comments. With TF–IDF vectors, project each comment onto the axis from the nontoxic centroid to the toxic centroid; larger projections indicate greater toxicity. section 4.2.1
A practical scoring workflow is:
- Compute toxic and nontoxic centroids, then subtract them to form the toxicity axis. section 4.2.1
- Calculate vectorized dot products between comment vectors and that axis. section 4.2.1
- Normalize scores to 0–1 and classify comments above a chosen cutoff, such as 0.5. section 4.2.1
From score to prediction
A manually constructed score reached 89.5% accuracy on the messages, while full LDA reached 99.9% on training data but only 55.4% after a train/test split, revealing severe generalization limits.
>>> mask = comments.toxic.astype(bool).values #1 >>> toxic_centroid = tfidf_docs[mask].mean(axis=0) #2 >>> nontoxic_centroid = tfidf_docs[~mask].mean(axis=0) #3 >>> centroid_axis = toxic_centroid - nontoxic_centroid >>> toxicity_score = tfidf_docs.dot(centroid_axis) #4 >>> toxicity_score.round(3) array([-0.008, -0.022, -0.014, ..., -0.025, -0.001, -0.022])
Inspect the centroid axis and calculate all comment toxicity scores in one NumPy dot-product operation. section 4.2.1
