Python: compare and insert a vector with an ordered numpy array Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Traverse a list in reverse order in PythonDistance between numpy arrays, columnwiseVectorize over only one axis in a 2D array with numpy vectorizeVectorize Operations in Numpy for Two Dependent ArraysPythonic way to compare sign of numpy array with DataframeVector operations with numpyNumpy vectorized zero-order interpolationCreate a Large Numpy ArrayPossible to vectorize this array operation in python?Tile rows of a 2D numpy array based on values in separate numpy vector

What were wait-states, and why was it only an issue for PCs?

How can I wire a 9-position switch so that each position turns on one more LED than the one before?

What was Apollo 13's "Little Jolt" after MECO?

France's Public Holidays' Puzzle

What to do with someone that cheated their way though university and a PhD program?

Did war bonds have better investment alternatives during WWII?

Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?

Simulate round-robin tournament draw

How long can a nation maintain a technological edge over the rest of the world?

What is ls Largest Number Formed by only moving two sticks in 508?

Co-worker works way more than he should

Can gravitational waves pass through a black hole?

When I export an AI 300x60 art board it saves with bigger dimensions

What do you call an IPA symbol that lacks a name (e.g. ɲ)?

What is a 'Key' in computer science?

Why is arima in R one time step off?

RIP Packet Format

How would it unbalance gameplay to rule that Weapon Master allows for picking a fighting style?

Preserving file and folder permissions with rsync

Test if all elements of a Foldable are the same

What is /etc/mtab in Linux?

What is the ongoing value of the Kanban board to the developers as opposed to management

What's parked in Mil Moscow helicopter plant?

Could a cockatrice have parasitic embryos?



Python: compare and insert a vector with an ordered numpy array



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Traverse a list in reverse order in PythonDistance between numpy arrays, columnwiseVectorize over only one axis in a 2D array with numpy vectorizeVectorize Operations in Numpy for Two Dependent ArraysPythonic way to compare sign of numpy array with DataframeVector operations with numpyNumpy vectorized zero-order interpolationCreate a Large Numpy ArrayPossible to vectorize this array operation in python?Tile rows of a 2D numpy array based on values in separate numpy vector



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have a numpy array whose rows is ordered and a vector. I need to insert this vector into the array if the value of the vector is smaller than some values of each array row. Furthermore, I need to get the index of the position. Can I use a vector like operation rather than two for loops?



For example, the i-th row of the array is [1,3,5,6], and the i-th value of vector is 4, the result is ([1,3,4,5], 2).
Here is the code example.



import random
import numpy as np

random.seed(0)
num_query = 2
topk = 3

distance = np.Inf * np.ones((num_query, topk))

print(distance)

for iter in range(4):
print('------------loop: :d------------'.format(iter))
dist = np.random.randn(num_query) / (0.3 * (iter+1))
dist = dist * np.sign(dist)
rows, columns = distance.shape
for row in range(rows):
d = dist[row]
if d > distance[row, columns - 1]:
continue
min_col = 0
for column in range(columns-1, -1, -1):
if distance[row, column] < d:
min_col = column + 1
break
distance[row, min_col + 1:] = distance[row, min_col:-1]
distance[row, min_col] = d
print(dist)
print(distance)
print('+++++++++++++++++++++++++++++++')
print(distance)


The output is:



[[inf inf inf]
[inf inf inf]]
------------loop: 0------------
[3.60155727 0.0991411 ]
[[3.60155727 inf inf]
[0.0991411 inf inf]]
+++++++++++++++++++++++++++++++
------------loop: 1------------
[1.4839764 1.99641269]
[[1.4839764 3.60155727 inf]
[0.0991411 1.99641269 inf]]
+++++++++++++++++++++++++++++++
------------loop: 2------------
[2.44132044 0.19221564]
[[1.4839764 2.44132044 3.60155727]
[0.0991411 0.19221564 1.99641269]]
+++++++++++++++++++++++++++++++
------------loop: 3------------
[0.29206705 1.17013653]
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]
+++++++++++++++++++++++++++++++
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]









share|improve this question
























  • It looks like you hit the submit button before you finished writing your question, as is evident by there being nothing after "the output is:". If you have any additional information you'd like to include in this question, please edit and add all relevant details.

    – Pikachu the Purple Wizard
    Mar 9 at 5:00











  • @SupaMegaDuckyMomodaWaffle, thanks, it was an image. Now I edit it.

    – Jiang QY.
    Mar 9 at 13:28

















1















I have a numpy array whose rows is ordered and a vector. I need to insert this vector into the array if the value of the vector is smaller than some values of each array row. Furthermore, I need to get the index of the position. Can I use a vector like operation rather than two for loops?



For example, the i-th row of the array is [1,3,5,6], and the i-th value of vector is 4, the result is ([1,3,4,5], 2).
Here is the code example.



import random
import numpy as np

random.seed(0)
num_query = 2
topk = 3

distance = np.Inf * np.ones((num_query, topk))

print(distance)

for iter in range(4):
print('------------loop: :d------------'.format(iter))
dist = np.random.randn(num_query) / (0.3 * (iter+1))
dist = dist * np.sign(dist)
rows, columns = distance.shape
for row in range(rows):
d = dist[row]
if d > distance[row, columns - 1]:
continue
min_col = 0
for column in range(columns-1, -1, -1):
if distance[row, column] < d:
min_col = column + 1
break
distance[row, min_col + 1:] = distance[row, min_col:-1]
distance[row, min_col] = d
print(dist)
print(distance)
print('+++++++++++++++++++++++++++++++')
print(distance)


The output is:



[[inf inf inf]
[inf inf inf]]
------------loop: 0------------
[3.60155727 0.0991411 ]
[[3.60155727 inf inf]
[0.0991411 inf inf]]
+++++++++++++++++++++++++++++++
------------loop: 1------------
[1.4839764 1.99641269]
[[1.4839764 3.60155727 inf]
[0.0991411 1.99641269 inf]]
+++++++++++++++++++++++++++++++
------------loop: 2------------
[2.44132044 0.19221564]
[[1.4839764 2.44132044 3.60155727]
[0.0991411 0.19221564 1.99641269]]
+++++++++++++++++++++++++++++++
------------loop: 3------------
[0.29206705 1.17013653]
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]
+++++++++++++++++++++++++++++++
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]









share|improve this question
























  • It looks like you hit the submit button before you finished writing your question, as is evident by there being nothing after "the output is:". If you have any additional information you'd like to include in this question, please edit and add all relevant details.

    – Pikachu the Purple Wizard
    Mar 9 at 5:00











  • @SupaMegaDuckyMomodaWaffle, thanks, it was an image. Now I edit it.

    – Jiang QY.
    Mar 9 at 13:28













1












1








1








I have a numpy array whose rows is ordered and a vector. I need to insert this vector into the array if the value of the vector is smaller than some values of each array row. Furthermore, I need to get the index of the position. Can I use a vector like operation rather than two for loops?



For example, the i-th row of the array is [1,3,5,6], and the i-th value of vector is 4, the result is ([1,3,4,5], 2).
Here is the code example.



import random
import numpy as np

random.seed(0)
num_query = 2
topk = 3

distance = np.Inf * np.ones((num_query, topk))

print(distance)

for iter in range(4):
print('------------loop: :d------------'.format(iter))
dist = np.random.randn(num_query) / (0.3 * (iter+1))
dist = dist * np.sign(dist)
rows, columns = distance.shape
for row in range(rows):
d = dist[row]
if d > distance[row, columns - 1]:
continue
min_col = 0
for column in range(columns-1, -1, -1):
if distance[row, column] < d:
min_col = column + 1
break
distance[row, min_col + 1:] = distance[row, min_col:-1]
distance[row, min_col] = d
print(dist)
print(distance)
print('+++++++++++++++++++++++++++++++')
print(distance)


The output is:



[[inf inf inf]
[inf inf inf]]
------------loop: 0------------
[3.60155727 0.0991411 ]
[[3.60155727 inf inf]
[0.0991411 inf inf]]
+++++++++++++++++++++++++++++++
------------loop: 1------------
[1.4839764 1.99641269]
[[1.4839764 3.60155727 inf]
[0.0991411 1.99641269 inf]]
+++++++++++++++++++++++++++++++
------------loop: 2------------
[2.44132044 0.19221564]
[[1.4839764 2.44132044 3.60155727]
[0.0991411 0.19221564 1.99641269]]
+++++++++++++++++++++++++++++++
------------loop: 3------------
[0.29206705 1.17013653]
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]
+++++++++++++++++++++++++++++++
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]









share|improve this question
















I have a numpy array whose rows is ordered and a vector. I need to insert this vector into the array if the value of the vector is smaller than some values of each array row. Furthermore, I need to get the index of the position. Can I use a vector like operation rather than two for loops?



For example, the i-th row of the array is [1,3,5,6], and the i-th value of vector is 4, the result is ([1,3,4,5], 2).
Here is the code example.



import random
import numpy as np

random.seed(0)
num_query = 2
topk = 3

distance = np.Inf * np.ones((num_query, topk))

print(distance)

for iter in range(4):
print('------------loop: :d------------'.format(iter))
dist = np.random.randn(num_query) / (0.3 * (iter+1))
dist = dist * np.sign(dist)
rows, columns = distance.shape
for row in range(rows):
d = dist[row]
if d > distance[row, columns - 1]:
continue
min_col = 0
for column in range(columns-1, -1, -1):
if distance[row, column] < d:
min_col = column + 1
break
distance[row, min_col + 1:] = distance[row, min_col:-1]
distance[row, min_col] = d
print(dist)
print(distance)
print('+++++++++++++++++++++++++++++++')
print(distance)


The output is:



[[inf inf inf]
[inf inf inf]]
------------loop: 0------------
[3.60155727 0.0991411 ]
[[3.60155727 inf inf]
[0.0991411 inf inf]]
+++++++++++++++++++++++++++++++
------------loop: 1------------
[1.4839764 1.99641269]
[[1.4839764 3.60155727 inf]
[0.0991411 1.99641269 inf]]
+++++++++++++++++++++++++++++++
------------loop: 2------------
[2.44132044 0.19221564]
[[1.4839764 2.44132044 3.60155727]
[0.0991411 0.19221564 1.99641269]]
+++++++++++++++++++++++++++++++
------------loop: 3------------
[0.29206705 1.17013653]
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]
+++++++++++++++++++++++++++++++
[[0.29206705 1.4839764 2.44132044]
[0.0991411 0.19221564 1.17013653]]






python broadcast numpy-broadcasting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 13:26







Jiang QY.

















asked Mar 9 at 4:22









Jiang QY.Jiang QY.

62




62












  • It looks like you hit the submit button before you finished writing your question, as is evident by there being nothing after "the output is:". If you have any additional information you'd like to include in this question, please edit and add all relevant details.

    – Pikachu the Purple Wizard
    Mar 9 at 5:00











  • @SupaMegaDuckyMomodaWaffle, thanks, it was an image. Now I edit it.

    – Jiang QY.
    Mar 9 at 13:28

















  • It looks like you hit the submit button before you finished writing your question, as is evident by there being nothing after "the output is:". If you have any additional information you'd like to include in this question, please edit and add all relevant details.

    – Pikachu the Purple Wizard
    Mar 9 at 5:00











  • @SupaMegaDuckyMomodaWaffle, thanks, it was an image. Now I edit it.

    – Jiang QY.
    Mar 9 at 13:28
















It looks like you hit the submit button before you finished writing your question, as is evident by there being nothing after "the output is:". If you have any additional information you'd like to include in this question, please edit and add all relevant details.

– Pikachu the Purple Wizard
Mar 9 at 5:00





It looks like you hit the submit button before you finished writing your question, as is evident by there being nothing after "the output is:". If you have any additional information you'd like to include in this question, please edit and add all relevant details.

– Pikachu the Purple Wizard
Mar 9 at 5:00













@SupaMegaDuckyMomodaWaffle, thanks, it was an image. Now I edit it.

– Jiang QY.
Mar 9 at 13:28





@SupaMegaDuckyMomodaWaffle, thanks, it was an image. Now I edit it.

– Jiang QY.
Mar 9 at 13:28












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073961%2fpython-compare-and-insert-a-vector-with-an-ordered-numpy-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073961%2fpython-compare-and-insert-a-vector-with-an-ordered-numpy-array%23new-answer', 'question_page');

);

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







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко