-1

I have a list of integer ranging from -3 to 3.

list = [-0.33, -2.5, 2.1, ......., -1.2, -2.4444, 1.788]

Is there a way to convert all the numbers between 0 to 1?

Chris_007
  • 829
  • 11
  • 29
  • 1
    Assuming your list is `data`, use `lo, hi = min(data), max(data); print ([(i - lo) / (hi-lo) for i in data])`. Don't have to get fancy with `numpy` or `sklearn`. – Henry Yik Jul 17 '20 at 07:17
  • This question explicitly states the input range to be -3 to 3. The claim that it is a duplicate, @Henry’s comment, and @Divyessh’s answer are all incorrect as they normalize to a range of it’s min and max values. Based on the wording of the question, this is not what is being asked. – Liam Jul 17 '20 at 07:59
  • OP is asking `Is there a way to convert all the numbers between 0 to 1`. I don't see how it is incorrect to normalize it @Liam. – Henry Yik Jul 17 '20 at 08:07
  • @HenryYik I interpret “ranging from -3 to 3” to mean “map a range of (-3 to 3) to a range of (0 to 1). I think that’s the most reasonable interpretation and I doubt OP would have mentioned the range of the input unless this is what was intended. Can you please clarify, @Chris_007? – Liam Jul 17 '20 at 08:17

2 Answers2

0
>>> from sklearn.preprocessing import StandardScaler
>>> data = [[0, 0, 0, 0, 1, 1, 1, 1]]
>>> scaler = StandardScaler()
>>> print(scaler.fit(data))
StandardScaler()
>>> print(scaler.mean_)
[0.5 0.5]
>>> print(scaler.transform(data))

this shall convert the data into -1 to 1

Dharman
  • 30,962
  • 25
  • 85
  • 135
Divyessh
  • 2,540
  • 1
  • 7
  • 24
-1

Add three to each, then divide by six.

lst = [-0.33, -2.5, 2.1, ......., -1.2, -2.4444, 1.788]
lst = [(i + 3) / 6 for i in lst]

(I renamed list to lst, as the former conflicts with the preexisting list type)

Liam
  • 317
  • 1
  • 11