I have few weeks data with units sold given
xs[weeks] = [1,2,3,4]
ys['Units Sold'] = [1043,6582,5452,7571]
from the given series, we can see that although there is a drop from xs[2] to xs[3] but overall the trend is increasing. How to detect the trend in small time series dataset.
Is finding a slope for the line is the best way? And how to calculate slope angle of a line in python?
The end-count must be greater than the start-count for an increasing trend. The end-count must be less than the start-count for a decreasing trend.
The easiest way to spot the Trend is to look at the months that hold the same position in each set of three period patterns. For example, month 1 is the first month in the pattern, as is month 4. The sales in month 4 are higher than in month 1.
Regression methods are commonly used to test for trend. When reporting a test for trend, we usually list each category of the risk factor and the strength of the effect (i.e., odds ratio) of each category on the outcome compared with the reference level, the p value at each level, and additionally the ptrend.
I have gone through the same issue that you face today. In order to detect the trend, I couldn't find a specific function to handle the situation.
I found a really helpful function ie, numpy.polyfit()
:
numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)
[Check this Official Documentation]
You can use the function like this
def trenddetector(list_of_index, array_of_data, order=1):
result = np.polyfit(list_of_index, list(array_of_data), order)
slope = result[-2]
return float(slope)
This function returns a float value that indicates the trend of your data and also you can analyze it by something like this.
For example,
if the slope is a +ve value --> increasing trend
if the slope is a -ve value --> decreasing trend
if the slope is a zero value --> No trend
Play with this function and find out the correct threshold as per your problem and give it as a condition.
Example Code for your Solution
import numpy as np
def trendline(index,data, order=1):
coeffs = np.polyfit(index, list(data), order)
slope = coeffs[-2]
return float(slope)
index=[1,2,3,4]
List=[1043,6582,5452,7571]
resultent=trendline(index,List)
print(resultent)
RESULT
1845.3999999999999
As per this output, The result is much greater than zero so it shows your data is increasing steadily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With