Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python numpy: Change the column type of a numpy matrix

I have a numpy matrix X, and I tried to change the datatype of column 1 using the code below:

X[:, 1].astype('str')
print(type(X[0, 1]))

but I got the following result:

<type 'numpy.float64'>

Anyone know why the type was not changed to str ? And what is a correct way to change the column type of X?Thanks!

like image 792
Edamame Avatar asked Apr 09 '16 19:04

Edamame


People also ask

How do I change the column of a matrix in numpy?

To transpose NumPy array ndarray (swap rows and columns), use the T attribute ( . T ), the ndarray method transpose() and the numpy. transpose() function.

How do you change a column in a matrix in Python?

Rows and columns of NumPy arrays can be selected or modified using the square-bracket indexing notation in Python. To select a row in a 2D array, use P[i] . For example, P[0] will return the first row of P . To select a column, use P[:, i] .

How do I change the Dtype of a numpy array?

In order to change the dtype of the given array object, we will use numpy. astype() function. The function takes an argument which is the target data type. The function supports all the generic types and built-in types of data.

How do you change the Type of a column in Python?

Change column type in pandas using DataFrame.apply() to_numeric, pandas. to_datetime, and pandas. to_timedelta as arguments to apply the apply() function to change the data type of one or more columns to numeric, DateTime, and time delta respectively.


2 Answers

Providing a simple example will explain it better.

>>> a = np.array([[1,2,3],[4,5,6]])
array([[1, 2, 3],
       [4, 5, 6]])
>>> a[:,1]
array([2, 5])
>>> a[:,1].astype('str') # This generates copy and then cast.
array(['2', '5'], dtype='<U21')
>>> a                    # So the original array did not change.
array([[1, 2, 3],
       [4, 5, 6]])
like image 62
Hun Avatar answered Oct 09 '22 22:10

Hun


More clear and straightforward answer. The type was not changed to str because NumPy array should have only one data type. The correct way to change the column type of X would be to use structured arrays or one of the solutions from this question.

I had the same problem, and I didn't want to use structured arrays. A possible option is to use pandas if it suits your task. If you're going to change just one column, possibly it means that your data is tabular. Then you can easily change the data type of column. Another compromise is to make a copy of the column and use it separately from the original array.

>>> x = np.ones((3, 3), dtype=np.float)
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])
>>> x[:, 1] = x[:, 1].astype(np.int)
>>> type(x[:, 1][0])
numpy.float64
>>> x_pd = pd.DataFrame(x)
>>> x_pd[1] = x_pd[1].astype(np.int16)
>>> type(x_pd[1][0])
numpy.int16
like image 32
dinarkino Avatar answered Oct 09 '22 23:10

dinarkino