Академический Документы
Профессиональный Документы
Культура Документы
and Scikit-Learn
Abdelaziz Testas
and Scikit-Learn
Abdelaziz Testas
https://doi.org/10.1007/978-1-4842-9751-3
While the advice and information in this book are believed to be true
and accurate at the date of publication, neither the authors nor the
editors nor the publisher can accept any legal responsibility for any
errors or omissions that may be made. The publisher makes no
warranty, express or implied, with respect to the material contained
herein.
Table of Contents
Similarity in Syntax
�������������������������������
�������������������������������
�������������������������������
�������������������������� 4
Loading Data
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 5
Selecting Columns
�������������������������������
�������������������������������
�������������������������������
��������������������������� 6
Aggregating Data
�������������������������������
�������������������������������
�������������������������������
����������������������������� 9
Filtering Data
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�� 11
Joining Data
�������������������������������
�������������������������������
�������������������������������
�������������������������������
���� 14
Saving Data
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 17
Modeling Steps
�������������������������������
�������������������������������
�������������������������������
������������������������������
18
Pipelines
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��������� 20
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
������������� 23
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 26
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
������������������������������
36
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 42
Table of ConTenTs
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
�������������������������������
���� 47
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 49
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
������������� 51
and PySpark
�������������������������������
�������������������������������
�������������������������������
�������������� 53
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
����� 54
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
������������� 73
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�� 109
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��������� 111
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 113
and PySpark
�������������������������������
�������������������������������
�������������������������������
������������ 115
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 116
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
�������������������������������
�� 137
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��������� 139
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 142
vi
Table of ConTenTs
and PySpark
�������������������������������
�������������������������������
�������������������������������
������������ 143
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 144
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 167
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 169
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 171
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 174
Logistic Regression
�������������������������������
�������������������������������
�������������������������������
���������������������� 180
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 204
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 208
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 212
and PySpark
�������������������������������
�������������������������������
�������������������������������
������������ 213
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 214
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 235
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 238
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 241
vii
Table of ConTenTs
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 253
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 255
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 258
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 260
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 277
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 278
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 280
and PySpark
�������������������������������
�������������������������������
�������������������������������
������������ 281
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 282
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 294
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 295
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 297
viii
Table of ConTenTs
and PySpark
�������������������������������
�������������������������������
�������������������������������
������������ 299
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 300
MLP Classification
�������������������������������
�������������������������������
�������������������������������
������������������������ 307
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 324
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 325
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 327
Surprise
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 351
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 352
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 353
and PySpark
�������������������������������
�������������������������������
�������������������������������
������������ 355
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 356
Naive Bayes
Classification�������������������������
�������������������������������
�������������������������������
������������ 371
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 389
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 391
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 394
ix
Table of ConTenTs
The Dataset
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 396
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 412
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 414
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 416
Examples of Hyperparameters
�������������������������������
�������������������������������
�������������������������������
����� 417
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 436
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 438
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 440
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 458
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 459
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 461
Table of ConTenTs
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 470
Scikit-Learn
�������������������������������
�������������������������������
�������������������������������
���������������������������� 479
PySpark
�������������������������������
�������������������������������
�������������������������������
�������������������������������
��� 480
Summary���������������������������
�������������������������������
�������������������������������
�������������������������������
����������� 482
Index
�������������������������������
�������������������������������
�������������������������������
������������������������ 483
xi
About the Author
xiii
xv
Acknowledgments
xvii
Introduction
In this context, this book will explore the benefits of using PySpark
over traditional single-node computing tools and provide guidance
for data scientists who are considering transitioning to PySpark.
xix
InTroduCTIon
This book spans 18 chapters and covers multiple topics. The first
two chapters examine why migration from Pandas and Scikit- Learn
to PySpark can be a seamless process, and address the challenges
of selecting an algorithm. Chapters 3–6 build, train, and evaluate
some popular regression models, namely, multiple linear regression,
decision trees, random forests, and gradient-boosted trees, and use
them to deal with some real-world tasks such as predicting house
prices. Chapters 7–12 deal with classification issues by building,
training, and evaluating widely used algorithms such as logistic
regression, decision trees, random forests, support vector machines,
Naive Bayes, and neural networks. In Chapters 13–15, we examine
three additional types of algorithms, namely, recommender systems,
natural language processing, and clustering with k-means. In the
final three chapters, we deal with hyperparameter tuning, pipelines,
and deploying models into production.
xx
CHAPTER 1
An Easy Transition
One of the key factors in making the transition from Pandas and
Scikit-Learn to PySpark relatively easy is the similarity in
functionality. This similarity will become evident after reading this
chapter and executing the code described herein.
Additionally, Spark can also be installed locally through the pip install
pyspark command.
[In]: spark =
SparkSession.builder.appName("BigRivers").getOrCreate()
[In]: pyspark_df.show()
[Out]:
Country
River
United States
Mississippi
Brazil
Amazon
Russia
Volga
2
ChApteR 1 An eASy tRAnSition
Spark application.
[In]: spark =
SparkSession.builder.appName("BigRivers").getOrCreate() Step 3:
Create a PySpark DataFrame
('Russia', 'Volga')]
[In]: print(pandas_df)
[Out]:
Country
River
United States
Mississippi
1
Brazil
Amazon
Russia
Volga
Similarity in Syntax
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside
the United States, check the laws of your country in addition to
the terms of this agreement before downloading, copying,
displaying, performing, distributing or creating derivative works
based on this work or any other Project Gutenberg™ work. The
Foundation makes no representations concerning the copyright
status of any work in any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if
you provide access to or distribute copies of a Project
Gutenberg™ work in a format other than “Plain Vanilla ASCII” or
other format used in the official version posted on the official
Project Gutenberg™ website (www.gutenberg.org), you must, at
no additional cost, fee or expense to the user, provide a copy, a
means of exporting a copy, or a means of obtaining a copy upon
request, of the work in its original “Plain Vanilla ASCII” or other
form. Any alternate format must include the full Project
Gutenberg™ License as specified in paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.