1

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?

2
  • Maybe for a 1-d array the question isn't relevant.. Commented Aug 30, 2016 at 15:52
  • if it was a 2-d list you would get 2 dimensions, but because it is only a 1-d list it only has one dimension. What tutorial are you using? Commented Aug 30, 2016 at 15:54

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.