0

I have an array converted from list, when I try to get its shape, I got only one number. like this:

    list1=[1,2,3,4,5]
    a1=numpy.array(list1)
    print a1.shape

and I got

    (5,)

and then I tried

    list2=[[1,2,3,4,5]]
    a2=numpy.array(list2)

    list3=[[1],[2],[3],[4],[5]]
    a3=numpy.array(list3)

    print a1+a2
    print a1+a3

I get

    [[ 2  4  6  8 10]]

    [[ 2  3  4  5  6]
    [ 3  4  5  6  7]
    [ 4  5  6  7  8]
    [ 5  6  7  8  9]
    [ 6  7  8  9 10]]

it seems a1 works like a2. Can I think like that way? Will it cause problems if i treat a1 as a2, besides shape method?

YD Han
  • 45
  • 1
  • 6

2 Answers2

2

Try:

list1=[[1,2,3,4,5]]
a=numpy.array(list1)
print a.shape

This will give you (1, 5), one row, 5 columns.

And this:

list1=[[1],[2],[3],[4],[5]]
a=numpy.array(list1)
print a.shape

Will get you (5, 1)

Peter
  • 745
  • 1
  • 6
  • 12
1

You can look at this post, it has clarified every thing related to numpy shape. (edit: question marked as duplicate of the same question)


This simple code might also help you get some insights:

import numpy as np

list1=[1,2,3,4,5]

a=np.array(list1)

print a.shape
# (5,)

## set be to be the transpose of a
b = a.T
print b.shape
# (5,)

print np.inner(a,b)
# 55
print np.inner(a,a)
# 55
Community
  • 1
  • 1
alili2050
  • 36
  • 5