Answer the question
In order to leave comments, you need to log in
How to logically split prices into 5 ranges if prices are greater than 5?
Good day everyone! I can't solve the problem.
There is a sorted list of prices, it needs to be divided into 5 ranges (from 600 to ..., from ... to etc.)
prices = [600, 1000, 4500, 5000, 6500, 7000, 7500, 10000]
amount = 5
legend = *range of 5 prices*
PS
Used two options. One of them is if we divide the number of prices by amount , we will determine the step for the range. But the option did not fit, it can not always group correctly.
Answer the question
In order to leave comments, you need to log in
But the option did not fit, it can not always group correctly.
What does "correctly" mean?
while, from the initial data, it is asked to simply take the maximum price (10000), divide it by the number of ranges (amount) - we get a step
In this example, we get 10000/5 = 2000
[0, 2000], [2001, 4000], [4001 , 6000], [6001, 8000], [8001, 10000]
Now, as you need, shove silt into arrays according to the lists:
1. 600, 1000,
2.
3. 4500, 5000,
4. 6500, 7000, 7500,
5 .10000
But the option didn’t fit, it can’t always group correctly
Clustering algorithms such as KMeans solve similar problems.
# lang: Python 3.5
import numpy as np
from sklearn.cluster import KMeans
cl = KMeans(n_clusters=5)
labels = cl.fit_predict(np.array([[600], [1000], [4500], [5000], [6500], [7000], [7500], [10000]))
#=> [0 0 3 3 1 1 4 2]
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question