Moving window with Gaussian shape over time series data in Python - python-3.x

I shortly started with Python, Pandas, Numpy, etc. for my master thesis. It concerns about time series (sensor) data to be fed into an LSTM (multi-class classification problem). The current dataset represents a dataframe with 32 features (columns).
My goal:
I want to verify the influence of moving windows and other methods on the LSTM's model performance. For this I already implemented a moving window method (median) with a size of 10 and exponential smoothing on the whole trace:
#Window size: 10
df_Median = df.rolling(10).median()
#application on whole trace
df_EWM_mean = df.ewm(alpha=0.2).mean()
My challenge:
The third method should be a moving window with a gaussian shape (Gauss kernel). I implemented it according to the Pandas documentation:
Pandas.DataFrame.rolling
for header, content in df.iteritems():
stdDev = np.std(content)
if stdDev != 0:
win_len = 10
df_Gauss = df.loc[:,header].rolling(win_len, win_type='gaussian').mean(std=stdDev)
This actually works. But the Standard Deviation argument stdDev must be calculated by the 10 current data points within the window, not from the whole trace... So I'd have to select the 10 current data points, calculate the stdDev, hand it over to the function, shift the window 10 points to the right and start from beginning...
How could this be accomplished? I'm quite new to Python, Pandas etc.
So I'd be very grateful for hints.
Many thanks in advance.

Related

Does fitting Weibull distribution to data using scipy.stats perform poor?

I am working on fitting Weibull distribution on some integer data and estimating relevant shape, scale, location parameters. However, I noticed poor performance of scipy.stats library while doing so.
So, I took a different direction and checked the fit performance by using the code below. I first create 100 numbers using Weibull distribution with parameters shape=3, scale=200, location=1. Subsequently, I estimate the best distribution fit using fitter library.
from fitter import Fitter
import numpy as np
from scipy.stats import weibull_min
# generate numbers
x = weibull_min.rvs(3, scale=200, loc=1, size=100)
# make them integers
data = np.asarray(x, dtype=int)
# fit one of the four distributions
f = Fitter(data, distributions=["gamma", "rayleigh", "uniform", "weibull_min"])
f.fit()
f.summary()
I expect the best fit to be Weibull distribution. I have tried re-running this test. Sometimes Weibull fit is a good estimate. However, most of the time Weibull fit is reported as the worst result. In this case, the estimated parameters are = (0.13836651040093312, 66.99999999999999, 1.3200752378443505). I assume these parameters correspond to shape, scale, location in order. Below is the summary of the fit procedure.
$ f.summary()
sumsquare_error aic bic kl_div
gamma 0.001601 1182.739756 -1090.410631 inf
rayleigh 0.001819 1154.204133 -1082.276256 inf
uniform 0.002241 1113.815217 -1061.400668 inf
weibull_min 0.004992 1558.203041 -976.698452 inf
Additionally, the following plot is produced.
Also, Rayleigh distribution is a special case of Weibull with shape parameter = 2. So, I expect the resulting Weibull fit to be at least as good as Rayleigh.
Update
I ran the tests above on Linux/Ubuntu 20.04 machine with numpy version 1.19.2 and scipy version 1.5.2. The code above seems to run as expected and return proper results for Weibull distribution on a Mac machine.
I have also tested fitting a Weibull distribution on data x generated above on the Linux machine by using an R library fitdistrplus as:
fit.weib <- fitdist(x, "weibull")
and observed that the estimated shape and scale values are found to be very close to the initially given values. The best guess so far is that the problem is due to some Python-Ubuntu bug/incompatibility.
I can be considered as a newbie in this area. So, I am wondering, am I doing something wrong here? Or is this result somehow expected? Any help is greatly appreciated.
Thank you.
Library fitter doesn't allow to specify parameters for distributions such as a, loc, etc. And strangely, Mac produces better fit while Linux heavily pains the results for best fit, for the same version of Numpy and Scipy. Underlying reasons may include different BLAS-LAPACK algorithms designed for Linux and Mac, https://stackoverflow.com/a/49274049/6806531, or weibull_min may not initialize parameter a = 1 which is discussed online, or default floating-point accuracy. However, one can solve the error inside fitter library. Knowing the fact that weib_min is expon_weib with parameter a is fixed as 1, changing the run function inside of _timed_run function in fitter.py as
def run(self):
try:
if distribution == "exponweib":
self.result = func(args,floc=0,fa = 1, **kwargs)
else:
self.result = func(args, floc=0, **kwargs)
except Exception as err:
self.exc_info = sys.exc_info()
and using exponweib as weib_min gives nearly same results as R fitdist.
I am not familiar with the Fitter library, but in order to draw some conclusions I would suggest:
Retry your code, but by taking size=10,000. In this case, there are sufficient datapoints for the fitting methods to utilize. Theoretically, you would then expect the Weibull to deliver the best fit.
I noticed that the location parameter can sometimes be a pain. You could try to run your fits by fixing the location parameter with floc=1 (i.e. equal to your sampling parameter for location). What do you get? Aditionally, FYI, with MLE, it suffices to take loc=min(x), where x is your dataset. For the exponential distribution, this in fact the MLE of the location parameter. For other distributions I am not sure, but I wouldn't be surprised if this holds for other distributions as well. This would reduce the fitting procedure with 1 parameter.
Lastly, I noticed that if you take small values for location/scale/shape for some distributions, the functions logpdf and logcdf of scipy.stats distributions result in np.inf values. In this scenario, you could perhaps use the Powell optimization algorithm and set bounds on the values of your parameters.

Improving accuracy of nearest neighbours algorithm - unsupervised learning problem

I have a situation where I am trying to find out 3 nearest neighbours for a given ID in my dataframe. I am using NN alogrithm (not KNN) to achieve this. The below code is giving me the three nearest neighbours, for the top node the results are fine but for the middle ones and the bottom ones the accuracy is only 1/3 neighbours are correct whereas I am eyeing to have atleast 2/3 neighours correct at every ID. My dataset has 47 features and 5000 points.
from sklearn.neighbors import KDTree
def findsuccess(sso_id):
neighbors_f_sso_id = np.where(nbrs.kneighbors_graph([X[i]]))[0]
print('Neighbors of id', neighbors_f_sso_id)
kdt = KDTree(X, leaf_size=40, metric='euclidean')
kdt.query(X, k=4, return_distance=False)
The above code will return the ID itself and the 3 nearest neighbours ,hence k=4
I have read that due to curse of dimensionality, this NN algorithm might not work well as there are about 47 features in my dataset but this is the only option I think I have when it comes to a data frame without a target variable. There is one article available here that says the KD Tree is not best of the algorithms that can be used.
What would be the best way to achieve the maximum accuracy, meaning achieving minimum distance?
Do I need to perform scaling before passing into KD Tree algorithm? Any other things that I need to take care off?

Using scipy.stats.entropy on gmm.predict_proba() values

Background so I don't throw out an XY problem -- I'm trying to check the goodness of fit of a GMM because I want statistical back-up for why I'm choosing the number of clusters I've chosen to group these samples. I'm checking AIC, BIC, entropy, and root mean squared error. This question is about entropy.
I've used kmeans to cluster a bunch of samples, and I want an entropy greater than 0.9 (stats and psychology are not my expertise and this problem is both). I have 59 samples; each sample has 3 features in it. I look for the best covariance type via
for cv_type in cv_types:
for n_components in n_components_range:
# Fit a Gaussian mixture with EM
gmm = mixture.GaussianMixture(n_components=n_components,
covariance_type=cv_type)
gmm.fit(data3)
where the n_components_range is just [2] (later I'll check 2 through 5).
Then I take the GMM with the lowest AIC or BIC, saved as best_eitherAB, (not shown) of the four. I want to see if the label assignments of the predictions are stable across time (I want to run for 1000 iterations), so I know I then need to calculate the entropy, which needs class assignment probabilities. So I predict the probabilities of the class assignment via gmm's method,
probabilities = best_eitherAB.predict_proba(data3)
all_probabilities.append(probabilities)
After all the iterations, I have an array of 1000 arrays, each contains 59 rows (sample size) by 2 columns (for the 2 classes). Each inner row of two sums to 1 to make the probability.
Now, I'm not entirely sure what to do regarding the entropy. I can just feed the whole thing into scipy.stats.entropy,
entr = scipy.stats.entropy(all_probabilities)
and it spits out numbers - as many samples as I have, I get a 2 item numpy matrix for each. I could feed just one of the 1000 tests in and just get 1 small matrix of two items; or I could feed in just a single column and get a single values back. But I don't know what this is, and the numbers are between 1 and 3.
So my questions are -- am I totally misunderstanding how I can use scipy.stats.entropy to calculate the stability of my classes? If I'm not, what's the best way to find a single number entropy that tells me how good my model selection is?

Finding the second peak in two normally distributed data

I am fitting 2 gaussians to a data. The fitting requires a guessed value of the centers of the peaks as start parameters. I managed to get the first max using the following (the gaussian is defined by 'x' and 'y' in my data):
maxd = max(y) #peak
center1 = x[y.argmax(maxd)] #guess of center 1
I am now trying to get the guess of the second peak (center 2) for input in my fitting. Any idea to do that?
The thing is that I could do it one-by-one by eye but I am doing fitting in batch for several data structures. So, I need an automated way.enter image description here
Thanks!
Rajeev
Cheers,
Rajeev

Get average values in some specific range based on spatial analysis with QGIS

I'm working on QGIS to compute average values attached to polygons
around a line.
What I'd like to do is compute the average values of polygon data within a user defined range from a line.
How would I go about doing this?
I have attached a picture below for reference:
There should be a couple ways of doing this. The easiest might be the answer in this question, and then using the answer to this question.
But, you should be able to do this with a little bit of python. I've written a little script that uses just one layer (a point layer that finds the average of points within a certain radius, it averages a field named 'cost'):
# get selected feature
layer = iface.activeLayer()
features = layer.selectedFeatures()
# Buffer the feature 10 layer units
buffer = features[0].geometry().buffer(10,-1)
# Will hold features intersecting buffer
inBuffer = []
# get selected features
for feature in layer.getFeatures():
if (feature.geometry().intersects(buffer)):
inBuffer.append(feature)
# for calculating the average
total = 0
number = 0
# Sum the features that intersect the buffer of the selection:
field = layer.fieldNameIndex('cost')
for feature in inBuffer:
total += (feature['cost'])
number += 1
#Get the average
average = float(total / number)
print (average)
This takes only the first selected feature (features[0]) and applies the search radius to that, this limitation is irrelevant if you only select on feature in the active layer
The code above can be compressed a fair bit, but I thought I'd break it out a little more. Especially as my python is fairly limited.
To find an average in a second layer based on the selection in a first layer, you could modify this slightly by grabbing all layers:
mapcanvas = iface.mapCanvas()
layers = mapcanvas.layers()
then using layers[0], layers[1] (or layers[i]) in place of layer at the appropriate places, something like:
features = layer.selectedFeatures() to features = layers[0].selectedFeatures()
for the source feature, and
for feature in layer.getFeatures(): to for feature in layers[1].getFeatures()
and
field = layer.fieldNameIndex('fieldname') to field = layers[1].fieldNameIndex('fieldname')
for the target feature (the one being averaged).
Hopefully the code I have posted is easy enough to apply. I would probably ensure that both layers use the same SRS to avoid any issues with the intersection, and remember that the buffer units are in the units of that SRS.

Resources