數字派 NumPy ︰陣列運算《四》

欲知所學固與不固,何妨來點測驗小猜謎

Quiz

A quiz is a form of game or mind sport, in which the players (as individuals or in teams) attempt to answer questions correctly. It is a game to test your knowledge about a certain subject. In some countries, a quiz is also a brief assessment used in education and similar fields to measure growth in knowledge, abilities, and/or skills.

Quizzes are usually scored in points and many quizzes are designed to determine a winner from a group of participants – usually the participant with the highest score. They may also involve eliminating those who get too many questions wrong, the winner being the last man standing.

Etymology

The earliest known examples of the word date back to 1780; its etymology is unknown, but it may have originated in student slang. It initially meant a “odd, eccentric person”[a] or a “joke, hoax”. Later (perhaps by association with words such as “inquisitive”) it came to mean “to observe, study intently”, and thence (from about mid-19th century) “test, exam.”[2][3]

There is a well-known myth about the word quiz that says that in 1791 a Dublin theater owner named Richard Daly made a bet that he could introduce a word into the language within 24 hours. He then went out and hired a group of street urchins to write the word “quiz”, which was a nonsense word, on walls around the city of Dublin. Within a day, the word was common currency and had acquired a meaning (since no one knew what it meant, everyone thought it was some sort of test) and Daly had some extra cash in his pocket.[4] However, there is no evidence to support the story, and the term was already in use before the alleged bet in 1791.

 

先讀『問題』 Question ︰

Are 1-dimensional numpy arrays equivalent to vectors? [closed]

I’m new to both linear algebra and numpy, so please bear with me. I’m taking a course on linear regression, where I learned that we can express our hypothesis as {\theta}^T X where \theta is our coefficient vector (written in math notation as a column vector) and X is our design matrix with m features and n rows.

Now, a vector can be viewed as one column or one row of a matrix. But in numpy, there is a difference between an array with shape (5,) and an array with shape (5,1).

  1. Is there a mathematical equivalent to the numpy distinction between shape (5,) and shape(5,1), or are we to view both as vectors?
  2. When we transpose a numpy array with shape (5,) its shape doesn’t change, whereas transposing an array with shape (5,1) results in the shape (1,5). What would be the correct numpy equivalent of {\theta}^T X?

 

嘗試自己『回答』 Answer …

然後認真『核實』 Verify ︰

 

 

 

 

可以虛心廣納『善解』也☆

1 Answer

 A NumPy array is a N-dimensional container of items of the same type and size. As a computer programming data structure, it is limited by resources and dtype — there are values which are not representable by NumPy arrays. Due to these limitations, NumPy arrays are not exactly equivalent to the mathematical concept of coordinate vectors. NumPy arrays are often used to (approximately) represent vectors however.Math also has a concept of vector spaces whose elements are called vectors. One example of a vector is an object with direction and magnitude. A coordinate vector is merely a represention of the vector with respect to a particular coordinate system. So while a NumPy array can at best record the coordinates of a vector (tacitly, with respect to a coordinate system), it can not capture the full abstract notion of a vector. The abstract notion of vector exists without any mention of coordinate system.

Moreover, vector spaces can be collections of things other than coordinates. For example, families of functions can form a vector space. The functions would then be vectors. So here is another example where NumPy arrays are not at all equivalent to vectors.


Linear algebra makes a distinction between “row vectors” and “column vectors”. There is no such distinction in NumPy. There are only n-dimensional arrays. Keep in mind that NumPy was built around a desire to generalize array-like containers to N dimensions where N is bigger than 2. So NumPy operations are defined in ways that generalize to higher dimensions.

For example, transposing a NumPy array of shape (a,b,c,d) returns an array of shape (d,c,b,a) — the axes are reversed. In two dimensions, this means an array of shape (a,b)(i.e. a rows, b columns) becomes an array of shape (b,a) (i.e, b rows, a columns). So NumPy’s notion of transposition matches up nicely with the linear algebra notion for 2-dimensional arrays.

But this also means that the transpose of a 1-dimensional NumPy array of shape (a,) still has shape (a,). Nothing changes. It is still the same 1-dimensional array. Thus there is no real distinction between “row vectors” and “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). Similarly, wherever you see a row vector in linear algebra you could use an array of shape(1,n).

However, NumPy also has a concept of broadcasting and one of the rules of broadcasting is that extra axes will be automatically added to any array on the left-hand side of its shape whenever an operation requires it. So, a 1-dimensional NumPy array of shape (5,) can broadcast to a 2-dimensional array of shape (1,5) (or 3-dimensional array of shape (1,1,5), etc).

This means a 1-dimensional array of shape (5,) can be thought of as a row vector since it will automatically broadcast up to an array of shape (1,5) whenever necessary.

On the other hand, broadcasting never adds extra axes on the right-hand side of the shape. You must do so explicitly. So if theta is an array of shape (5,), to create a “column vector” of shape (5,1) you must explicitly add the new axis yourself by using theta[:, np.newaxis] or the shorthand theta[:, None].