0

I'm trying to normalize an array of values, but when I put it in the numpy array, it automatically converts the values to integers. Code is below:

import numpy as np

data = np.array([[1,2],[3,1],[3,5],[4,6],[3,4]])
s = np.std(data[:,0])
m = np.mean(data[:,0])
temp = (data[:,0] - m) 
temp /= s

print(temp)
data[:,0] = temp
print(data)

and the result is this:

[-0.86266219 -1.40182605  0.75482941  1.29399328  0.21566555]
[[ 1  0]
 [ 3 -1]
 [ 3  0]
 [ 4  1]
 [ 3  0]]

Why does it automatically convert to integer when I put it into numpy? How do I fix this?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
sedrick
  • 201
  • 4
  • 14
  • What is the problem here? It now takes up too little space? – Stephen Rauch Feb 20 '19 at 02:45
  • I want the second column of "data" to be [-0.86266219 -1.40182605 0.75482941 1.29399328 0.21566555], but it's showing [0, -1, 0, 1, 0] – sedrick Feb 20 '19 at 02:47
  • Anything put into an integer `dtype` array is converted to an integer. Arrays don't hold mixed data types. You need to collect the normalized values in a float dtype array. (And any thing inserted into that array will become a float!). – hpaulj Feb 20 '19 at 04:20
  • I don't like the duplicate link because he should not be trying to convert `data` in-place to float. `data` is integer, and should remain that way. – hpaulj Feb 20 '19 at 04:46

0 Answers0