<jupyter_start><jupyter_text>Logistic Regression in pyspark<jupyter_code>import findspark
findspark.init()
import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('logreg').getOrCreate()<jupyter_output><empty_output><jupyter_text>Load dataset<jupyter_code>df = spark.read.csv(r'logreg.csv', header=True, inferSchema=True)
df.show(5)
df.count(),len(df.columns)
df.printSchema()
df.describe().show()<jupyter_output>+-------+--------+-----------------+-----------------+--------+-----------------+------------------+
|summary| Country|              Age|   Repeat_Visitor|Platform| Web_pages_viewed|            Status|
+-------+--------+-----------------+-----------------+--------+-----------------+------------------+
|  count|   20000|            20000|            20000|   20000|            20000|             20000|
|   mean|    null|         28.53955|           0.5029|    null|           9.5533|               0.5|
| stddev|    null|7.888912950773227|0.500004090187782|    null|6.073903499824976|0.5000125004687693|
|    min|  Brazil|               17|                0|    Bing|                1|                 0|
|    max|Malaysia|              111|                1|   Yahoo|               29|                 1|
+-------+--------+-----------------+-----------------+--------+-----------------+------------------+<jupyter_text>Converting categorical variables to features  Use LabelEncoding -> OneHotEncoding<jupyter_code>from pyspark.ml.feature import StringIndexer, VectorAssembler, OneHotEncoder
lbl_enc = StringIndexer(inputCol='Country',outputCol='country_labels').fit(df)
df = lbl_enc.transform(df)
df.show(5)
lbl_enc = StringIndexer(inputCol='Platform', outputCol='Platform_labels').fit(df)
df = lbl_enc.transform(df)
ohe = OneHotEncoder(inputCol='country_labels',outputCol='country_vector').fit(df)
df = ohe.transform(df)
ohe = OneHotEncoder(inputCol='Platform_labels',outputCol='platform_vector').fit(df)
df = ohe.transform(df)
vec_asm = VectorAssembler(inputCols=['country_vector', 'Repeat_Visitor', 'Web_pages_viewed', 'Status', 'platform_vector'], outputCol='features')
df = vec_asm.transform(df)
df = df.select('features','Status')<jupyter_output><empty_output><jupyter_text>Splitting Dataset<jupyter_code>train_df, test_df = df.randomSplit([0.8,0.2])
train_df.groupBy('Status').count().show()<jupyter_output>+------+-----+
|Status|count|
+------+-----+
|     1| 7984|
|     0| 7978|
+------+-----+<jupyter_text>Training Model<jupyter_code>from pyspark.ml.classification import LogisticRegression
logreg = LogisticRegression(labelCol='Status').fit(df)<jupyter_output><empty_output><jupyter_text>Prediction and Evaluation<jupyter_code>preds = logreg.evaluate(test_df).predictions
preds = preds.select('Status','prediction')
TP = preds.filter((preds['Status'] == 1) & (preds['prediction'] == 1)).count()
TN = preds.filter((preds['Status'] == 0) & (preds['prediction'] == 0)).count()
FP = preds.filter((preds['Status'] == 0)&(preds['prediction'] == 1)).count()
FN = preds.filter((preds['Status'] == 1)&(preds['prediction'] == 0)).count()
acc = (TP+TN)/(TP+TN+FP+FN)
acc
precision = TP/(TP+FP)
recall = TP/(TP+FN)
precision
recall<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2411214953271028, 'fraction_numerical': 0.04672897196261682, 'mean_word_length': 2.864019253910951, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 24, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '9853', 'license': 'MIT', 'n_tokens_mistral': 1155, 'n_tokens_neox': 1042, 'n_words': 148, 'path': 'Machine Learning with PySpark/Chapter 5 Logistic regression.ipynb', 'repo_name': 'sonwanesuresh95/Books-to-notebooks', 'size': '3210'}