vishnu
vishnu

Reputation: 25

Shape command does not return the column number when the data frame has only column

The dataset is mentioned as below The defines variable is exam . exam.shape = (29,2)

    |Hours| |Pass| 
0   |0.5 |    |0|  
1   |0.75|    |0|  
2   |1.00|    |0|  
3   |1.25|    |0|  
4   |1.50|    |0| 

Clear image is attached in the screenshot

Data set image - Exam

X = exam.Hours  
y = exam.Pass 

X.shape = (29,)  # The column number one is not mentioned   
y.shape = (29,) # The column value is not mentioned 

Expected Outcome

X.shape = (29,1)    
y.shape = (29,1)

Upvotes: 1

Views: 507

Answers (2)

hpaulj
hpaulj

Reputation: 231530

In [200]: df
Out[200]: 
   age  rank  height  weight
0   20     2     155      53
1   15     7     159      60
2   34     6     180      75
3   40     5     163      80
4   60     1     170      49
In [201]: df.shape
Out[201]: (5, 4)

The following is a Series,which only has length, not columns:

In [202]: df['height']
Out[202]: 
0    155
1    159
2    180
3    163
4    170
Name: height, dtype: int64
In [203]: df['height'].shape
Out[203]: (5,)

Index with a list returns a dataframe with column number. Note the difference in display:

In [204]: df[['height']]
Out[204]: 
   height
0     155
1     159
2     180
3     163
4     170
In [205]: df[['height']].shape
Out[205]: (5, 1)

Upvotes: 0

Ivan
Ivan

Reputation: 40708

Both arrays are one-dimensional and you are looking to add one extra dimension. You need to unsqueeze a new axis on your arrays.

>>> x = np.random.rand(29)

>>> x.shape
(29,)

Either using indexing:

>>> x = x[..., np.newaxis] # i.e. x[..., None]

>>> x.shape
(29, 1)

Or with the np.expand_dims utility:

>>> x = np.expand_dims(x, -1)

>>> x.shape
(29, 1)

Upvotes: 1

Related Questions