Is dot product and normal multiplication results of 2 numpy arrays same?

2024/10/6 8:17:21

I am working with kernel PCA in Python and I have to find the values after projecting the original data to the principal components.I use the equation

 fv = eigvecs[:,:ncomp]print(len(fv))td = fv.T * K.T

where K is the kernel matrix of dimension (150x150),ncomp is the number of principal components.The code works perfectly fine when fv has dimension (150x150).But when I select ncomp as 3 making fv to be of (150x3) as dimension,there occurs error stating operands could not be broadcast together.I referred various links and tried using dot products liketd=np.dot(fv.T,K.T). I dont get any error now.But I dont know whether the values retrieved are correct or not...

Plz help...

Answer

The * operator depends on the data type. On Numpy arrays it does an element-wise multiplication (not the matrix multiplication); numpy.vdot() does the "dot" scalar product of two vectors (which returns a simple scalar result)

>>> import numpy as np
>>> x = np.array([[1,2,3]])
>>> np.vdot(x, x)
14
>>> x * x
array([[1, 4, 9]])

To multiply 2 arrays as matrices properly, use numpy.dot:

>>> np.dot(x, x)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ValueError: objects are not aligned
>>> np.dot(x.T, x)
array([[ 1,  4,  9],[ 4, 16, 36],[ 9, 36, 81]])
>>> np.dot(x, x.T)
array([[98]])

Then there is numpy.matrix, a specialization of array for which the * means matrix multiplication, and ** means matrix power; so be sure to know what datatype you are operating on.


The upcoming Python 3.5 will have a new operator @ that can be used for matrix multiplication; then you could write x @ x.T to replace the code in the last example.

https://en.xdnf.cn/q/119433.html

Related Q&A

Tkinter Entry widget stays empty in larger programs (Python 2)

I want to make a program where, after clicking on a button, a user gets asked for its name, after which the program continues. Im stuck on making the popup return the textstring that has been entered i…

How do i implement this python tree linked list code in dart?

Here is the python codedef tree(root_label, branches=[]):for branch in branches:assert is_tree(branch), branches must be treesreturn [root_label] + list(branches)def label(tree):return tree[0]def branc…

How can i find the ips in network in python

How can i find the TCP ips in network with the range(i.e 132.32.0.3 to 132.32.0.44) through python programming and also want to know the which ips are alive and which are dead. please send me.. thanks …

Python Coding (call function / return values)

I am having trouble writing code for this question. I have seen this question asked in a few places but I still cannot figure out the answer from the tips they provided. The question is: Write a progr…

discord.py Mention user by name

I am trying to mention a user by their name in discord.py. My current code is: @bot.command(name=mention) @commands.has_role(OwnerCommands) async def mention(ctx, *, member: discord.Member):memberid = …

Python unexpected EOF while parsing : syntax error

I am trying to do a simple toto history with a dictionary and function however I have this funny syntax error that keeps appearing that states "unexpected EOF while parsing" on the python she…

How can I read part of file and write the rest to another file?

I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?

Encryption/Decryption - Python GCSE [duplicate]

This question already has an answer here:Encryption and Decryption within the alphabet - Python GCSE(1 answer)Closed 8 years ago.I am currently trying to write a program, for school, in order to encryp…

Convert decimal to binary (Python) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Kivy Digital Clock Issues

Im trying to add a digital clock to my Kivy program, it seems to be having trouble.Here is the .py:import kivykivy.require(1.10.0)from kivy.lang import Builder from kivy.uix.screenmanager import Screen…