API Reference¶
Top-level package for EduBehaviors-Kit.
Assertion = Literal['sentence_addresses_the_whole_class', 'sentence_answers_a_question', 'sentence_calls_on_student_by_name', 'sentence_checks_for_understanding_or_agreement', 'sentence_evaluates_a_student_response', 'sentence_expresses_certainty_or_emphasis', 'sentence_expresses_confusion_or_requests_help', 'sentence_expresses_emotion_or_humor', 'sentence_expresses_personal_stance_or_thinking_aloud', 'sentence_grants_or_requests_permission', 'sentence_has_a_directive_or_instruction', 'sentence_has_a_question', 'sentence_has_a_rhetorical_question', 'sentence_has_acknowledgment', 'sentence_has_agreement_or_affirmation', 'sentence_has_answer_to_a_math_problem', 'sentence_has_apology', 'sentence_has_comparison_terms', 'sentence_has_counting_sequence', 'sentence_has_disagreement_or_challenge', 'sentence_has_explanation_or_reasoning', 'sentence_has_fraction_terms', 'sentence_has_informal_language', 'sentence_has_math_terms', 'sentence_has_measurement_terms', 'sentence_has_negation_or_denial', 'sentence_has_number', 'sentence_has_politeness_marker', 'sentence_has_praise_or_encouragement', 'sentence_has_time_reference', 'sentence_includes_student_name', 'sentence_invites_participation', 'sentence_is_a_declarative_statement_or_description', 'sentence_is_a_short_utterance', 'sentence_is_incomplete_or_trails_off', 'sentence_manages_classroom_behavior_or_attention', 'sentence_narrates_ongoing_action', 'sentence_poses_a_hypothetical_or_scenario', 'sentence_quotes_or_reads_text_aloud', 'sentence_references_classroom_materials_or_visuals', 'sentence_references_prior_learning_or_lesson', 'sentence_references_student_behavior_or_work', 'sentence_references_task_procedure_or_logistics', 'sentence_repeats_or_revoices_prior_speech', 'sentence_seeks_or_gives_clarification', 'sentence_shows_realization_or_insight', 'sentence_shows_uncertainty', 'sentence_summarizes_or_reviews', 'sentence_uses_collaborative_or_inclusive_language']
module-attribute
¶
Type for assertions with a published model.
DEFAULT_WORDS = ('you', 'to', 'the', 'and', 'so', 'okay', 'i', 'what', 'is', 'a', 'that', 'do', 'of', 'this', 'one', 'going', 'we', 'it', 'your', 'are', 'right', 'have', 'in', 'two', 'be', 'how', 'can', 'three', 'all', 'on', 'number', 'go', 'for', 'then', 'were', 'if', "you're", 'up', 'not', 'think', 'would', 'about', "i'm", 'know', 'just', 'here', 'with', 'its', "it's", 'did', 'need', 'thats', 'want', 'at', 'when', 'now', 'there', 'get', 'four', 'like', 'me', 'because', 'or', 'our', 'lets', 'see', 'but', 'times', 'by', 'five', 'my', 'guys', 'answer', 'make', 'many', 'good', 'write', 'out', 'use', 'was', 'numbers', 'these', 'those', 'them', 'they', 'down', 'got', 'will', 'say', 'why', 'does', 'work', 'add', 'more', 'some', 'no', 'tell', 'math', 'give', 'he')
module-attribute
¶
Default word list for annotation with WordAnnotator
EXISTING_ASSERTIONS = get_args(Assertion)
module-attribute
¶
The assertions with a published model.
AssertionAnnotator
¶
Scores text against one or more assertions using their SetFit classifiers.
Attributes:
| Name | Type | Description |
|---|---|---|
assertions |
list[Assertion]
|
The assertions this annotator scores, in output column order. |
models |
dict[Assertion, SetFitModel]
|
The loaded models, keyed by assertion. Empty when |
device |
device
|
The device inference runs on. |
lazy |
bool
|
Whether models are loaded per call rather than held for the annotator's lifetime. |
Source code in src/edubehaviors/annotation.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
__init__(assertions=None, *, device=None, lazy=True)
¶
Validate the requested assertions and, unless lazy is set, load their models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
assertions
|
Iterable[Assertion] | None
|
The assertions to score. Defaults to |
None
|
device
|
str | device | None
|
The device to run inference on. Defaults to the best available accelerator. |
None
|
lazy
|
bool
|
Whether to load each model only for as long as it is being used, instead of holding every model for the annotator's lifetime. |
True
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/edubehaviors/annotation.py
annotate(inputs, *, batch_size=32, show_progress_bar=None, threshold=0.5)
¶
Annotate inputs for every assertion, the way WordAnnotator.annotate does.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
list[str] | Series
|
The texts to score. |
required |
batch_size
|
int
|
The batch size to encode with. |
32
|
show_progress_bar
|
bool | None
|
Whether to show a progress bar while encoding. |
None
|
threshold
|
float
|
The probability at or above which an assertion fires. |
0.5
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Whether each input fires each assertion, with one column per assertion, sharing the |
DataFrame
|
index of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/edubehaviors/annotation.py
predict(inputs, *, batch_size=32, show_progress_bar=None, threshold=0.5)
¶
Predict whether each input fires each assertion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
list[str] | Series
|
The texts to score. |
required |
batch_size
|
int
|
The batch size to encode with. |
32
|
show_progress_bar
|
bool | None
|
Whether to show a progress bar while encoding. |
None
|
threshold
|
float
|
The probability at or above which an assertion fires. |
0.5
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
Whether each input fires each assertion, with one column per assertion, sharing the |
DataFrame
|
index of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/edubehaviors/annotation.py
predict_proba(inputs, *, batch_size=32, show_progress_bar=None)
¶
Score inputs against every assertion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
list[str] | Series
|
The texts to score. |
required |
batch_size
|
int
|
The batch size to encode with. |
32
|
show_progress_bar
|
bool | None
|
Whether to show a progress bar while encoding. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
The positive-class probability per input, with one column per assertion. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/edubehaviors/annotation.py
ClassificationPipeline
¶
Takes in a labeled DataFrame, annotates it, splits into train/test, trains a standard classifier, and scores the test set.
pipeline = ClassificationPipeline(data, words="all", label_column="label_press_for_reasoning")
print(pipeline.report())
Word features are counted with WordAnnotator and assertion features are scored with
AssertionAnnotator. To use all default words or existing annotators, pass "all".
Otherwise, pass a list of words or assertions, or None to skip an annotator. At
least one of words or assertions must be passed.
Labels are used exactly as they appear in label_column, so a boolean column gives a binary
problem and a column of names gives a multiclass one, with no relabelling behind your back.
Attributes:
| Name | Type | Description |
|---|---|---|
word_annotator |
WordAnnotator | None
|
The word annotator, or None when word features are off. |
assertion_annotator |
AssertionAnnotator | None
|
The assertion annotator, or None when assertion features are off. |
text_column |
str
|
The column holding the text that was annotated. |
label_column |
str
|
The column holding the labels being predicted. |
group_column |
str | None
|
The column that was kept out of both splits at once, or None. |
word_method |
WordMethod
|
Whether word features count occurrences or flag containment. |
assertion_method |
AssertionMethod
|
Whether assertion features are probabilities or booleans. |
test_size |
float
|
The fraction of the data held out for testing. |
stratify |
bool
|
Whether the split preserved the label distribution. |
random_state |
int | RandomState | None
|
The seed the split and the classifier used. |
batch_size |
int
|
The batch size assertion scoring encoded with. |
show_progress_bar |
bool | None
|
Whether assertion scoring showed a progress bar. |
data |
DataFrame
|
The frame the pipeline was built from. |
features |
DataFrame
|
The feature matrix, sharing the index of |
outcome |
Series
|
The labels, as they appeared in |
split |
Series
|
Whether each row is in the train or test split. |
split_strategy |
SplitStrategy
|
How the split was produced. |
groups |
Series | None
|
The group of each row, or None when |
classifier |
LogisticRegressionCV
|
The classifier, fitted on the train split. |
metrics |
dict[str, float]
|
How the classifier scored on the test split. |
Source code in src/edubehaviors/pipeline.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
annotated
property
¶
The data with its features and split assignment, for inspection or export.
Note
A split column already in the data is replaced by the pipeline's own.
coefficients
property
¶
The fitted coefficients, indexed by feature name. Binary problems give a Series; multiclass problems give one column per label.
predicted
property
¶
The held-out rows with their true and predicted labels.
predictions
property
¶
The predicted label of each held-out row, which metrics is computed from.
__init__(data, words=None, assertions=None, *, text_column='sentence', label_column='label', group_column=None, word_method='count', case=False, assertion_method='proba', lazy=True, test_size=0.2, stratify=True, random_state=None, batch_size=32, show_progress_bar=None, features=None)
¶
Annotate data, split it, fit the classifier, and score the held-out split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
The frame holding the text column, the label column, and the group column when one is given. |
required |
words
|
Iterable[str] | Literal['all'] | None
|
The words to count, |
None
|
assertions
|
Iterable[Assertion] | Literal['all'] | None
|
The assertions to score, |
None
|
text_column
|
str
|
The column holding the text to annotate. |
'sentence'
|
label_column
|
str
|
The column holding the labels to predict. Its values are used as they are, so binarize beforehand if that is what you want. |
'label'
|
group_column
|
str | None
|
The column whose values must not straddle the split, for example a transcript id, so that rows from one transcript stay on one side. |
None
|
word_method
|
WordMethod
|
Whether word features count occurrences or flag containment. |
'count'
|
case
|
bool
|
Whether word matching is case-sensitive. |
False
|
assertion_method
|
AssertionMethod
|
Whether assertion features are positive-class probabilities
( |
'proba'
|
lazy
|
bool
|
Whether assertion models are loaded per call rather than held for the
pipeline's lifetime. Pass False when calling |
True
|
test_size
|
float
|
The fraction of the data to hold out. With |
0.2
|
stratify
|
bool
|
Whether the split preserves the label distribution. With |
True
|
random_state
|
int | RandomState | None
|
The seed the split and the classifier use. |
None
|
batch_size
|
int
|
The batch size assertion scoring encodes with. |
32
|
show_progress_bar
|
bool | None
|
Whether assertion scoring shows a progress bar. |
None
|
features
|
DataFrame | None
|
A feature matrix from another pipeline's |
None
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If a configured column is missing from |
TypeError
|
If |
ValueError
|
If neither words nor assertions were requested, |
Warns:
| Type | Description |
|---|---|
UserWarning
|
If the test split holds a single label, which makes its metrics degenerate. |
Source code in src/edubehaviors/pipeline.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
annotate(data)
¶
Build the feature matrix for data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | Series | list[str]
|
A frame holding the text column, or the texts themselves. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
The word features followed by the assertion features, sharing the index of |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
TypeError
|
If the texts are not strings. |
ValueError
|
If |
Source code in src/edubehaviors/pipeline.py
evaluate(*, split='test')
¶
Get macro-averaged metrics for specified split using fitted classifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split
|
Split
|
The split to score. |
'test'
|
Returns:
| Type | Description |
|---|---|
dict[str, float]
|
Accuracy, macro precision, macro recall and macro F1, with the size of each split. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/edubehaviors/pipeline.py
predict(data)
¶
Predict labels for text the pipeline has not seen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | Series | list[str]
|
A frame holding the text column, or the texts themselves. |
required |
Returns:
| Type | Description |
|---|---|
Series
|
The predicted label per row, sharing the index of |
Source code in src/edubehaviors/pipeline.py
predict_proba(data)
¶
Predict label probabilities for text the pipeline has not seen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame | Series | list[str]
|
A frame holding the text column, or the texts themselves. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One column per label, sharing the index of |
Source code in src/edubehaviors/pipeline.py
report(*, split='test', digits=2)
¶
Return classification report for specified split.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
split
|
Split
|
The split to report on. |
'test'
|
digits
|
int
|
The number of decimal places to report. |
2
|
Returns:
| Type | Description |
|---|---|
str
|
SKlearn classification report |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/edubehaviors/pipeline.py
WordAnnotator
¶
Counts occurrences of a list of words in text.
Attributes:
| Name | Type | Description |
|---|---|---|
words |
list[str]
|
The words this annotator counts, in output column order. |
case |
bool
|
Whether matching is case-sensitive. |
Source code in src/edubehaviors/annotation.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
__init__(words=None, *, case=False)
¶
Validate the words to count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words
|
Iterable[str] | None
|
The words to count. Defaults to |
None
|
case
|
bool
|
Whether matching is case-sensitive. Defaults to False, so the lowercase
|
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/edubehaviors/annotation.py
annotate(inputs, *, method='count', allow_na=False)
¶
Annotate inputs for every word in self.words.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inputs
|
list[str] | Series
|
The texts to annotate. |
required |
method
|
WordMethod
|
Whether to count occurrences or flag containment. |
'count'
|
allow_na
|
bool
|
Whether missing values are allowed. When they are, a missing input yields
|
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One column per word, in |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/edubehaviors/annotation.py
standard_classifier(*, Cs=DEFAULT_C_GRID, scoring='f1_macro', class_weight='balanced', cv=5, **kwargs)
¶
Factory method for customized sklearn.linear_model.LogisticRegressionCV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Cs
|
int | Sequence[float]
|
The inverse regularization strengths to choose between, or how many to space logarithmically between 1e-4 and 1e4. |
DEFAULT_C_GRID
|
scoring
|
str | Callable
|
The metric the penalty is chosen against, as a scorer name or a callable. |
'f1_macro'
|
class_weight
|
Mapping | Literal['balanced'] | None
|
How much each class counts toward the loss. |
'balanced'
|
cv
|
int
|
The number of StratifiedKFold folds used. |
5
|
**kwargs
|
Any
|
Keyword arguments to pass to LogisticRegressionCV. |
{}
|
Returns:
| Type | Description |
|---|---|
LogisticRegressionCV
|
An unfitted |