<jupyter_start><jupyter_text>This is an [Apache `Spark`](https://spark.apache.org) variation of the [logistic scoring example](https://github.com/WinVector/data_algebra/blob/master/Examples/LogisticExample/ScoringExample.ipynb).<jupyter_code>import pyspark
import pyspark.sql

import pandas

from data_algebra.data_ops import *
import data_algebra.SparkSQL
d_local = pandas.DataFrame({
    'subjectID':[1, 1, 2, 2],
    'surveyCategory': [ "withdrawal behavior", "positive re-framing", "withdrawal behavior", "positive re-framing"],
    'assessmentTotal': [5, 2, 3, 4],
    'irrelevantCol1': ['irrel1']*4,
    'irrelevantCol2': ['irrel2']*4,
})

d_local
handle = data_algebra.SparkSQL.example_handle()

handle.insert_table(d_local, table_name='d', allow_overwrite=True)
local_copy = handle.read_query('SELECT * FROM d')

local_copy
scale = 0.237

ops = data_algebra.data_ops.describe_table(d_local, 'd'). \
    extend({'probability': f'(assessmentTotal * {scale}).exp()'}). \
    extend({'total': 'probability.sum()'},
           partition_by='subjectID'). \
    extend({'probability': 'probability/total'}). \
    extend({'row_number': '_row_number()'},
           partition_by=['subjectID'],
           order_by=['probability'], reverse=['probability']). \
    select_rows('row_number == 1'). \
    select_columns(['subjectID', 'surveyCategory', 'probability']). \
    rename_columns({'diagnosis': 'surveyCategory'})
    
print(ops.to_python(pretty=True))
ops.transform(d_local)

sql = handle.to_sql(ops, pretty=True, use_with=True, annotate=True)

print(sql)
res = handle.read_query(sql)

res
handle.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1676829268292683, 'fraction_numerical': 0.011585365853658536, 'mean_word_length': 5.335907335907336, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '295744', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 594, 'n_tokens_neox': 567, 'n_words': 93, 'path': 'Examples/Spark/pyspark_example.ipynb', 'repo_name': 'WinVector/data_algebra', 'size': '1640'}