[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I couldn't have the transpose of this array in python.

Posted on 2012-04-13
3
Medium Priority
?
591 Views
Last Modified: 2012-04-16
Hi there;

Can you check and help me why I cannot have the transpose of this array?

http://sudrap.org/paste/text/137636/

Kind regards.
0
Comment
Question by:jazzIIIlove
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 1332 total points
ID: 37844789
The matrix transposition means to reorganize a 2-dimensional matrix to the other 2D matrix so that rows become columns.  The 2D matrix is expressed as a list of rows where row is also a list.  Giving the array() only one list, you create only one-dimensional array where you cannot say if it is a column or a row.  Try the following:
c:\tmp\_Python\jazzIIIlove>py26
Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> a = numpy.array([[1, 2, 3],
...                 [11, 22, 33]])
>>> a
array([[ 1,  2,  3],
       [11, 22, 33]])
>>> b = a.transpose()
>>> b
array([[ 1, 11],
       [ 2, 22],
       [ 3, 33]])
>>>

Open in new window


You can see here that two rows were transposed to three rows.
0
 
LVL 9

Assisted Solution

by:Hamed Zaghaghi
Hamed Zaghaghi earned 668 total points
ID: 37844861
Hi,
instead of
a = numpy.arange(10)
use
a = numpy.arange(10).reshape(1,10)

so you have a 2D array.
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 1332 total points
ID: 37845816
Good idea by zaghaghi!  Think also about the fact that arange() is only one way of creating an array filled by certain values.  You can directly define the array with your values via numpy.array() -- double the brackets to express 2D array with one row.  Here it is illustrated both the zaghaghi's way and manually typed:
>>> import numpy
>>> a = numpy.arange(10).reshape(1,10)
>>> a
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> b = numpy.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> b
array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
>>> t = a.transpose()
>>> t
array([[0],
       [1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8],
       [9]])

Open in new window

The a and b are the same initially.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
This article covers the basics of data encryption, what it is, how it works, and why it's important. If you've ever wondered what goes on when you "encrypt" data, you can look here to build a good foundation for your personal learning.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question