100% found this document useful (1 vote)
14 views59 pages

Advanced Data Analytics Using Python: With Machine Learning, Deep Learning and NLP Examples Mukhopadhyay

The document provides information on various ebooks related to advanced data analytics, machine learning, and deep learning using Python. It includes links to download titles such as 'Advanced Data Analytics Using Python' by Sayan Mukhopadhyay and 'Deep Learning with Python' by Jason Brownlee. The collection is available for instant download in multiple formats at textbookfull.com.

Uploaded by

llengapickey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
14 views59 pages

Advanced Data Analytics Using Python: With Machine Learning, Deep Learning and NLP Examples Mukhopadhyay

The document provides information on various ebooks related to advanced data analytics, machine learning, and deep learning using Python. It includes links to download titles such as 'Advanced Data Analytics Using Python' by Sayan Mukhopadhyay and 'Deep Learning with Python' by Jason Brownlee. The collection is available for instant download in multiple formats at textbookfull.com.

Uploaded by

llengapickey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Explore the full ebook collection and download it now at textbookfull.

com

Advanced Data Analytics Using Python: With Machine


Learning, Deep Learning and NLP Examples
Mukhopadhyay

https://textbookfull.com/product/advanced-data-analytics-
using-python-with-machine-learning-deep-learning-and-nlp-
examples-mukhopadhyay/

OR CLICK HERE

DOWLOAD EBOOK

Browse and Get More Ebook Downloads Instantly at https://textbookfull.com


Click here to visit textbookfull.com and download textbook now
Your digital treasures (PDF, ePub, MOBI) await
Download instantly and pick your perfect format...

Read anywhere, anytime, on any device!

Deep Learning with Python Develop Deep Learning Models on


Theano and TensorFLow Using Keras Jason Brownlee

https://textbookfull.com/product/deep-learning-with-python-develop-
deep-learning-models-on-theano-and-tensorflow-using-keras-jason-
brownlee/
textbookfull.com

Mastering Machine Learning with Python in Six Steps: A


Practical Implementation Guide to Predictive Data
Analytics Using Python 1st Edition Manohar Swamynathan
(Auth.)
https://textbookfull.com/product/mastering-machine-learning-with-
python-in-six-steps-a-practical-implementation-guide-to-predictive-
data-analytics-using-python-1st-edition-manohar-swamynathan-auth/
textbookfull.com

Python Natural Language Processing Advanced machine


learning and deep learning techniques for natural language
processing 1st Edition Jalaj Thanaki
https://textbookfull.com/product/python-natural-language-processing-
advanced-machine-learning-and-deep-learning-techniques-for-natural-
language-processing-1st-edition-jalaj-thanaki/
textbookfull.com

Data Processing with Optimus: Supercharge big data


preparation tasks for analytics and machine learning with
Optimus using Dask and PySpark Leon
https://textbookfull.com/product/data-processing-with-optimus-
supercharge-big-data-preparation-tasks-for-analytics-and-machine-
learning-with-optimus-using-dask-and-pyspark-leon/
textbookfull.com
Practical Machine Learning for Data Analysis Using Python
1st Edition Abdulhamit Subasi

https://textbookfull.com/product/practical-machine-learning-for-data-
analysis-using-python-1st-edition-abdulhamit-subasi/

textbookfull.com

Data Science and Machine Learning Interview Questions


Using Python Second Edition Vishwanathan Narayanan

https://textbookfull.com/product/data-science-and-machine-learning-
interview-questions-using-python-second-edition-vishwanathan-
narayanan/
textbookfull.com

Introducing Data Science Big Data Machine Learning and


more using Python tools 1st Edition Davy Cielen

https://textbookfull.com/product/introducing-data-science-big-data-
machine-learning-and-more-using-python-tools-1st-edition-davy-cielen/

textbookfull.com

Beginning Anomaly Detection Using Python-Based Deep


Learning: With Keras and PyTorch Sridhar Alla

https://textbookfull.com/product/beginning-anomaly-detection-using-
python-based-deep-learning-with-keras-and-pytorch-sridhar-alla/

textbookfull.com

Learn PySpark: Build python-based machine learning and


deep learning models 1st Edition Pramod Singh

https://textbookfull.com/product/learn-pyspark-build-python-based-
machine-learning-and-deep-learning-models-1st-edition-pramod-singh/

textbookfull.com
Advanced
Data Analytics
Using Python
With Machine Learning,
Deep Learning and NLP Examples

Sayan Mukhopadhyay
Advanced Data
Analytics Using
Python
With Machine Learning, Deep
Learning and NLP Examples

Sayan Mukhopadhyay
Advanced Data Analytics Using Python
Sayan Mukhopadhyay
Kolkata, West Bengal, India

ISBN-13 (pbk): 978-1-4842-3449-5 ISBN-13 (electronic): 978-1-4842-3450-1


https://doi.org/10.1007/978-1-4842-3450-1
Library of Congress Control Number: 2018937906

Copyright © 2018 by Sayan Mukhopadhyay


This work is subject to copyright. All rights are reserved by the Publisher, whether the whole
or part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical
way, and transmission or information storage and retrieval, electronic adaptation,
computer software, or by similar or dissimilar methodology now known or hereafter
developed.
Trademarked names, logos, and images may appear in this book. Rather than use a
trademark symbol with every occurrence of a trademarked name, logo, or image we use the
names, logos, and images only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms,
even if they are not identified as such, is not to be taken as an expression of opinion as to
whether or not they are subject to proprietary rights.
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.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Celestin
Development Editor: Matthew Moodie
Coordinating Editor: Divya Modi
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201)
348-4505, e-mail [email protected], or visit www.springeronline.com. Apress
Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business
Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.
com/rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information,
reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this
book is available to readers on GitHub via the book’s product page, located at
www.apress.com/978-1-4842-3449-5. For more detailed information, please visit
www.apress.com/source-code.
Printed on acid-free paper
This is dedicated to all my math teachers,
especially to Kalyan Chakraborty.
Table of Contents
About the Author���������������������������������������������������������������������������������xi
About the Technical Reviewer�����������������������������������������������������������xiii
Acknowledgments������������������������������������������������������������������������������xv

Chapter 1: Introduction������������������������������������������������������������������������1
Why Python?���������������������������������������������������������������������������������������������������������1
When to Avoid Using Python���������������������������������������������������������������������������������2
OOP in Python�������������������������������������������������������������������������������������������������������3
Calling Other Languages in Python���������������������������������������������������������������������12
Exposing the Python Model as a Microservice���������������������������������������������������14
High-Performance API and Concurrent Programming����������������������������������������17

Chapter 2: ETL with Python (Structured Data)������������������������������������23


MySQL�����������������������������������������������������������������������������������������������������������������23
How to Install MySQLdb?�������������������������������������������������������������������������������23
Database Connection�������������������������������������������������������������������������������������24
INSERT Operation������������������������������������������������������������������������������������������24
READ Operation���������������������������������������������������������������������������������������������25
DELETE Operation������������������������������������������������������������������������������������������26
UPDATE Operation�����������������������������������������������������������������������������������������27
COMMIT Operation�����������������������������������������������������������������������������������������28
ROLL-BACK Operation�����������������������������������������������������������������������������������28

v
Table of Contents

Elasticsearch�������������������������������������������������������������������������������������������������������31
Connection Layer API�������������������������������������������������������������������������������������33
Neo4j Python Driver��������������������������������������������������������������������������������������������34
neo4j-rest-client�������������������������������������������������������������������������������������������������35
In-Memory Database������������������������������������������������������������������������������������������35
MongoDB (Python Edition)����������������������������������������������������������������������������������36
Import Data into the Collection����������������������������������������������������������������������36
Create a Connection Using pymongo�������������������������������������������������������������37
Access Database Objects������������������������������������������������������������������������������37
Insert Data�����������������������������������������������������������������������������������������������������38
Update Data���������������������������������������������������������������������������������������������������38
Remove Data�������������������������������������������������������������������������������������������������38
Pandas����������������������������������������������������������������������������������������������������������������38
ETL with Python (Unstructured Data)������������������������������������������������������������������40
E-mail Parsing�����������������������������������������������������������������������������������������������40
Topical Crawling��������������������������������������������������������������������������������������������42

Chapter 3: Supervised Learning Using Python�����������������������������������49


Dimensionality Reduction with Python���������������������������������������������������������������49
Correlation Analysis���������������������������������������������������������������������������������������50
Principal Component Analysis�����������������������������������������������������������������������53
Mutual Information����������������������������������������������������������������������������������������56
Classifications with Python���������������������������������������������������������������������������������57
Semisupervised Learning�����������������������������������������������������������������������������������58
Decision Tree�������������������������������������������������������������������������������������������������������59
Which Attribute Comes First?������������������������������������������������������������������������59
Random Forest Classifier������������������������������������������������������������������������������60

vi
Table of Contents

Naive Bayes Classifier�����������������������������������������������������������������������������������������61


Support Vector Machine��������������������������������������������������������������������������������������62
Nearest Neighbor Classifier��������������������������������������������������������������������������������64
Sentiment Analysis���������������������������������������������������������������������������������������������65
Image Recognition����������������������������������������������������������������������������������������������67
Regression with Python��������������������������������������������������������������������������������������67
Least Square Estimation��������������������������������������������������������������������������������68
Logistic Regression���������������������������������������������������������������������������������������69
Classification and Regression�����������������������������������������������������������������������������70
Intentionally Bias the Model to Over-Fit or Under-Fit������������������������������������������71
Dealing with Categorical Data�����������������������������������������������������������������������������73

Chapter 4: Unsupervised Learning: Clustering�����������������������������������77


K-Means Clustering��������������������������������������������������������������������������������������������78
Choosing K: The Elbow Method���������������������������������������������������������������������������82
Distance or Similarity Measure���������������������������������������������������������������������������82
Properties������������������������������������������������������������������������������������������������������82
General and Euclidean Distance��������������������������������������������������������������������83
Squared Euclidean Distance��������������������������������������������������������������������������84
Distance Between String-Edit Distance��������������������������������������������������������85
Similarity in the Context of Document����������������������������������������������������������������87
Types of Similarity�����������������������������������������������������������������������������������������87
What Is Hierarchical Clustering?�������������������������������������������������������������������������88
Bottom-Up Approach�������������������������������������������������������������������������������������89
Distance Between Clusters���������������������������������������������������������������������������90
Top-Down Approach��������������������������������������������������������������������������������������92
Graph Theoretical Approach��������������������������������������������������������������������������97
How Do You Know If the Clustering Result Is Good?�������������������������������������97

vii
Table of Contents

Chapter 5: Deep Learning and Neural Networks���������������������������������99


Backpropagation�����������������������������������������������������������������������������������������������100
Backpropagation Approach�������������������������������������������������������������������������100
Generalized Delta Rule��������������������������������������������������������������������������������100
Update of Output Layer Weights������������������������������������������������������������������101
Update of Hidden Layer Weights�����������������������������������������������������������������102
BPN Summary���������������������������������������������������������������������������������������������103
Backpropagation Algorithm�������������������������������������������������������������������������������104
Other Algorithms�����������������������������������������������������������������������������������������������106
TensorFlow��������������������������������������������������������������������������������������������������������106
Recurrent Neural Network��������������������������������������������������������������������������������113

Chapter 6: Time Series���������������������������������������������������������������������121


Classification of Variation���������������������������������������������������������������������������������121
Analyzing a Series Containing a Trend��������������������������������������������������������������121
Curve Fitting������������������������������������������������������������������������������������������������122
Removing Trends from a Time Series����������������������������������������������������������123
Analyzing a Series Containing Seasonality�������������������������������������������������������124
Removing Seasonality from a Time Series�������������������������������������������������������125
By Filtering��������������������������������������������������������������������������������������������������125
By Differencing��������������������������������������������������������������������������������������������126
Transformation��������������������������������������������������������������������������������������������������126
To Stabilize the Variance�����������������������������������������������������������������������������126
To Make the Seasonal Effect Additive���������������������������������������������������������127
To Make the Data Distribution Normal���������������������������������������������������������127
Stationary Time Series��������������������������������������������������������������������������������������128
Stationary Process��������������������������������������������������������������������������������������128
Autocorrelation and the Correlogram����������������������������������������������������������129
Estimating Autocovariance and Autocorrelation Functions�������������������������129

viii
Table of Contents

Time-Series Analysis with Python���������������������������������������������������������������������130


Useful Methods��������������������������������������������������������������������������������������������131
Autoregressive Processes���������������������������������������������������������������������������133
Estimating Parameters of an AR Process����������������������������������������������������134
Mixed ARMA Models�����������������������������������������������������������������������������������������137
Integrated ARMA Models�����������������������������������������������������������������������������������138
The Fourier Transform���������������������������������������������������������������������������������������140
An Exceptional Scenario�����������������������������������������������������������������������������������141
Missing Data�����������������������������������������������������������������������������������������������������143

Chapter 7: Analytics at Scale�����������������������������������������������������������145


Hadoop��������������������������������������������������������������������������������������������������������������145
MapReduce Programming���������������������������������������������������������������������������145
Partitioning Function�����������������������������������������������������������������������������������146
Combiner Function��������������������������������������������������������������������������������������147
HDFS File System����������������������������������������������������������������������������������������159
MapReduce Design Pattern�������������������������������������������������������������������������159
Spark�����������������������������������������������������������������������������������������������������������������166
Analytics in the Cloud���������������������������������������������������������������������������������������168
Internet of Things����������������������������������������������������������������������������������������������179

Index�������������������������������������������������������������������������������������������������181

ix
About the Author
Sayan Mukhopadhyay has more than
13 years of industry experience and has been
associated with companies such as Credit
Suisse, PayPal, CA Technologies, CSC, and
Mphasis. He has a deep understanding of
applications for data analysis in domains such
as investment banking, online payments,
online advertisement, IT infrastructure, and
retail. His area of expertise is in applying
high-performance computing in distributed
and data-driven environments such as real-time analysis, high-frequency
trading, and so on.
He earned his engineering degree in electronics and instrumentation
from Jadavpur University and his master’s degree in research in
computational and data science from IISc in Bangalore.

xi
About the Technical Reviewer
Sundar Rajan Raman has more than 14 years
of full stack IT experience in machine
learning, deep learning, and natural
language processing. He has six years
of big data development and architect
experience, including working with Hadoop
and its ecosystems as well as other NoSQL
technologies such as MongoDB and
Cassandra. In fact, he has been the technical
reviewer of several books on these topics.
He is also interested in strategizing using Design Thinking principles
and in coaching and mentoring people.

xiii
Acknowledgments
Thanks to Labonic Chakraborty (Ripa) and Kusumika Mukherjee.

xv
CHAPTER 1

Introduction
In this book, I assume that you are familiar with Python programming.
In this introductory chapter, I explain why a data scientist should choose
Python as a programming language. Then I highlight some situations
where Python is not a good choice. Finally, I describe some good practices
in application development and give some coding examples that a data
scientist needs in their day-to-day job.

W
 hy Python?
So, why should you choose Python?

• It has versatile libraries. You always have a ready-­


made library in Python for any kind of application.
From statistical programming to deep learning to
network application to web crawling to embedded
systems, you will always have a ready-made library in
Python. If you learn this language, you do not have to
stick to a specific use case. R has a rich set of analytics
libraries, but if you are working on an Internet of Things
(IoT) application and need to code in a device-side
embedded system, it will be difficult in R.

© Sayan Mukhopadhyay 2018 1


S. Mukhopadhyay, Advanced Data Analytics Using Python,
https://doi.org/10.1007/978-1-4842-3450-1_1
Chapter 1 Introduction

• It is very high performance. Java is also a versatile


language and has lots of libraries, but Java code runs
on a Java virtual machine, which adds an extra layer
of latency. Python uses high-performance libraries
built in other languages. For example, SciPy uses
LAPACK, which is a Fortran library for linear algebra
applications. TensorFlow uses CUDA, which is a C
library for parallel GPU processing.

• It is simple and gives you a lot of freedom to code.


Python syntax is just like a natural language. It is easy to
remember, and it does not have constraints in variables
(like constants or public/private).

When to Avoid Using Python


Python has some downsides too.

• When you are writing very specific code, Python may


not always be the best choice. For example, if you are
writing code that deals only with statistics, R is a better
choice. If you are writing MapReduce code only, Java is
a better choice than Python.

• Python gives you a lot of freedom in coding. So, when


many developers are working on a large application,
Java/C++ is a better choice so that one developer/
architect can put constraints on another developer’s
code using public/private and constant keywords.

• For extremely high-performance applications, there is


no alternative to C/C++.

2
Chapter 1 Introduction

O
 OP in Python
Before proceeding, I will explain some features of object-oriented
programming (OOP) in a Python context.
The most basic element of any modern application is an object. To
a programmer or architect, the world is a collection of objects. Objects
consist of two types of members: attributes and methods. Members can be
private, public, or protected. Classes are data types of objects. Every object
is an instance of a class. A class can be inherited in child classes. Two
classes can be associated using composition.
In a Python context, Python has no keywords for public, private, or
protected, so encapsulation (hiding a member from the outside world)
is not implicit in Python. Like C++, it supports multilevel and multiple
inheritance. Like Java, it has an abstract keyword. Classes and methods
both can be abstract.
The following code is an example of a generic web crawler that is
implemented as an airline’s web crawler on the Skytrax site and as a retail
crawler for the Mouthshut.com site. I’ll return to the topic of web crawling
in Chapter 2.

from abc import ABCMeta, abstractmethod


import BeautifulSoup
import urllib
import sys
import bleach
#################### Root Class (Abstract) ####################
class SkyThoughtCollector(object):
       __metaclass__ = ABCMeta

       baseURLString = "base_url"
       airlinesString = "air_lines"
       limitString = "limits"

3
Chapter 1 Introduction

       baseURl = ""
       airlines = []
       limit = 10

       @abstractmethod
       def collectThoughts(self):
             print "Something Wrong!! You're calling
an abstract method"

       @classmethod
       def getConfig(self, configpath):
             #print "In get Config"
             config = {}
             conf = open(configpath)
             for line in conf:
                    if ("#" not in line):
                          words = line.strip().split('=')
                          config[words[0].strip()] = words[1].
strip()
             #print config
             self.baseURl = config[self.baseURLString]
             if config.has_key(self.airlinesString):
                    self.airlines = config[self.
airlinesString].split(',')
             if config.has_key(self.limitString):
                    self.limit = int(config[self.limitString])
             #print self.airlines

       def downloadURL(self, url):


             #print "downloading url"
             pageFile = urllib.urlopen(url)

4
Chapter 1 Introduction

             if pageFile.getcode() != 200:


                    return "Problem in URL"
             pageHtml = pageFile.read()
             pageFile.close()
             return "".join(pageHtml)

       def remove_junk(self, arg):


             f = open('junk.txt')
             for line in f:
                    arg.replace(line.strip(),'')
             return arg

       def print_args(self, args):


             out =''
             last = 0
             for arg in args:
                    if args.index(arg) == len(args) -1:
                          last = 1
                    reload(sys)
                    sys.setdefaultencoding("utf-8")
                    
arg = arg.decode('utf8','ignore').
encode('ascii','ignore').strip()
                    arg = arg.replace('\n',' ')
                    arg = arg.replace('\r','')
                    arg = self.remove_junk(arg)
                    if last == 0:
                          out = out + arg + '\t'
                    else:
                          out = out + arg
             print out

5
Chapter 1 Introduction

####################### Airlines Chield #######################

class AirLineReviewCollector(SkyThoughtCollector):

      months = ['January', 'February', 'March', 'April', 'May',


'June', 'July', 'August', 'September', 'October', 'November',
'December' ]

       def __init__(self, configpath):


             #print "In Config"
       super(AirLineReviewCollector,self).getConfig(configpath)

       def parseSoupHeader(self, header):


             #print "parsing header"
             name = surname = year = month = date = country =''
             txt = header.find("h9")
             words = str(txt).strip().split(' ')
             for j in range(len(words)-1):
                    if words[j] in self.months:
                          date = words[j-1]
                          month= words[j]
                          year = words[j+1]
                          name = words[j+3]
                          surname = words[j+4]
             if ")" in words[-1]:
                    country = words[-1].split(')')[0]
             if "(" in country:
                    country = country.split('(')[1]
             else:
                    country = words[-2].split('(')[1] + country
             return (name, surname, year, month, date, country)

6
Chapter 1 Introduction

       def parseSoupTable(self, table):


             #print "parsing table"
             images = table.findAll("img")
             over_all = str(images[0]).split("grn_bar_")[1].
split(".gif")[0]
             money_value = str(images[1]).split("SCORE_")[1].
split(".gif")[0]
             seat_comfort = str(images[2]).split("SCORE_")[1].
split(".gif")[0]
             staff_service = str(images[3]).split("SCORE_")[1].
split(".gif")[0]
             catering = str(images[4]).split("SCORE_")[1].
split(".gif")[0]
             entertainment = str(images[4]).split("SCORE_")[1].
split(".gif")[0]
             if 'YES' in str(images[6]):
                    recommend = 'YES'
             else:
                    recommend = 'NO'
             status = table.findAll("p", {"class":"text25"})
             stat = str(status[2]).split(">")[1].split("<")[0]
             return (stat, over_all, money_value, seat_comfort,
staff_service, catering, entertainment, recomend)

       def collectThoughts(self):
             #print "Collecting Thoughts"
             for al in AirLineReviewCollector.airlines:
                    count = 0
                    while count < AirLineReviewCollector.limit:
                          count = count + 1
                          url = ''

7
Chapter 1 Introduction

                          if count == 1:
                                 url = AirLineReviewCollector.
baseURl + al + ".htm"
                          else:
                                 url = AirLineReviewCollector.
baseURl + al + "_"+str(count)+
".htm"
                          soup = BeautifulSoup.BeautifulSoup
(super(AirLineReviewCollector,self).
downloadURL(url))
                          blogs = soup.findAll("p",
{"class":"text2"})
                          tables = soup.findAll("table",
{"width":"192"})
                          review_headers = soup.findAll("td",
{"class":"airport"})
                          for i in range(len(tables)-1):
                                 (name, surname, year, month,
date, country) = self.parse
SoupHeader(review_headers[i])
                                 (stat, over_all, money_value,
seat_comfort, staff_service,
catering, entertainment,
recomend) = self.parseSoup
Table(tables[i])
                                 blog = str(blogs[i]).
split(">")[1].split("<")[0]
                                 args = [al, name, surname,
year, month, date, country,
stat, over_all, money_value,
seat_comfort, staff_service,
catering, entertainment,
recomend, blog]
8
Chapter 1 Introduction

                                        
super(AirLineReviewCo
llector,self).print_
args(args)

######################## Retail Chield ########################

class RetailReviewCollector(SkyThoughtCollector):
       def __init__(self, configpath):
             #print "In Config"
       super(RetailReviewCollector,self).getConfig(configpath)

       def collectThoughts(self):
             soup = BeautifulSoup.BeautifulSoup(super(RetailRev
iewCollector,self).downloadURL(RetailReviewCollect
or.baseURl))
             lines = soup.findAll("a",{"style":
"font-­size:15px;"})
             links = []
             for line in lines:
                     if ("review" in str(line)) & ("target" in
str(line)):
                          ln = str(line)
                           link = ln.split("href=")[-1].split
("target=")[0].replace("\"","").
strip()
                          links.append(link)

             for link in links:


                    
soup = BeautifulSoup.BeautifulSoup(
super(RetailReviewCollector,self).
downloadURL(link))

9
Chapter 1 Introduction

                    comment = bleach.clean(str(soup.findAll("di
v",{"itemprop":"description"})[0]),tags=[],
strip=True)
                    tables = soup.findAll("table",
{"class":"smallfont space0 pad2"})
                    parking = ambience = range = economy =
product = 0
                    for table in tables:
                          if "Parking:" in str(table):
                                 rows = table.findAll("tbody")
[0].findAll("tr")
                                 for row in rows:
                                       if "Parking:" in
str(row):
                                              parking =
str(row).
count("read-
barfull")
                                       if "Ambience" in
str(row):
                                              ambience =
str(row).
count("read-
barfull")
                                       if "Store" in str(row):
                                              range = str(row).
count("read-
barfull")

10
Chapter 1 Introduction

                                       if "Value" in str(row):


                                              
economy =
str(row).
count("read-
barfull")
                                       if "Product" in str(row):
                                              
product =
str(row).count
("smallratefull")

                    
author = bleach.clean(soup.findAll("spa
n",{"itemprop":"author"})[0], tags=[],
strip=True)
                    
date = soup.findAll("meta",{"itemprop":"dat
ePublished"})[0]["content"]
                    
args = [date, author,str(parking),
str(ambience),str(range), str(economy),
str(product), comment]
                                        
super(RetailReview
Collector,self).print_
args(args)

######################## Main Function ########################

if __name__ == "__main__":
       if sys.argv[1] == 'airline':
             instance = AirLineReviewCollector(sys.argv[2])
             instance.collectThoughts()
       else:
             if sys.argv[1] == 'retail':
                    
instance = RetailReviewCollector(sys.argv[2])
                    instance.collectThoughts()

11
Chapter 1 Introduction

             else:
                    print "Usage is"
                    print sys.argv[0], '<airline/retail>',
"<Config File Path>"

The configuration for the previous code is shown here:

base_url = http://www.airlinequality.com/Forum/
#base_url = http://www.mouthshut.com/product-reviews/Mega-Mart-­
Bangalore-reviews-925103466
#base_url = http://www.mouthshut.com/product-reviews/Megamart-­
Chennai-­reviews-925104102
air_lines = emrts,brit_awys,ual,biman,flydubai
limits = 10

I’ll now discuss the previous code in brief. It has a root class that is an
abstract class. It contains essential attributes such as a base URL and a
page limit; these are essential for all child classes. It also contains common
logic in class method functions such as the download URL, print output,
and read configuration. It also has an abstract method collectThoughts,
which must be implemented in child classes. This abstract method is
passing on a common behavior to every child class that all of them must
collect thoughts from the Web. Implementations of this thought collection
are child specific.

Calling Other Languages in Python


Now I will describe how to use other languages’ code in Python. There are
two examples here; one is calling R code from Python. R code is required
for some use cases. For example, if you want a ready-made function for the
Holt-Winter method in a time series, it is difficult to do in Python. But it is

12
Chapter 1 Introduction

available in R. So, you can call R code from Python using the rpy2 module,
as shown here:

import rpy2.robjects as ro
ro.r('data(input)')
ro.r('x <-HoltWinters(input)')

Sometimes you need to call Java code from Python. For example,
say you are working on a name entity recognition problem in the field of
natural language processing (NLP); some text is given as input, and you
have to recognize the names in the text. Python’s NLTK package does have
a name entity recognition function, but its accuracy is not good. Stanford
NLP is a better choice here, which is written in Java. You can solve this
problem in two ways.

• You can call Java at the command line using


Python code.

import subprocess

subprocess.call(['java','-cp','*','edu.
stanford.nlp.sentiment.SentimentPipeline',
'-file','foo.txt'])

• You can expose Stanford NLP as a web service and call


it as a service.

nlp = StanfordCoreNLP('http://127.0.0.1:9000')
output = nlp.annotate(sentence, properties={
"annotators": "tokenize,ssplit,parse,sentiment",
"outputFormat": "json",
# Only split the sentence at End Of Line.
We assume that this method only takes in one
single sentence.
"ssplit.eolonly": "true",

13
Chapter 1 Introduction

# Setting enforceRequirements to skip some


annotators and make the process faster
"enforceRequirements": "false"
})

E xposing the Python Model


as a Microservice
You can expose the Python model as a microservice in the same way as
your Python model can be used by others to write their own code. The best
way to do this is to expose your model as a web service. As an example, the
following code exposes a deep learning model using Flask:

from flask import Flask, request, g


from flask_cors import CORS
import tensorflow as tf
from sqlalchemy import *
from sqlalchemy.orm import sessionmaker
import pygeoip
from pymongo import MongoClient
import json
import datetime as dt
import ipaddress
import math

app = Flask(__name__)
CORS(app)

@app.before_request
def before():
       db = create_engine('sqlite:///score.db')
       metadata = MetaData(db)

14
Chapter 1 Introduction

       g.scores = Table('scores', metadata, autoload=True)


       Session = sessionmaker(bind=db)
       g.session = Session()

       client = MongoClient()
       g.db = client.frequency

       g.gi = pygeoip.GeoIP('GeoIP.dat')

       sess = tf.Session()
       new_saver = tf.train.import_meta_graph('model.obj.meta')
       new_saver.restore(sess, tf.train.latest_checkpoint('./'))
       all_vars = tf.get_collection('vars')

       g.dropped_features = str(sess.run(all_vars[0]))
       g.b = sess.run(all_vars[1])[0]
       return

def get_hour(timestamp):
       return dt.datetime.utcfromtimestamp(timestamp / 1e3).hour

def get_value(session, scores, feature_name, feature_value):


       s = scores.select((scores.c.feature_name == feature_
name) & (scores.c.feature_value == feature_value))
       rs = s.execute()
       row = rs.fetchone()
       if row is not None:
             return float(row['score'])
       else:
             return 0.0

15
Chapter 1 Introduction

@app.route('/predict', methods=['POST'])
def predict():
       input_json = request.get_json(force=True)

       features = ['size','domain','client_time','device',
'ad_position','client_size', 'ip','root']
       predicted = 0
       feature_value = ''
       for f in features:
             if f not in g.dropped_features:
                    if f == 'ip':
                          feature_value = str(ipaddress.
IPv4Address(ipaddress.ip_address
(unicode(request.remote_addr))))
                    else:
                          feature_value = input_json.get(f)
                    if f == 'ip':
                          if 'geo' not in g.dropped_features:
                                 geo = g.gi.country_name_by_
addr(feature_value)
                                 predicted = predicted + get_
value(g.session, g.scores,
'geo', geo)
                          if 'frequency' not in g.dropped_
features:
                                 res = g.db.frequency.find_
one({"ip" : feature_value})
                                 freq = 1
                                 if res is not None:
                                       freq = res['frequency']
                                 predicted = predicted + get_
value(g.session, g.scores,
'frequency', str(freq))
16
Chapter 1 Introduction

                    if f == 'client_time':
                           feature_value = get_
hour(int(feature_value))
                    predicted = predicted + get_value(g.
session, g.scores, f, feature_value)
       return str(math.exp(predicted + g.b)-1)
app.run(debug = True, host ='0.0.0.0')

This code exposes a deep learning model as a Flask web service.


A JavaScript client will send the request with web user parameters such
as the IP address, ad size, ad position, and so on, and it will return the
price of the ad as a response. The features are categorical. You will learn
how to convert them into numerical scores in Chapter 3. These scores
are stored in an in-memory database. The service fetches the score from
the database, sums the result, and replies to the client. This score will be
updated real time in each iteration of training of a deep learning model. It
is using MongoDB to store the frequency of that IP address in that site. It is
an important parameter because a user coming to a site for the first time
is really searching for something, which is not true for a user where the
frequency is greater than 5. The number of IP addresses is huge, so they
are stored in a distributed MongoDB database.

 igh-Performance API and Concurrent


H
Programming
Flask is a good choice when you are building a general solution that is
also a graphical user interface (GUI). But if high performance is the most
critical requirement of your application, then Falcon is the best choice. The
following code is an example of the same model shown previously exposed
by the Falcon framework. Another improvement I made in this code is that
I implemented multithreading, so the code will be executed in parallel.

17
Random documents with unrelated
content Scribd suggests to you:
CHAPTER THE TWENTY-FIRST.
BACK AT TREVLYN.
“Randolph! Can this really be Trevlyn?”
The young countess stood in all her radiant loveliness upon the
threshold of her old home, and turned her happy face towards the
husband who stood beside her, watching with a smile in his eyes for
the effect to be produced by his labour of love.
“Can this really be Trevlyn?”
“You seemed destined never to know your old home again when you
have been banished from it, Monica,” he answered, smiling. “Well, is
it as much changed as you expected?”
“It is perfect,” said Monica simply; adding, after another long look
round her: “If only my father could have seen this—could have lived
to witness the realisation of his dream!”
But he would not let her indulge one sad thought that should cloud
the brightness of this happy home-coming. He kissed her gently in
token of his sympathy, and then drew her towards the blazing fire,
whose dancing flames were illuminating the great hall.
“Does it realise your dream, too, my Monica?” he asked softly.
She looked up in his face, deep feeling welling up in the glance of
her soft dark eyes.
“To be with you is my dream, Randolph. That is enough for me.”
He saw that she was moved, and knew that the associations of
Trevlyn, the old home, were crowding upon her. Without speaking,
he led her towards a door, which in old days led to a room vast and
empty, save for the odds and ends of lumber that gradually
accumulated there. Monica glanced up in a sort of surprise as he
turned the handle. Why was he taking her there?
She paused on the threshold, and looked about her in mute amaze.
The floor was of polished parquetrie work; the panelled walls,
quaintly and curiously carved, shone with the care that had been
bestowed upon them; the vaulted roof had been carefully restored
and was a fine specimen of mediæval skill and beauty. The
mullioned window to the west had been filled with rich stained glass,
that gave back a dusky glimmer through its tinted panes, though the
daylight was failing fast. Near to the window stood the one great
feature of the room, an organ, which Monica’s eyes saw at once was
a particularly fine and perfect instrument. An organ of her very own!
It was just like Randolph to think of it! She gave him one sweet
glance of gratitude, and went up to it in the dim, dusky twilight.
“How good you are to me!” she said softly.
He heard the little quiver in her voice, and bent his head to kiss her;
but he spoke in a lighter tone.
“Do you like it? I am so glad! I thought your home ought not to be
without its music-room. See, Monica, your organ will be a sort of
friend to whom you can confide all your secrets; for you want
nobody to blow it for you. You can set the bellows at work by just
turning this handle, and nobody need disturb your solitude when you
want to be alone.”
She looked up gratefully. He never forgot anything—not even her old
love for solitude.
“I never want to be alone now, Randolph,” she said. “I always want
you.”
“And you generally have me, sweet wife. I think we have hardly
been separated for more than a few hours at a time since that
happy, happy day that made you really mine.”
“I want it always to be like that,” said Monica, dreamily; “always like
that.”
He looked at her, and carried the hand that he held to his lips.
“Will you play, Monica?”
She sat down and struck a few dreamy chords, gradually leading up
to the theme that was in her mind. Randolph leaned against the
mullioned window-frame and watched her. He could see, even in the
darkness, the pure, pale outline of her perfect profile, and the crown
of her golden hair that framed her face like an aureole.
“Another dream realised, Monica,” he said softly, as she turned to
him at length.
“What dream, Randolph?”
“A dream that came to me once, in the little cliff church where we
were married, as I watched you—little as you knew it—sitting at the
organ, and playing to yourself, one sunny afternoon. But this is
better than any dream of pictured saint or spirit—my Monica, my
own true wife.”
She looked up at him, and came and put her arms about his neck—
an unusual demonstration, even now, for her, and they stood very
close together in the gathering darkness that was not dark to them.

Monica paid an early visit to St. Maws to see her friends, and to
confide to Mrs. Pendrill a little of the wonderful happiness that had
flooded her life with sunshine. Then, too, she wanted to see Tom,
and to ask him the result of the mission he had half promised to
undertake. So far she had learned nothing save that Fitzgerald had
not been seen near Trevlyn for many weeks, and was supposed to
have gone abroad.
“Did you see him, Tom?” she asked, when she had found the
opportunity she desired.
“Yes, once or twice. I had a good look at him. I should not call him
exactly mad, though in a decidedly peculiar mental state. We merely
met, as it were, by chance, and talked on indifferent subjects for the
most part. Once he asked me, in a sort of veiled way, for
professional advice, describing certain unpleasant symptoms and
sensations. I advised him to give up the use of spirits, and to try
what travelling would do for him. He seemed to think he would take
my advice, and shortly afterwards he disappeared from the
neighbourhood; but where he has gone I do not know.”
Monica knew that this advice had been followed. “He may go
anywhere he likes, if he will only keep away from here,” she said. “I
am very much obliged to you, Tom, for doing as I asked.”
“Pray don’t mention it.”
“I must mention it, because it was very good of you. Tom, will you
come and stay at Trevlyn next week? We have one or two people
coming for the pheasants, and we want you to make one of the
party, if you will.”
“Oh, very well; anything to please. I have had no shooting worth
speaking of so far. I should like a week’s holiday very well.”
So that matter was speedily and easily arranged.
Tom did not ask who were the guests he was to meet, and Monica
did not think of naming such entire strangers, Lord Haddon and Lady
Beatrice Wentworth. She forgot that Tom and the young earl had
met once before on a different occasion.
Those two were to be the first guests. Perhaps later on they would
ask more, but Monica was too entirely happy in her present life to
wish it in any way disturbed, and Randolph by no means cared to be
obliged to give up to guests those happy hours that heretofore he
had always spent with Monica. But Beatrice and her brother had
already been invited. They were his oldest friends, and were
Monica’s friends too. She was glad to welcome them to her old
home, and the rapturous admiration that its beauties elicited would
have satisfied a more exacting nature than hers.
Beatrice was, as usual, radiant, bewitching, delightful. Monica
wished that Tom had come in time to see her arrival, and listen to
her sparkling flow of talk. Tom professed to be a woman-hater, or
next door to it, but she thought that even he would have to make an
exception in favour of Lady Beatrice Wentworth.
She went upstairs with her guest to her room at length, when
Beatrice suddenly turned towards her, with quite a new expression
upon her face.
“Monica,” she said, looking straight into her eyes, “you are changed
—you are different from what you were in London—different even
from what you were in Scotland, though I saw a change then. I
don’t know how to express it, but you are beautified—glorified. What
is it? What has changed you since I first knew you?”
Monica knew right well; but some feelings could not be translated
into words.
“I am very happy,” she said, quietly. “If there is any change, that
must be the cause.”
“Happier than you have ever been before?”
“Yes; I think every week makes me happier. I learn to know my
husband better and better, you see.”
A sudden wistful sadness flashed into the eyes so steadily regarding
her. Monica saw it before it had been blotted out by the arch drollery
of the look that immediately succeeded.
“And it does not wear off, Monica? Sometimes it does, you know—
after a time. Will it ever, in your case, do you think?”
“I think not,” she answered.
“And I think not, too,” answered Beatrice. “Ah me! How happy some
people are!”
She laughed, but there was something of bitterness in the tone.
Monica looked at her seriously.
“Are you not happy, Beatrice?”
The girl’s audacious smile beamed out over her face.
“Don’t I look so?”
“Sometimes—not always.”
“One must have variety before all things, you know,” was the gay
answer. “It would never do to be always in the same style—it lacks
piquancy after a time. Now let me have time to beautify myself in
harmony with this most charming of old places, and come back for
me when you are dressed; I feel as if I should lose my way, or see
bogies in these delightful corridors and staircases.”
And Monica left her guest as desired, coming back, half an hour
later, to find her transformed into the semblance of some pictured
dame of a century or two gone by, in stiff amber brocade, quaintly
cut about the neck and sleeves, and relieved here and there by
dazzling scarlet blossoms. Beatrice never at any time looked like
anybody else, but to-night she was particularly, strikingly original.
“Ah, you black-robed queen, you will just do as a foil for me!” was
the greeting Monica received. “Whenever I see you in any garb, no
matter what it is, I always think it is just one that suits you best of
everything. Are you having a dinner-party to-night?”
“Not exactly. A few men are coming, who have asked Randolph to
shoot since we came back. You and I are the only ladies.”
And then they went down to the empty drawing-room a good half-
hour before any one else was likely to appear.
Beatrice chatted away very brightly. She seemed in gay spirits, and
had a great deal to tell of what had passed since their farewell in
Scotland a month or two ago.
She moved about the drawing-room, examining the various
treasures it contained, and admiring the beauty of the pictures. She
was standing half concealed by the curtains draping a recessed
window, when the door opened, admitting Tom Pendrill. He was in
dinner dress, having arrived about an hour previously.
“You have come then, Tom,” said Monica. “I am glad. I was afraid
you meant to desert us after all.”
“The wish being father to the thought, I presume,” answered Tom,
shaking hands. “By-the-bye, here is a letter from Arthur’s doctor I’ve
brought to show you. He gives a capital account of his patient. Can
you read German writing, or shall I construe? He writes about as
crabbedly as——”
And here Tom stopped short, seeing that Monica was not alone.
“I beg your pardon,” he added, drawing himself up with a
ceremoniousness quite unusual with him.
“Not at all,” answered Monica, quietly. “Let me introduce you to Lady
Beatrice Wentworth—Mr. Tom Pendrill.”
They exchanged bows very distantly. Monica became suddenly
aware, in some subtle, inexplicable fashion, that these two were not
strangers to one another—that this was not their first meeting.
Moreover, it appeared as if their former acquaintance, such as it
was, could have been by no means agreeable to either, for it was
easy to see that a sort of covert antagonism existed between them
which neither of them took over much pains to conceal.
Tom’s face assumed its most sharply cynical expression, as he drew
at once into his hardest shell of distant reserve and sarcastic
politeness.
Beatrice opened her feather fan, and wielded it with a sort of
aggressive negligence. She dropped into a seat beside Monica, and
began to talk to her with an air of studied affectation utterly at
variance with her ordinary manner, ignoring Tom as entirely as if no
introduction had passed between them, and that with an assumption
of hauteur that could only be explained by a deeply-seated
antipathy.
Monica tried to include Tom in the conversation; but he declined to
be included, returned an indifferent answer, and withdrew to a
distant corner of the room, where he remained deeply engrossed, as
it seemed, in the study of a photographic album.
Monica was perplexed. She could not imagine what it all meant. She
had never heard the Pendrills speak of Lady Beatrice Wentworth,
and she was sufficiently acquainted with Tom’s history to render this
perplexity the greater. She was certain Mrs. Pendrill had heard the
name of her expected guest, and it had aroused no emotion in her.
Yet she would presumably know the name of a lady towards whom
her nephew cherished so great an antipathy. Monica could not make
it out. But one thing was plain enough: those two were sworn foes,
and intended to remain so—and they were guests beneath the same
roof!
CHAPTER THE TWENTY-SECOND.
AN ENIGMA.
It was a relief when the other men came in, and when dinner was
announced. Randolph evidently knew nothing of any disturbing
element in the party as he handed Beatrice in to dinner, and again
made a sort of attempt to introduce her to Tom, who was seated
opposite, not knowing that Monica had already had an opportunity
of performing that little ceremony.
“You are two of my oldest friends, you know,” said their host, in his
pleasant, easy fashion, “and you are both my guests now, so you
will have a capital opportunity of expatiating together upon my many
perfections.”
“No need for that, Randolph,” answered Beatrice, gaily. “They speak
too loud for themselves, and your wife’s eyes tell too many tales of
them. You know I never could bear paragons. If you turn into one, I
shall have no more to say to you.”
“You are very cutting, Beatrice; almost as much so as Tom here. It is
really rather a trying position to be hedged in between a clever
woman and a clever man.”
“If you call me a clever woman again, Randolph, I’ll never forgive
you. I abominate the whole race!” cried Beatrice, hotly; “and as for
clever men—I detest them!”
This was said so heartily as to elicit a guffaw of laughter from a
ruddy-faced young gentleman of sporting tastes, who was her
neighbour on the other side. She turned to him with one of her most
sparkling glances.
“Now you, I am quite certain, agree with me. Your face tells me you
do. Don’t you think that it is the clever people who make the world
an intolerable place?”
“They’re the greatest nuisance out,” assented that young gentleman,
cordially. “I always did say so. I was never clever. I was plucked
three times, I think, for my little-go.”
“Then you and I are sure to be great friends,” said Beatrice,
laughing. “I am quite, quite sure I should never have passed any
examination if I had been a man. I was at Oxford once, long ago;
and oh! you know, the only men that were any good at all were
those who had been ‘plucked,’ as they call it, or fully expected to be.
The clever, good, precocious boys were—oh! well, let us not think of
them. It takes away one’s appetite!”
The sporting gentleman laughed, and enjoyed this summary verdict;
but Randolph just glanced across at his wife. He, too, was aware
that there was something odd in Beatrice’s manner. He detected the
covert vein of bitterness in her tone; and he was as much at a loss
to understand it as any one else could be. Tom’s face and
impenetrable silence puzzled him likewise.
Dinner, however, passed smoothly enough. Beatrice was very lively,
and her witticisms kept all the table alive. Her young neighbour lost
his heart to her at once, and she flirted with him in the most frank
and open fashion possible. She could be very fascinating when she
chose, and to-night, after the first edge had been taken off her
sallies, she was, undoubtedly, exceedingly attractive.
If there was something a little forced in her mirth, at least nobody
detected it, save those who knew her very well, and not even all of
those, for Haddon was obviously unconscious that anything was
wrong, and talked to Monica in the most unconcerned fashion
possible. What Tom thought of it all nobody could hazard an opinion.
At length Monica gave the signal to her animated guest, and they
two withdrew together. Beatrice laughed gaily, as she half walked
half waltzed across the hall, humming a dance tune the while.
“What a lovely place this would be for a dance!” she exclaimed, “a
masked, or, better still, a fancy dress ball. Shouldn’t we look
charming in these panelled rooms, flitting about this great baronial
hall, and up and down that delightful staircase? Monica, you and
Randolph mustn’t get lazy; you must live up to your house. It is too
beautiful to be wasted. If you don’t know how to manage matters, I
must come and teach you?”
And so she rattled on, first on one theme, and then on another, in
restless, aimless fashion, as people do who are talking against time,
or talking with a purpose, determined not to let silence fall between
them and their companions. It was easy to see that Beatrice wished
to avoid any confidential conversation—wished to escape from any
kind of questioning, or from quiet talk, of whatever description it
might be. When at length she did let Monica go back to the drawing-
room, it was not with any idea of silence. She went straight to the
piano, and began playing stormily.
Presently, after dashing off fragments vocal and instrumental in a
sort of confused medley, Monica, growing dreamy as she listened to
the succession of changing harmonies, she began once again with
more of purpose and of passion in her voice—indeed, there was so
much of pain and passion, that Monica was aroused to listen.
“My heart, my heart is like a singing bird
Whose nest is in a watered shoot;
My heart, my heart is like an apple-tree,
Whose boughs are hung with thick-set fruit.
My heart, my heart is like a rainbow-shell
That paddles in a halcyon sea;
My heart, my heart is gladder than all these,
Because my love, my love has come to me.
My heart——”
And then the singer’s voice failed utterly; a dismal discordant chord
broke the eager harmonies that had followed one another so rapidly.
Beatrice broke into a sudden storm of tears, and hurried from the
room without a word.
Monica sat aghast and bewildered. What could it all mean? Was she
by chance to come upon the secret sorrow of Beatrice’s life?—the
sorrow she had half suspected sometimes, but had never heard in
any way explained. Was it to be explained to her now? Was Tom
Pendrill connected with that sorrow? If so, what part had he taken?
Could they ever have been lovers? Did she not remember, long ago,
hearing something of a suspicion on Mrs. Pendrill’s part that Tom
had been “jilted” by the woman he loved? Was there not a time,
long ago, when he was not the reserved, cynical man he affected
now to be; but was genial, brilliant, the pleasantest of companions?
Yes, Monica was sure of it—was certain that he had changed, and
changed somewhat suddenly, many years since; but she had paid
but little heed to the matter then, as it was about that time when
every faculty was absorbed in watching over Arthur, who long lay
hovering between life and death. Changes after that passed almost
unheeded. Had not her whole life been changed too?
She did not follow Beatrice, however, to try and comfort her, or
attempt to force her confidence. She treated her as she would wish
herself to be treated in similar case; and shortly after the gentlemen
had joined them, had the satisfaction of seeing Beatrice come back
as brilliant and full of vivacity as ever, and there was no need after
her appearance, to wonder how the evening should be passed, it
seemed quite sufficient entertainment for the company to sit in a
circle round her, and hear Beatrice talk. Tom Pendrill was the one
exception. He did not attempt to join the magic ring. He took Monica
a little apart, and talked over with her the latest news from
Germany.
When the guests had departed, and Beatrice, as well as her brother
and Monica, had gone upstairs, Tom turned his face towards
Randolph with its hardest and most cynical look.
“Tell you what, Trevlyn, don’t you ask that poor young fellow Radlet
here again, so long as that arrant flirt is a guest under your roof.”
Randolph simply smiled.
“The ‘arrant flirt,’ as you are polite enough to call my guest, is one of
my oldest friends. Kindly keep that fact in mind in talking of her to
me.”
“I am not talking of her. I am talking of poor young Radlet.”
“It seems to me that poor young Radlet, as you call him, is very well
able to take care of himself.”
“Oh, you think that, do you? Shows how much you know! Can’t you
see she was doing her very best to enslave his fancy, and that he
was falling under the spell as fast as ever he could?”
“Pooh! Nonsense!” answered Randolph; “they were just exchanging
a little of the current coin that is constantly passing in gay society.
Young Radlet is not a green-horn. They understand their game
perfectly.”
“She does, of course—no one better; but it’s a question if he does.”
“Well, he’s a greater fool than he looks, if he does not!” answered
Randolph. “Does he expect a girl like Beatrice Wentworth to be
enslaved by his charms in the course of a few hours? The thing’s a
manifest absurdity!”
“Possibly; but that woman can make a man think anything.”
Randolph looked at his friend with some attention.
“You seem to have formed very exhaustive conclusions about Lady
Beatrice Wentworth.”
It almost seemed as if Tom coloured a little as he turned impatiently
away.
Next day Beatrice seemed to have regained her usual even flow of
spirits. She met Tom at breakfast as she would meet any guest
under the same roof, and neither courted nor avoided him in any
way. He seemed to take his cue from her; but his face still wore the
thin-lipped cynical expression that betrayed a certain amount of
subdued irritation. However, sport was the all-prevailing topic of the
hour, and as soon as breakfast was concluded, the men departed,
with the dogs and keepers in their wake.
“What would you like to do, Beatrice?” asked Monica when the
sportsmen had disappeared. “We have the whole day before us.”
“Like to do? Why, everything must be delightful in this lovely out-of-
the-world place. Monica, no wonder you are just yourself—not one
bit like any one else—brought up here with only the sea, and the
clouds, and the sunshine for companions and playmates. I used to
look at you in a sort of wonder, but I understand it all now. You
ought always to live at Trevlyn—never anywhere else. What should I
like to do? Why, anything. Suppose we ride. I should love to gallop
along the cliffs with you. I want to see the queer little church
Haddon described to me, where you were married, and the
picturesque little town where—where Randolph and he put up on the
eve of that day. I want to see everything that belongs to your past
life, Monica. It interests me more than I can express.”
Monica smiled in her tranquil fashion.
“Very well; you shall gratify your wish. I will order the horses at
once. If we go to St. Maws, I ought to go and see Aunt Elizabeth—
Mrs. Pendrill that is, aunt to Arthur, and to Tom Pendrill and his
brother. She is sure to want us to stay to luncheon with her if we do.
She will be all alone; Tom here, and Raymond on his rounds. Would
you dislike that, Beatrice? She is a sweet old lady, and seems more a
part of my past life than anything else I can show you, though I
could not perhaps explain why.”
A curious light shone in Beatrice’s eyes.
“Dislike it! I should like it above everything. I love old ladies. They
are so much more interesting than young ones, I often wish I were
old myself—not middle-aged, you know, but really old, very old, with
lovely white hair, and a waxen face all over tiny wrinkles, like my
own grandmother—the most beautiful woman without exception that
I ever saw. Yes, Monica, let us do that. It will be delightful. Why did
you never mention the Pendrills to me before?”
She put the question with studied carelessness. Yet Monica was
certain it was asked with effort.
“Did I not? I thought I used to tell you so much about my past life.”
“So you did; but I never heard that name.”
“You knew Arthur was a Pendrill.”
“Indeed I did not. He was always Arthur to you. I wonder I never
asked his surname; but somehow I never did. I had a vague idea
that some such people as these Pendrills existed; but I never heard
you name them.”
“Perhaps you heard, and forgot it?” suggested Monica tentatively.
“That I am sure I never did,” was the very emphatic answer.
Beatrice was delighted with her morning’s ride. It was a beautiful
autumn day, and everything was looking its best. The sea flashed
and sparkled in the sunlight; the sky was clear and soft above them,
the horses, delighted to feel the soft turf beneath their feet, pranced
and curvetted and galloped, with that easy elastic motion that is so
peculiarly exhilarating.
The girl herself looked peculiarly and vividly beautiful, and Monica
was not surprised at the affectionate interest Mrs. Pendrill evinced in
her from the first moment of introduction.
But she was a little surprised at the peculiar sweetness of Beatrice’s
demeanour towards the old lady. Whilst retaining all her arch
brightness and vivacity, the girl managed to infuse into her manner,
her voice, and her words something gentle and deferential and
winning that was inexplicably fascinating; all the more so from its
evident unconscious sincerity.
Mrs. Pendrill was charmed with the beauty and sweetness of the girl,
and it seemed as if Beatrice on her side was equally fascinated.
When the time came to say good-bye, and the old lady held both
her hands, and gazed into her bright face, as she asked for another
visit very soon, she stooped suddenly, and kissed her with pretty,
spontaneous warmth.
“Come again! Of course I will, as often as Monica will bring me.
Good-bye, Mrs. Pendrill—Aunt Elizabeth I should like to say”—with a
little rippling laugh. “I think you are just fit to be Monica’s ‘Saint
Elizabeth.’ Is it the air of this place that makes you all so perfectly
delightful? I shall have to come and live here too, I think.”
And as she and Monica rode home together over the sweeping
downs, Beatrice turned to her after a long pause of silence and said:
“Monica, it was a dangerous experiment asking me to Trevlyn.”
“Why?”
“Because I don’t feel as if I should ever want to leave it again. And
I’m a dreadful sort of creature when I’m bent on my own way.”
Monica smiled.
“You will have to turn me out neck and crop in the end, I firmly
believe. I feel I should just take root here, and never wish to go.”
Monica shook her head with a look of subdued amusement.
“I am very glad it pleases you so much; but do you know, Beatrice, I
think you will have a different tale to tell in a week or two? You
cannot realise, till you have tried it, how solitary and isolated we are,
especially as the winter draws on. Very soon you will think it is a
dreadfully lonely place—a sort of enchanted castle, as Randolph
used to call it; and you will be pining to get back to the gay, busy
whirl of life, that you have left behind.”
Monica stopped short there struck by the strange look turned upon
her by her companion. Beatrice’s face had grown grave and almost
pale. A curious wistful sadness shone in her eyes; it almost seemed
as if tears glistened on the long lashes.
Her words were almost as enigmatical as her looks.
She gazed at Monica for a moment speechlessly, and then softly
murmured:
“Et tu Brute!”
END OF VOLUME II.
PRINTED BY KELLY AND CO., GATE STREET, LINCOLN’S INN FIELDS,
AND KINGSTON-ON-THAMES.
Transcriber's Notes
Minor punctuation and printer errors repaired.
*** END OF THE PROJECT GUTENBERG EBOOK MONICA: A NOVEL,
VOLUME 2 (OF 3) ***

Updated editions will replace the previous one—the old editions


will be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States
copyright in these works, so the Foundation (and you!) can copy
and distribute it in the United States without permission and
without paying copyright royalties. Special rules, set forth in the
General Terms of Use part of this license, apply to copying and
distributing Project Gutenberg™ electronic works to protect the
PROJECT GUTENBERG™ concept and trademark. Project
Gutenberg is a registered trademark, and may not be used if
you charge for an eBook, except by following the terms of the
trademark license, including paying royalties for use of the
Project Gutenberg trademark. If you do not charge anything for
copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such
as creation of derivative works, reports, performances and
research. Project Gutenberg eBooks may be modified and
printed and given away—you may do practically ANYTHING in
the United States with eBooks not protected by U.S. copyright
law. Redistribution is subject to the trademark license, especially
commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the


free distribution of electronic works, by using or distributing this
work (or any other work associated in any way with the phrase
“Project Gutenberg”), you agree to comply with all the terms of
the Full Project Gutenberg™ License available with this file or
online at www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand,
agree to and accept all the terms of this license and intellectual
property (trademark/copyright) agreement. If you do not agree
to abide by all the terms of this agreement, you must cease
using and return or destroy all copies of Project Gutenberg™
electronic works in your possession. If you paid a fee for
obtaining a copy of or access to a Project Gutenberg™
electronic work and you do not agree to be bound by the terms
of this agreement, you may obtain a refund from the person or
entity to whom you paid the fee as set forth in paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only


be used on or associated in any way with an electronic work by
people who agree to be bound by the terms of this agreement.
There are a few things that you can do with most Project
Gutenberg™ electronic works even without complying with the
full terms of this agreement. See paragraph 1.C below. There
are a lot of things you can do with Project Gutenberg™
electronic works if you follow the terms of this agreement and
help preserve free future access to Project Gutenberg™
electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright
law in the United States and you are located in the United
States, we do not claim a right to prevent you from copying,
distributing, performing, displaying or creating derivative works
based on the work as long as all references to Project
Gutenberg are removed. Of course, we hope that you will
support the Project Gutenberg™ mission of promoting free
access to electronic works by freely sharing Project Gutenberg™
works in compliance with the terms of this agreement for
keeping the Project Gutenberg™ name associated with the
work. You can easily comply with the terms of this agreement
by keeping this work in the same format with its attached full
Project Gutenberg™ License when you share it without charge
with others.

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. Unless you have removed all references to Project


Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project
Gutenberg™ work (any work on which the phrase “Project
Gutenberg” appears, or with which the phrase “Project
Gutenberg” is associated) is accessed, displayed, performed,
viewed, copied or distributed:

This eBook is for the use of anyone anywhere in the United


States and most other parts of the world at no cost and
with almost no restrictions whatsoever. You may copy it,
give it away or re-use it under the terms of the Project
Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United
States, you will have to check the laws of the country
where you are located before using this eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is


derived from texts not protected by U.S. copyright law (does not
contain a notice indicating that it is posted with permission of
the copyright holder), the work can be copied and distributed to
anyone in the United States without paying any fees or charges.
If you are redistributing or providing access to a work with the
phrase “Project Gutenberg” associated with or appearing on the
work, you must comply either with the requirements of
paragraphs 1.E.1 through 1.E.7 or obtain permission for the use
of the work and the Project Gutenberg™ trademark as set forth
in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is


posted with the permission of the copyright holder, your use and
distribution must comply with both paragraphs 1.E.1 through
1.E.7 and any additional terms imposed by the copyright holder.
Additional terms will be linked to the Project Gutenberg™
License for all works posted with the permission of the copyright
holder found at the beginning of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

textbookfull.com

You might also like