Академический Документы
Профессиональный Документы
Культура Документы
Time Series Algorithms Recipes Implement Machine Learning and Deep Learning Techniques With Python Akshay R Kulkarni Ebook Full Chapter
Time Series Algorithms Recipes Implement Machine Learning and Deep Learning Techniques With Python Akshay R Kulkarni Ebook Full Chapter
Time Series Algorithms Recipes Implement Machine Learning and Deep Learning Techniques With Python Akshay R Kulkarni Ebook Full Chapter
Akshay R Kulkarni
Adarsha Shivananda
Anoosh Kulkarni
V Adithya Krishnan
Time Series Algorithms Recipes: Implement Machine Learning and Deep
Learning Techniques with Python
Akshay R Kulkarni Adarsha Shivananda
Bangalore, Karnataka, India Hosanagara, Karnataka, India
Preface�����������������������������������������������������������������������������������������������xv
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
viii
Table of Contents
ix
Table of Contents
Index�������������������������������������������������������������������������������������������������169
x
About the Authors
Akshay R Kulkarni is an artificial intelligence (AI) and machine learning (ML)
evangelist and thought leader. He has consulted several Fortune 500 and
global enterprises to drive AI and data science–led strategic
transformations. He is a Google developer, an author, and a regular
speaker at major AI and data science conferences (including the O’Reilly
Strata Data & AI Conference and Great Indian Developer Summit (GIDS)).
He is a visiting faculty member at some of the top graduate institutes in
India. In 2019, he was featured as one of India’s “top 40 under 40” data
scientists. In his spare time, Akshay enjoys reading, writing, coding, and
helping aspiring data scientists. He lives in Bangalore with his family.
xii
About the Technical Reviewer
Krishnendu Dasgupta is a co-founder of DOCONVID AI. He is a computer
science and engineering graduate with a decade of experience building
solutions and platforms on applied machine learning. He has worked
with NTT DATA, PwC, and Thoucentric and is now working on applied
AI research in medical imaging and decentralized privacy-preserving
machine learning in healthcare. Krishnendu is an alumnus of the MIT
Entrepreneurship and Innovation Bootcamp and devotes his free time as
an applied AI and ML research volunteer for various research NGOs and
universities across the world.
xiii
Preface
Before reading this book, you should have a basic knowledge of statistics,
machine learning, and Python programming. If you want to learn how to
build basic to advanced time series forecasting models, then this book
will help by providing recipes for implementation in Python. By the end
of the book, you will have practical knowledge of all the different types of
modeling methods in time series.
The desire to know the unknown and to predict the future has been
part of human culture for ages. This desire has driven mankind toward
the discipline of forecasting. Time series forecasting predicts unknown
future data points based on the data's previous (past) observed pattern. It
can depend not only on the previous target points and time (univariate)
but also on other independent variables (multivariate). This book is a
cookbook containing various recipes to handle time series forecasting.
Data scientists starting a new time series project but don’t have prior
experience in this domain can easily utilize the various recipes in this
book, which are domain agnostic, to kick-start and ease their development
process.
This book is divided into five chapters. Chapter 1 covers recipes for
reading and processing the time series data and basic Exploratory Data
Analysis (EDA). The following three chapters cover various forecasting
modeling techniques for univariate and multivariate datasets. Chapter 2
has recipes for multiple statistical univariate forecasting methods, with
more advanced techniques continued in Chapter 3. Chapter 3 also covers
statistical multivariate methods. Chapter 4 covers time series forecasting
using machine learning (regression-based). Chapter 5 is on advanced time
series modeling methods using deep learning.
xv
Preface
The code for all the implementations in each chapter and the required
datasets is available for download at github.com/apress/time-series-
algorithm-recipes.
xvi
CHAPTER 1
Getting Started
with Time Series
A time series is a sequence of time-dependent data points. For example,
the demand (or sales) for a product in an e-commerce website can be
measured temporally in a time series, where the demand (or sales)
is ordered according to the time. This data can then be analyzed to
find critical temporal insights and forecast future values, which helps
businesses plan and increase revenue.
Time series data is used in every domain where real-time analytics is
essential. Analyzing this data and forecasting its future value has become
essential to these domains.
Time series analysis/forecasting was previously considered a purely
statistical problem. It is now used in many machine learning and deep
learning–based solutions, which perform equally well or even outperform
most other solutions. This book uses various methods and approaches to
analyze and forecast time series.
This chapter uses recipes to read/write time series data and perform
simple preprocessing and Exploratory Data Analysis (EDA).
The following lists the recipes explored in this chapter.
Solution
Pandas load the data into a dataframe structure.
How It Works
The following steps read the data.
2
Chapter 1 Getting Started with Time Series
The following are some of the important input arguments for read_csv.
Solution
Save the dataframe as a comma-separated (CSV) file.
How It Works
The following steps read the data.
4
Chapter 1 Getting Started with Time Series
5
Chapter 1 Getting Started with Time Series
Solution
Save the dataframes as a CSV file.
How It Works
The following steps store the data.
### Check
print(data1.head(2).T)
6
Chapter 1 Getting Started with Time Series
1981-01-01
1981-01-02 17.9
1981-01-03 18.8
Name: 20.7, dtype: float64
Solution
A univariate time series is data with a single time-dependent variable.
Let’s look at a sample dataset of the monthly minimum temperatures
in the Southern Hemisphere from 1981 to 1990. The temperature is the
time-dependent target variable.
How It Works
The following steps read and plot the univariate data.
7
Chapter 1 Getting Started with Time Series
Date
1981-01-01 20.7
1981-01-02 17.9
1981-01-03 18.8
1981-01-04 14.6
1981-01-05 15.8
Name: Temp, dtype: float64
data.plot()
plt.ylabel('Minimum Temp')
plt.title('Min temp in Southern Hemisphere From 1981 to 1990')
plt.show()
8
Chapter 1 Getting Started with Time Series
This is called univariate time series analysis since only one variable,
temp (the temperature over the past 19 years), was used.
Solution
A multivariate time series is a type of time series data with more features
that the target depends on, which are also time-dependent; that is, the
target is not only dependent on its past values. This relationship is used to
forecast the target values.
9
Chapter 1 Getting Started with Time Series
How It Works
The following steps read and plot the multivariate data.
def parse(x):
return datetime.strptime(x, '%Y %m %d %H')
10
Chapter 1 Getting Started with Time Series
data1['pollution'].fillna(0, inplace=True)
data1 = data1[24:]
print(data1.head(5))
11
Chapter 1 Getting Started with Time Series
vals = data1.values
group_list = [0, 1, 2, 3, 5, 6, 7]
i = 1
plt.show()
12
Chapter 1 Getting Started with Time Series
Solution
A trend is the overall movement of data in a particular direction—that is,
the values going upward (increasing) or downward (decreasing) over a
period of time.
Let’s use a shampoo sales dataset, which has a monthly sales count for
three years.
How It Works
The following steps read and plot the data.
13
Chapter 1 Getting Started with Time Series
14
Another random document with
no related content on Scribd:
Photo by Mr. Charles Temple.
MAPS
Map of Southern Nigeria 46
” Northern Nigeria 92
And, thinking over this personal side of the matter as one jogs
along up hill and down dale, through plain and valley and mountain
side, through lands of plenty and lands of desolation, past carefully
fenced-in fields of cotton and cassava, past the crumbling ruins of
deserted habitations, along the great white dusty road through the
heart of Hausaland, along the tortuous mountain track to the pagan
stronghold, there keeps on murmuring in one’s brain the refrain:
“How is it done? How is it done?” Ten years ago, nay, but six, neither
property nor life were safe. The peasant fled to the hills, or hurried at
nightfall within the sheltering walls of the town. Now he is
descending from the hills and abandoning the towns.
And the answer forced upon one, by one’s own observations, is
that the incredible has been wrought, primarily and fundamentally,
not by this or that brilliant feat of arms, not by Britain’s might or
Britain’s wealth, but by a handful of quiet men, enthusiastic in their
appreciation of the opportunity, strong in their sense of duty, keen in
their sense of right, firm in their sense of justice, who, working in an
independence, and with a personal responsibility in respect to which,
probably, no country now under the British flag can offer a parallel,
whose deeds are unsung, and whose very names are unknown to
their countrymen, have shown, and are every day showing, that, with
all her faults, Britain does still breed sons worthy of the highest
traditions of the race.
CHAPTER II
ON THE GREAT WHITE ROAD
You may fairly call it the Great White Road to Hausaland, although
it does degenerate in places into a mere track where it pierces some
belt of shea-wood or mixed trees, and you are reduced to Indian file.
But elsewhere it merits its appellation, and it glimmers ghostly in the
moonlight as it cuts the plain, cultivated to its very edge with guinea-
corn and millet, cassava and cotton, beans and pepper. And you
might add the adjective, dusty, to it. For dusty at this season of the
year it certainly is. Dusty beyond imagination. Surely there is no dust
like this dust as it sweeps up at you, impelled by the harmattan
blowing from the north, into your eyes and mouth and nose and hair?
Dust composed of unutterable things. Dust which countless bare
human feet have tramped for months. Dust mingled with the manure
of thousands of oxen, horses, sheep and goats. Dust which converts
the glossy skin of the African into an unattractive drab, but which
cannot impair his cheerfulness withal. Dust which eats its way into
your boxes, and defies the brush applied to your clothes, and finds
its way into your soup and all things edible and non-edible. Dust
which gets between you and the sun, and spoils your view of the
country, wrapping everything in a milky haze which distorts distances
and lies thick upon the foliage. The morning up to nine, say, will be
glorious and clear and crisp, and then, sure enough, as you halt for
breakfast and with sharpened appetite await the looked-for “chop,” a
puff of wind will spring up from nowhere and in its train will come the
dust. The haze descends and for the rest of the day King Dust will
reign supreme. It is responsible for much sickness, this Sahara dust,
of that my African friends and myself are equally convinced. You may
see the turbaned members of the party draw the lower end of that
useful article of apparel right across the face up to the eyes when the
wind begins to blow. The characteristic litham of the Tuareg, the men
of the desert, may have had its origin in the necessity, taught by
experience, of keeping the dust out of nose and mouth. I have been
told by an officer of much Northern Nigerian experience, that that
terrible disease, known as cerebro-spinal meningitis, whose
characteristic feature is inflammation of the membranes of the brain,
and which appears in epidemic form out here, is aggravated, if not
induced, in his opinion—and he assures me in the opinion of many
natives he has consulted—by this disease-carrying dust. In every
town and village in the Northern Hausa States, you will see various
diseases of the eye lamentably rife, and here, I am inclined to think,
King Dust also plays an active and discreditable part.
A GROUP OF TUAREGS.
A BORNU OX.
The Great White Road. It thoroughly deserves that title from the
point where one enters the Kano Province coming from Zaria. It is
there not only a great white road but a very fine one, bordered on
either side by a species of eucalyptus, and easily capable, so far as
breadth is concerned, of allowing the passage of two large
automobiles abreast. I, personally, should not care to own the
automobile which undertook the journey, because the road is not
exactly what we would call up-to-date. Thank Heaven that there is
one part of the world, at least, to be found where neither roads, nor
ladies’ costumes are “up-to-date.” If the Native Administration of the
Kano Emirate had nothing else to be commended for, and under the
tactful guidance of successive Residents it has an increasing
account to its credit, the traveller would bear it in grateful recollection
for its preservation of the trees in the immediate vicinity of, and
sometimes actually on the Great White Road itself. It is difficult to
over-estimate the value to man and beast, to the hot and dusty
European, to the weary-footed carrier, to the patient pack-ox, and
cruelly-bitted native horse, of the occasional shady tree at the edge
of or on the road. And what magnificent specimens of the vegetable
kingdom the fertile soil of Kano Province does carry—our New
Forest giants, though holding their own for beauty and shape and, of
course, clinging about our hearts with all their wealth of historical
memories and inherited familiarity, would look puny in comparison.
With one exception I do not think anything on the adverse side of
trivialities has struck me more forcibly out here than the insane
passion for destroying trees which seems to animate humanity,
White and Black. In many parts of the country I have passed through
the African does appear to appreciate his trees, both as shade for his
ordinary crops and special crops (such as pepper, for instance,
which you generally find planted under a great tree) and cattle. In
Kano Province, for instance, this is very noticeable. But in other parts
he will burn down his trees, or rather let them burn down, with
absolute equanimity, making no effort to protect them (which on
many occasions he could easily do) when he fires the grasses
(which, pace many learned persons, it seems to me, he is compelled
by his agricultural needs to do—I speak now of the regions I have
seen). I have noticed quantities of splendid and valuable timber
ruined in this way. The European—I should say some Europeans—
appears to suffer from the same complaint. It is the fashion—if the
word be not disrespectful, and Heaven forfend that the doctors
should be spoken of disrespectfully in this part of the world, of all
places—among the new school of tropical medicine out here to
condemn all growing things in a wholesale manner. In the eyes of
some, trees or plants of any kind in the vicinity of a European station
are ruthlessly condemned. Others are specially incensed against low
shrubs. Some are even known to pronounce the death-warrant of the
pine-apple, and I met an official at a place, which shall be nameless,
who went near weeping tears of distress over a fine row of this fruit
which he had himself planted, and which were threatened, as he put
it, by the ferocity of the local medical man. In another place
destruction hangs over a magnificent row of mango trees—and for
beauty and luxuriousness of foliage the mango tree is hard to beat—
planted many years ago by the Roman Catholic Fathers near one of
their mission stations; and in still another, an official, recently
returned on leave, found to his disgust that a group of trees he
especially valued had been cut down during his absence by a
zealous reformer of the medical world.
Each twenty-four hours brings its own series of events and its own
train of thoughts following upon them. A new incident, it may be of
the most trivial kind, sets the mind working like an alarum; a petty
act, a passing word, have in them revealing depths of character.
Nature seems such an open book here. She does not hide her
secrets. She displays them; which means that she has none; and, in
consequence, that she is as she was meant to be, moral. The
trappings of hide-bound convention do not trammel her every stride
like the hobble skirts of the foolish women who parade their shapes
along the fashionable thoroughfares of London. What quagmires of
error we sink into when we weigh out our ideas of morality to the
African standard—such a very low one it is said.
Well, I have covered a good deal of ground in this country—
although I have not been in it very long, measured in time—and I
have seen many thousands of human beings. I have seen the Hausa
woman and the bush Fulani woman in their classical robes. I have
seen the Yoruba woman bathing in the Ogun, clad only in the natural
clothing of her own dusky skin. I have seen the scantily-attired
Gwarri and Ibo woman, and the woman of the Bauchi highlands with
her bunch of broad green leaves “behind and before,” and nothing
else, save a bundle of wood or load of sorts on her head, or a hoe in
her hand. I have visited many African homes, sometimes
announced, sometimes not, at all hours of the day, and sometimes of
the night. I have passed the people on the beaten track, and sought
and found them off the beaten track. I have yet to see outside our
cantonments—where the wastrels drift—a single immodest gesture
on the part of man or woman. Humanity which is of Nature is, as
Nature herself, moral. There is no immodesty in nakedness which
“knows not that it is naked.” The Kukuruku girl, whose only garment
is a single string of beads round neck and waist, is more modest
than your Bond Street dame clad in the prevailing fashion,
suggesting nakedness. Break up the family life of Africa, undermine