how can I apply the np.dot function on two lists in numpy pandas
up vote
-2
down vote
favorite
I have two different lists and I have to apply np.dot function of numpy in python
how can I do that?
list1=
array([[ nan, nan],
[ 0.000829, 0.000326],
[-0.000149, -0.00033 ],
...,
[-0.000757, -0.000737],
[-0.000795, -0.00068 ],
[-0.000788, -0.00069 ]])
list2 =
array([[ nan, nan],
[ nan, nan],
[-0.000829, -0.000326],
...,
[ 0.000763, 0.000738],
[ 0.000757, 0.000737],
[ 0.000795, 0.00068 ]])
these are two seperate list of lists
so I want to do it this way:
np.dot([-0.000149, -0.00033 ], [-0.000829, -0.000326])
so it is like
np.dot(list1[3], list2[3])
and it continue to choose one index from one list and one index from the other list
and that should return the one dimensional array, the problem is data, which is in two seperate list, so I need one index from list one and one index from the other list, i know it can be done through the loop, but not sure how is it possible,
i hope it is clear now
python python-3.x numpy
add a comment |
up vote
-2
down vote
favorite
I have two different lists and I have to apply np.dot function of numpy in python
how can I do that?
list1=
array([[ nan, nan],
[ 0.000829, 0.000326],
[-0.000149, -0.00033 ],
...,
[-0.000757, -0.000737],
[-0.000795, -0.00068 ],
[-0.000788, -0.00069 ]])
list2 =
array([[ nan, nan],
[ nan, nan],
[-0.000829, -0.000326],
...,
[ 0.000763, 0.000738],
[ 0.000757, 0.000737],
[ 0.000795, 0.00068 ]])
these are two seperate list of lists
so I want to do it this way:
np.dot([-0.000149, -0.00033 ], [-0.000829, -0.000326])
so it is like
np.dot(list1[3], list2[3])
and it continue to choose one index from one list and one index from the other list
and that should return the one dimensional array, the problem is data, which is in two seperate list, so I need one index from list one and one index from the other list, i know it can be done through the loop, but not sure how is it possible,
i hope it is clear now
python python-3.x numpy
@lucidbrot yes it is the kind of matrix multiplication
– id101112
Nov 21 at 21:02
@timgeb I don't have the output yet, but it after matrix multiplication it should be a one dimensional list
– id101112
Nov 21 at 21:04
@id101112 Okay, apparently the dot function is defined for more shapes than I expected but usinglist1 @ list2
is recommended. Your question needs some additional info about what is wrong withnp.dot(list1, list2)
though
– lucidbrot
Nov 21 at 21:05
@id101112list1
is not a vector, it is a matrix. How do you expect the matrix multiplication to work without transposing either list? And I don't think the result can ever be onedimensional. You have matrixes of shapeNx2
so it will be at least 2-dimensional, right?
– lucidbrot
Nov 21 at 21:07
1
let me edit my question again, it might not be very cleared,
– id101112
Nov 21 at 21:08
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have two different lists and I have to apply np.dot function of numpy in python
how can I do that?
list1=
array([[ nan, nan],
[ 0.000829, 0.000326],
[-0.000149, -0.00033 ],
...,
[-0.000757, -0.000737],
[-0.000795, -0.00068 ],
[-0.000788, -0.00069 ]])
list2 =
array([[ nan, nan],
[ nan, nan],
[-0.000829, -0.000326],
...,
[ 0.000763, 0.000738],
[ 0.000757, 0.000737],
[ 0.000795, 0.00068 ]])
these are two seperate list of lists
so I want to do it this way:
np.dot([-0.000149, -0.00033 ], [-0.000829, -0.000326])
so it is like
np.dot(list1[3], list2[3])
and it continue to choose one index from one list and one index from the other list
and that should return the one dimensional array, the problem is data, which is in two seperate list, so I need one index from list one and one index from the other list, i know it can be done through the loop, but not sure how is it possible,
i hope it is clear now
python python-3.x numpy
I have two different lists and I have to apply np.dot function of numpy in python
how can I do that?
list1=
array([[ nan, nan],
[ 0.000829, 0.000326],
[-0.000149, -0.00033 ],
...,
[-0.000757, -0.000737],
[-0.000795, -0.00068 ],
[-0.000788, -0.00069 ]])
list2 =
array([[ nan, nan],
[ nan, nan],
[-0.000829, -0.000326],
...,
[ 0.000763, 0.000738],
[ 0.000757, 0.000737],
[ 0.000795, 0.00068 ]])
these are two seperate list of lists
so I want to do it this way:
np.dot([-0.000149, -0.00033 ], [-0.000829, -0.000326])
so it is like
np.dot(list1[3], list2[3])
and it continue to choose one index from one list and one index from the other list
and that should return the one dimensional array, the problem is data, which is in two seperate list, so I need one index from list one and one index from the other list, i know it can be done through the loop, but not sure how is it possible,
i hope it is clear now
python python-3.x numpy
python python-3.x numpy
edited Nov 21 at 21:17
asked Nov 21 at 20:58
id101112
174115
174115
@lucidbrot yes it is the kind of matrix multiplication
– id101112
Nov 21 at 21:02
@timgeb I don't have the output yet, but it after matrix multiplication it should be a one dimensional list
– id101112
Nov 21 at 21:04
@id101112 Okay, apparently the dot function is defined for more shapes than I expected but usinglist1 @ list2
is recommended. Your question needs some additional info about what is wrong withnp.dot(list1, list2)
though
– lucidbrot
Nov 21 at 21:05
@id101112list1
is not a vector, it is a matrix. How do you expect the matrix multiplication to work without transposing either list? And I don't think the result can ever be onedimensional. You have matrixes of shapeNx2
so it will be at least 2-dimensional, right?
– lucidbrot
Nov 21 at 21:07
1
let me edit my question again, it might not be very cleared,
– id101112
Nov 21 at 21:08
add a comment |
@lucidbrot yes it is the kind of matrix multiplication
– id101112
Nov 21 at 21:02
@timgeb I don't have the output yet, but it after matrix multiplication it should be a one dimensional list
– id101112
Nov 21 at 21:04
@id101112 Okay, apparently the dot function is defined for more shapes than I expected but usinglist1 @ list2
is recommended. Your question needs some additional info about what is wrong withnp.dot(list1, list2)
though
– lucidbrot
Nov 21 at 21:05
@id101112list1
is not a vector, it is a matrix. How do you expect the matrix multiplication to work without transposing either list? And I don't think the result can ever be onedimensional. You have matrixes of shapeNx2
so it will be at least 2-dimensional, right?
– lucidbrot
Nov 21 at 21:07
1
let me edit my question again, it might not be very cleared,
– id101112
Nov 21 at 21:08
@lucidbrot yes it is the kind of matrix multiplication
– id101112
Nov 21 at 21:02
@lucidbrot yes it is the kind of matrix multiplication
– id101112
Nov 21 at 21:02
@timgeb I don't have the output yet, but it after matrix multiplication it should be a one dimensional list
– id101112
Nov 21 at 21:04
@timgeb I don't have the output yet, but it after matrix multiplication it should be a one dimensional list
– id101112
Nov 21 at 21:04
@id101112 Okay, apparently the dot function is defined for more shapes than I expected but using
list1 @ list2
is recommended. Your question needs some additional info about what is wrong with np.dot(list1, list2)
though– lucidbrot
Nov 21 at 21:05
@id101112 Okay, apparently the dot function is defined for more shapes than I expected but using
list1 @ list2
is recommended. Your question needs some additional info about what is wrong with np.dot(list1, list2)
though– lucidbrot
Nov 21 at 21:05
@id101112
list1
is not a vector, it is a matrix. How do you expect the matrix multiplication to work without transposing either list? And I don't think the result can ever be onedimensional. You have matrixes of shape Nx2
so it will be at least 2-dimensional, right?– lucidbrot
Nov 21 at 21:07
@id101112
list1
is not a vector, it is a matrix. How do you expect the matrix multiplication to work without transposing either list? And I don't think the result can ever be onedimensional. You have matrixes of shape Nx2
so it will be at least 2-dimensional, right?– lucidbrot
Nov 21 at 21:07
1
1
let me edit my question again, it might not be very cleared,
– id101112
Nov 21 at 21:08
let me edit my question again, it might not be very cleared,
– id101112
Nov 21 at 21:08
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
So your question is actually about how to loop through the lists and call np.dot
on each corresponding pair. Here's one way to do it, using list comprehension and zip
:
>>> import numpy as np
>>> list1 = np.array([[1,2],[3,4]])
>>> list2 = list1.copy()
>>> list_of_results = [np.dot(a,b) for a,b in zip(list1, list2)]
>>> list_of_results
[5, 25]
If you are not familiar with list comprehension, I advise you to look that up. But you could also do it with a simple for loop:
assert list1.shape == list2.shape, "List shapes do not match"
results =
for inner_list_index in range(list1.shape[0]):
a = list1[inner_list_index]
b = list2[inner_list_index]
res = np.dot(a,b)
results = results.append(res)
This can be reduced to fewer lines:
>>> assert list1.shape[0] == list2.shape[0]
>>> results =
>>> for i in range(list1.shape[0]):
... results.append(np.dot(list1[i], list2[i]))
...
>>> results
[5, 25]
Note that both of these approaches return a normal list
, not a numpy ndarray
. This is because appending to numpy arrays is usually not too fast. You could use np.append()
instead. Or just apply np.array()
to the result if you need it as an np array again.
okey yes I just needed this line[np.dot(a,b) for a,b in zip(list1, list2)]
thanks
– id101112
Nov 21 at 22:39
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
So your question is actually about how to loop through the lists and call np.dot
on each corresponding pair. Here's one way to do it, using list comprehension and zip
:
>>> import numpy as np
>>> list1 = np.array([[1,2],[3,4]])
>>> list2 = list1.copy()
>>> list_of_results = [np.dot(a,b) for a,b in zip(list1, list2)]
>>> list_of_results
[5, 25]
If you are not familiar with list comprehension, I advise you to look that up. But you could also do it with a simple for loop:
assert list1.shape == list2.shape, "List shapes do not match"
results =
for inner_list_index in range(list1.shape[0]):
a = list1[inner_list_index]
b = list2[inner_list_index]
res = np.dot(a,b)
results = results.append(res)
This can be reduced to fewer lines:
>>> assert list1.shape[0] == list2.shape[0]
>>> results =
>>> for i in range(list1.shape[0]):
... results.append(np.dot(list1[i], list2[i]))
...
>>> results
[5, 25]
Note that both of these approaches return a normal list
, not a numpy ndarray
. This is because appending to numpy arrays is usually not too fast. You could use np.append()
instead. Or just apply np.array()
to the result if you need it as an np array again.
okey yes I just needed this line[np.dot(a,b) for a,b in zip(list1, list2)]
thanks
– id101112
Nov 21 at 22:39
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
add a comment |
up vote
1
down vote
accepted
So your question is actually about how to loop through the lists and call np.dot
on each corresponding pair. Here's one way to do it, using list comprehension and zip
:
>>> import numpy as np
>>> list1 = np.array([[1,2],[3,4]])
>>> list2 = list1.copy()
>>> list_of_results = [np.dot(a,b) for a,b in zip(list1, list2)]
>>> list_of_results
[5, 25]
If you are not familiar with list comprehension, I advise you to look that up. But you could also do it with a simple for loop:
assert list1.shape == list2.shape, "List shapes do not match"
results =
for inner_list_index in range(list1.shape[0]):
a = list1[inner_list_index]
b = list2[inner_list_index]
res = np.dot(a,b)
results = results.append(res)
This can be reduced to fewer lines:
>>> assert list1.shape[0] == list2.shape[0]
>>> results =
>>> for i in range(list1.shape[0]):
... results.append(np.dot(list1[i], list2[i]))
...
>>> results
[5, 25]
Note that both of these approaches return a normal list
, not a numpy ndarray
. This is because appending to numpy arrays is usually not too fast. You could use np.append()
instead. Or just apply np.array()
to the result if you need it as an np array again.
okey yes I just needed this line[np.dot(a,b) for a,b in zip(list1, list2)]
thanks
– id101112
Nov 21 at 22:39
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
So your question is actually about how to loop through the lists and call np.dot
on each corresponding pair. Here's one way to do it, using list comprehension and zip
:
>>> import numpy as np
>>> list1 = np.array([[1,2],[3,4]])
>>> list2 = list1.copy()
>>> list_of_results = [np.dot(a,b) for a,b in zip(list1, list2)]
>>> list_of_results
[5, 25]
If you are not familiar with list comprehension, I advise you to look that up. But you could also do it with a simple for loop:
assert list1.shape == list2.shape, "List shapes do not match"
results =
for inner_list_index in range(list1.shape[0]):
a = list1[inner_list_index]
b = list2[inner_list_index]
res = np.dot(a,b)
results = results.append(res)
This can be reduced to fewer lines:
>>> assert list1.shape[0] == list2.shape[0]
>>> results =
>>> for i in range(list1.shape[0]):
... results.append(np.dot(list1[i], list2[i]))
...
>>> results
[5, 25]
Note that both of these approaches return a normal list
, not a numpy ndarray
. This is because appending to numpy arrays is usually not too fast. You could use np.append()
instead. Or just apply np.array()
to the result if you need it as an np array again.
So your question is actually about how to loop through the lists and call np.dot
on each corresponding pair. Here's one way to do it, using list comprehension and zip
:
>>> import numpy as np
>>> list1 = np.array([[1,2],[3,4]])
>>> list2 = list1.copy()
>>> list_of_results = [np.dot(a,b) for a,b in zip(list1, list2)]
>>> list_of_results
[5, 25]
If you are not familiar with list comprehension, I advise you to look that up. But you could also do it with a simple for loop:
assert list1.shape == list2.shape, "List shapes do not match"
results =
for inner_list_index in range(list1.shape[0]):
a = list1[inner_list_index]
b = list2[inner_list_index]
res = np.dot(a,b)
results = results.append(res)
This can be reduced to fewer lines:
>>> assert list1.shape[0] == list2.shape[0]
>>> results =
>>> for i in range(list1.shape[0]):
... results.append(np.dot(list1[i], list2[i]))
...
>>> results
[5, 25]
Note that both of these approaches return a normal list
, not a numpy ndarray
. This is because appending to numpy arrays is usually not too fast. You could use np.append()
instead. Or just apply np.array()
to the result if you need it as an np array again.
edited Nov 21 at 21:34
answered Nov 21 at 21:29
lucidbrot
1,20811233
1,20811233
okey yes I just needed this line[np.dot(a,b) for a,b in zip(list1, list2)]
thanks
– id101112
Nov 21 at 22:39
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
add a comment |
okey yes I just needed this line[np.dot(a,b) for a,b in zip(list1, list2)]
thanks
– id101112
Nov 21 at 22:39
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
okey yes I just needed this line
[np.dot(a,b) for a,b in zip(list1, list2)]
thanks– id101112
Nov 21 at 22:39
okey yes I just needed this line
[np.dot(a,b) for a,b in zip(list1, list2)]
thanks– id101112
Nov 21 at 22:39
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
No problem! Please upvote and accept my answer if it helped you :) and if something is unclear, feel free to ask
– lucidbrot
Nov 22 at 4:45
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420381%2fhow-can-i-apply-the-np-dot-function-on-two-lists-in-numpy-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
@lucidbrot yes it is the kind of matrix multiplication
– id101112
Nov 21 at 21:02
@timgeb I don't have the output yet, but it after matrix multiplication it should be a one dimensional list
– id101112
Nov 21 at 21:04
@id101112 Okay, apparently the dot function is defined for more shapes than I expected but using
list1 @ list2
is recommended. Your question needs some additional info about what is wrong withnp.dot(list1, list2)
though– lucidbrot
Nov 21 at 21:05
@id101112
list1
is not a vector, it is a matrix. How do you expect the matrix multiplication to work without transposing either list? And I don't think the result can ever be onedimensional. You have matrixes of shapeNx2
so it will be at least 2-dimensional, right?– lucidbrot
Nov 21 at 21:07
1
let me edit my question again, it might not be very cleared,
– id101112
Nov 21 at 21:08