7

I have a matrix tempsyntheticGroup2 with 6 columns. I want to change the value of columns (0,1,2,3,5) from float to int. This is my code:

tempsyntheticGroup2=tempsyntheticGroup2[:,[0,1,2,3,5]].astype(int)

but it doesn't work properly and I loose the other columns.

ali_m
  • 71,714
  • 23
  • 223
  • 298
Talia
  • 2,947
  • 4
  • 17
  • 28
  • You can use a list to be a index of other list, but not this way. I think that there is a mistake in your code. Can you share the entire matrix? Are you sure that list values are float? I am asking because the list `[0,1,2,3,5]` are integers. – Andre Araujo Oct 29 '15 at 02:41
  • What is the `dtype` of `tempsyntheticGroup2`; also its `shape`? – hpaulj Oct 29 '15 at 03:57
  • While it is technically possible for a numpy array to contain heterogeneously typed elements if its dtype is `np.object`, in practice this is almost never a good idea for performance reasons. It might make sense to use a [structured array](http://docs.scipy.org/doc/numpy/user/basics.rec.html) or a [pandas `DataFrame`](http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html), although perhaps the most straightforward option would be to store the columns that need to have different dtypes in separate arrays and address them by a common row index. – ali_m Oct 29 '15 at 09:13

2 Answers2

6

I don't think you can have a numpy array with some element that are ints, and some that are floats (there is only one possible dtype per array). But if you just want to round to lower integer (while keeping all elements as floats) you can do this:

# define dummy example matrix
t = np.random.rand(3,4) + np.arange(12).reshape((3,4))

array([[  0.68266426,   1.4115732 ,   2.3014562 ,   3.5173022 ],
       [  4.52399807,   5.35321628,   6.95888015,   7.17438118],
       [  8.97272076,   9.51710983,  10.94962065,  11.00586511]])



# round some columns to lower int
t[:,[0,2]] = np.floor(t[:,[0,2]])
# or
t[:,[0,2]] = t[:,[0,2]].astype(int)

array([[  0.        ,   1.4115732 ,   2.        ,   3.5173022 ],
       [  4.        ,   5.35321628,   6.        ,   7.17438118],
       [  8.        ,   9.51710983,  10.        ,  11.00586511]])

otherwise you probably need to split your original array into 2 different arrays, with one containing the column that stay floats, the other containing the column that become ints.

t_int =  t[:,[0,2]].astype(int)

array([[ 0,  2],
       [ 4,  6],
       [ 8, 10]])


t_float = t[:,[1,3]]

array([[  1.4115732 ,   3.5173022 ],
       [  5.35321628,   7.17438118],
       [  9.51710983,  11.00586511]])

Note that you'll have to change your indexing accordingly to access your elements...

Julien
  • 13,986
  • 5
  • 29
  • 53
0

I think you use wrong syntax to get column data.

read this article.

How do you extract a column from a multi-dimensional array?

Community
  • 1
  • 1