Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python, hstack column numpy arrays (column vectors) of different types

Tags:

python

numpy

I currently have a numpy multi-dimensional array (of type float) and a numpy column array (of type int). I want to combine the two into a mutli-dimensional numpy array.

import numpy

>> dates.shape
(1251,)
>> data.shape
(1251,10)
>> test = numpy.hstack((dates, data))
ValueError: all the input arrays must have same number of dimensions

To show that the types of the arrays are different:

>> type(dates[0])
<type 'numpy.int64'>
>> type(data[0,0])
<type 'numpy.float64'>
like image 465
benjaminmgross Avatar asked Dec 31 '11 02:12

benjaminmgross


People also ask

Can NumPy array contains different data types?

Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.

Can NumPy contain elements of different types?

Yes, if you use numpy structured arrays, each element of the array would be a "structure", and the fields of the structure can have different datatypes.

Is NumPy array a column vector?

The NumPy ndarray class is used to represent both matrices and vectors. A vector is an array with a single dimension (there's no difference between row and column vectors), while a matrix refers to an array with two dimensions. For 3-D or higher dimensional arrays, the term tensor is also commonly used.

Are NumPy arrays row or column vectors?

NumPy apes the concept of row and column vectors using 2-dimensional arrays. An array of shape (5,1) has 5 rows and 1 column. You can sort of think of this as a column vector, and wherever you would need a column vector in linear algebra, you could use an array of shape (n,1) .


2 Answers

import numpy as np

np.column_stack((dates, data))

The types are cast automatically to the most precise, so your int array will be converted to float.

like image 82
Griffith Rees Avatar answered Sep 28 '22 19:09

Griffith Rees


The types don't matter, you should reshape dates to be (1251, 1) before using hstack.

Ps. The ints will be cast to float.

like image 20
Bi Rico Avatar answered Sep 28 '22 18:09

Bi Rico