Using with Transpose instead?

Screen Link:

The following is the DQ answer for finding the dot product of vector_one and vector_two:

vector_one = np.asarray([
], dtype=np.float32)

vector_two = np.asarray([
], dtype=np.float32)

dot_product =[:,0], vector_two)

Would it be ok to pass vector_one.T into the function instead of vector_one[:,0]? This just strikes me as easier so I am wondering if something is wrong with this method?

Is there any other reason for using vector_one[:,0] besides transposing it into a 1x3 matrix?

I’m having a little trouble following it.

Thank you for your time!

In numpy, transposing a vector/array can affect the shape of that vector/array.

For vector_one, the shape is (3, 1).

For vector_one[:,0] the shape is (3,)

For vector_one.T, the shape is (1, 3)

Looking at just the shape, it would seem that taking the transpose should be better because that closely represents how we view matrix multiplication (n,m)*(m,n).

However, the output of that matters too.

For[:,0], vector_two)

the output of the above is [4] and that has the shape (1,).

For,, vector_two)

the output of the above is [[4]] and that has the shape (1, 1).

In certain cases (as you will come across when you get more into the machine learning related content), that distinction in the shape is important.

So, while there is nothing theoretically wrong with what you suggest, practically, with libraries like numpy, there can be consequences we should keep in mind.


Thank you very much for the explanation


I have a follow up question if you don’t mind.

I understand the concept of using vector_one[:,0] because the output has a shape of (1,) and that this shape might be required to pass into another function we will use in the future.

Is this a similar concept to the earlier ML mission where we instantiated a KNeighborsRegressor() and then passed it into .fit() making sure the first parameter of .fit() had double brackets to make sure it was a dataframe not a series?

Here is an example from that mission:

model = KNeighborsRegressor()
train_iteration_one = dc_listings[dc_listings["fold"] != 1]
test_iteration_one = dc_listings[dc_listings["fold"] == 1].copy()[["accommodates"]], train_iteration_one["price"])

I have notes that the first parameter of .fit() has to have a shape of a dataframe such as (100,1) thus the double brackets are required. If we used single brackets the it would return a series with shape of (100,) and return an error.

Is this a similar idea to what you are referring to?

Also, I was wondering if you could dive into that concept a bit more? :

Is there an example you can give where you need the output of to be in a specific shape?

Is this a common theme to be aware of in general?: that the output of a function ( for example) will ultimately be passed into another function or something where it must be in a specific shape or format? So not only is it important that we arrive at the correct answer (4) in this case but that it is equally important that its in the correct format to what it will be passed into next?

I feel like this is an important concept to be aware of and want to make sure I understand it.

Thanks for your time!

Yes, it is.

I don’t have any specific example that comes to mind. I think of it mostly as a case-by-case basis, but I don’t have enough experience to comment any fufrther than that.

In general, I think it would be important to think about how many features you are planning to work with depending on whatever you are working with. Like in the fit() example, we need a dataframe because we have a (rows, columns) shape requirement because the columns represents the number of features we can work with.

1 Like