user7366076
user7366076

Reputation:

using python how to convert numeric data in ordinal?

data :

118.6
109.7
126.7
107.8
113.9
109.7
109.7
98.2
112.3
153.7
157.8
85
126.7
125.1
155.4
138.5
154.6
189.9
120.6
101.6
128.7
138.5
210.8
124.4
189.8
122.5
161.7
188.6
229.1
168.7
233.7
137.5
126.6
244.4
141.9
227.5
183
177.6
244.4
95.1
116.4
75.9
75.3
109.8
117.1
75.9
109.8
71.2
71.3
89.6
93.3
84.7
85
82.9
145.3
107.7
84.2
96.7
89.8
86.2
85
89.6
67.5
64.9
48.1
54.9
56.1
60.6
51
44.6
64.3
57.6
66.2
69
60
70.2
65.4
60.1
49.4
61.4
62.8
78.8
70.3
82.7
68.6

I want to convert this numeric data in to ordinal .

Example :
if data values are comes in 60 to 69.9 then it will show me 1.

if data values are comes in 70 to 79.9 then it will show me 2.

if data values are comes in 80 to 89.9 then it will show me 3.

if data values are comes in 90 to 99.9 then it will show me 4. so on.

I know how to binarize data, using binaryX = binarizer.transform(X) but i don't know how to convert numeric interval values in single ordinal value.

Upvotes: 0

Views: 1286

Answers (1)

Nils Werner
Nils Werner

Reputation: 36765

What about just dividing by 10, and subtracting the offset?

data = ['60', '69.9', '70', '73', '80']
[int((float(a) // 10) - 5) for a in data]                          # [1, 1, 2, 2, 3]

or, if you are using NumPy

((numpy.array([float(a) for a in data]) // 10) - 5).astype(int)    # [1 1 2 2 3]

Upvotes: 3

Related Questions