A
A
Anton2018-02-05 10:40:42
Mathematics
Anton, 2018-02-05 10:40:42

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

5 answer(s)
A
Antony, 2018-02-05
@RiseOfDeath

But the option did not fit, it can not always group correctly.

So I want to answer "42".
Judging by the formulation of the question, you yourself do not know "how to" group correctly. First, decide for yourself what a "correctly sorted result" looks like. to begin with in particular cases, then in a general form (algorithmically).

D
Dmitry Eremin, 2018-02-05
@EreminD

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

R
rustler2000, 2018-02-05
@rustler2000

But the option didn’t fit, it can’t always group correctly

Named all the letters - could not read the word :D

A
Anton Zhilin, 2018-02-07
@Anton3

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]

Sort clusters in ascending order is no longer difficult.
I suggest you find the implementation of KMeans for your language yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question