In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Facebook open source one-stop service python timing weapon Kats what is useful, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Time series analysis is a very important field in data science, which mainly includes statistical analysis, detection of change points, anomaly detection and prediction of future trends. However, these time series techniques are usually implemented by different libraries. Is there a way for you to get all these technologies in one library?
The answer is yes, and in this article I will share a great toolkit, Kats, which can solve the above problems perfectly.
What is Kats?
At present, there are many techniques for time series analysis and modeling, but they are relatively scattered. FaceBook open source Kats, which is a lightweight, easy to use, general time series analysis framework, including: prediction, anomaly detection, multivariate analysis and feature extraction and embedding. You can think of Kats as an one-stop toolkit for time series analysis in Python.
Install Katspip install-- upgrade pippip install kats
To understand the functionality of Kats, we will use this framework to analyze the StackOverflow problem counting problem on Kaggle. The data link is: https://www.kaggle.com/aishu200023/stackindex
First, let's start by reading the data.
Import pandas as pddf = pd.read_csv ("MLTollsStackOverflow.csv") # Turn the month column into datetimedf ["month"] = pd.to_datetime (df ["month"], format= "% Ymuri% b") df = df.set_index ("month")
Now let's analyze the count of StackOverflow problems related to Python. The data is divided into a column and a test set to evaluate the prediction.
Python = df ["python"]. To_frame () # Split data into train and test settrain_len = 102train = python.iloc [: train_len] test = python.iloc [train _ len:] convert the data into time series
First, a time series object is constructed. We use time_col_name='month' to specify the time column.
From kats.consts import TimeSeriesData# Construct TimeSeriesData objectts = TimeSeriesData (train.reset_index (), time_col_name= "month")
To draw the data, call the plot method:
Ts.plot (cols= ["python"])
Cool! It seems that the number of questions about Python increases over time. Can we predict the trend in the next 30 days? Yes, we can do it with Kats.
Forecast
Kats currently supports the following 10 prediction models:
Linear
Quadratic
ARIMA
SARIMA
Holt-Winters
Prophet
AR-Net
LSTM
Theta
VAR
There are many of the above models. Let's try two of them.
Start by using Prophet for forecasting: from kats.models.prophet import ProphetModel, ProphetParams# Specify parametersparams = ProphetParams (seasonality_mode= "multiplicative") # Create a model instancem = ProphetModel (ts, params) # Fit modem.fit () # Forecastfcst = m.predict (steps=30, freq= "MS") fcst
Visual m.plot ()
Cool! Let's evaluate the prediction by comparing it with the test data.
Import matplotlib.pyplot as pltfig, ax= plt.subplots (figsize= (12,7)) train.plot (ax=ax, label= "train", color= "black") test.plot (ax=ax, color= "black") fcst.plot (x = "time", y = "fcst", ax=ax, color= "blue") ax.fill_between (test.index, fcst ["fcst_lower"], fcst ["fcst_upper"], alpha=0.5) ax.get_legend (). Remove ()
The forecast seems to be in good agreement with the observation!
Holt-Winters
The next pattern we will try is Holt-Winters. It's a way to capture seasonality. Here is how to use the Holt-Winters method in Kats.
From kats.models.holtwinters import HoltWintersParams, HoltWintersModelimport warningswarnings.simplefilter (action='ignore') params= HoltWintersParams (trend= "add", seasonal= "mul", seasonal_periods=12,) m = HoltWintersModel (data=ts, params=params) m.fit () fcst = m.predict (steps=30, alpha = 0.1) m.plot ()
Detect the change point
Have you ever thought about the time when statistically significant mean changes occur in your time series?
Kats allows the use of the CUSUM algorithm to detect change points. Cusum is a method to detect the up and down shift of the mean in time series.
Let's take a look at how to detect change points in Kats.
From kats.consts import TimeSeriesData, TimeSeriesIteratorfrom kats.detectors.cusum_detection import CUSUMDetectorimport matplotlib.pyplot as pltdetector = CUSUMDetector (ts) change_points = detector.detector (change_directions= ["increase", "decrease"]) print ("The change point is on", change_points [0] [0] .start _ time) # plot the resultsplt.xticks (rotation=45) detector.plot (change_points) plt.show ()
Cool! Let's try to detect other categories of changes in the StackOverflow problem count.
First create a function to detect the change points provided by the theme.
Def get_ts (topic: str): return TimeSeriesData (DF [topic]. To _ frame (). Reset_index (), time_col_name= "month") def detect_change_point (topic: str): ts = get_ts (topic) detector = CUSUMDetector (ts) change_points = detector.detector () for change_point in change_points: print ("The change point is on") Change_point [0] .start _ time) # plot the results plt.xticks (rotation=45) detector.plot (change_points) plt.show () Machine Learning detect_change_point ("machine-learning")
Deep learning detect_change_point ("deep-learning")
Outlier detection
What do you see when you look at NLP's time series?
Df ["nlp"] .plot ()
The number of NLP problems declined from 2018 to 2019.
The decline in the number of problems is an outlier. It is important to detect abnormal values because they can cause problems in downstream processing.
However, it is not always efficient and easy to find outliers by looking at the data. Fortunately, Kats also allows you to detect outliers in time series!
It takes only a few lines of code to detect abnormal values with kat.
From kats.detectors.outlier import OutlierDetector# Get time series objectts = get_ts ("nlp") # Detect outliersts_outlierDetection = OutlierDetector (ts, "additive") ts_outlierDetection.detector () # Print outliersoutlier_range1 = ts_outlierDetection.outliers [0] print (f "The outliers range from {outlier_range1 [0]} to {outlier_range1 [1]}")
The outliers range from 2018-01-01 00:00:00 to 2019-03-01 00:00:00
Cool! The results confirm what we see from the picture above.
Time series feature
In addition to statistics, there are other features in the time series, such as linearity, trend intensity, seasonal intensity, seasonal parameters, and so on, which may be of interest to you.
Kats allows you to find important information about the characteristics of a time series through TsFeatures:
From kats.tsfeatures.tsfeatures import TsFeaturesmodel = TsFeatures () output_features = model.transform (ts) output_features
Thank you for reading this article carefully. I hope the article "what is the use of Facebook open source one-stop service python time sequence weapon Kats" shared by the editor is helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.