logo

numpy.where() في بايثون

توفر الوحدة NumPy دالة numpy.where()‎ لاختيار العناصر بناءً على الشرط. تقوم بإرجاع العناصر المختارة من a أو b حسب الحالة.

على سبيل المثال، إذا تم تمرير جميع الوسيطات -> الشرط، a & b في numpy.where()، فسوف تُرجع العناصر المحددة من a & b اعتمادًا على القيم الموجودة في المصفوفة المنطقية الناتجة عن الشرط.

إذا تم توفير الشرط فقط، فهذه الوظيفة هي اختصار للدالة np.asarray (الحالة).nonzero(). على الرغم من أنه ينبغي تفضيل غير الصفر مباشرة، لأنه يتصرف بشكل صحيح للفئات الفرعية.

جافا الفرز السريع

بناء الجملة:

 numpy.where(condition[, x, y]) 

حدود:

هذه هي المعلمات التالية في الدالة numpy.where():

الحالة: array_like، bool

لغة الآلة

إذا تم تعيين هذه المعلمة على True، فستنتج x وإلا فستنتج y.

س، ص: array_like:

تحدد هذه المعلمة القيم التي سيتم الاختيار منها. يجب أن تكون x وy وشرط قابلة للبث إلى شكل ما.

عائدات:

تقوم هذه الدالة بإرجاع المصفوفة التي تحتوي على عناصر من x حيث يكون الشرط صحيحًا وعناصر من y في مكان آخر.

يساوي CPP

المثال 1: np.where()

 import numpy as np a=np.arange(12) b=np.where(a<6,a,5*a) b < pre> <p> <strong>In the above code</strong> </p> <ul> <li>We have imported numpy with alias name np.</li> <li>We have created an array &apos;a&apos; using np.arange() function.</li> <li>We have declared the variable &apos;b&apos; and assigned the returned value of np.where() function.</li> <li>We have passed the array &apos;a&apos; in the function.</li> <li>Lastly, we tried to print the value of b.</li> </ul> <p>In the output, the values ranging from 0 to 5 remain the same as per the condition, and the other values have been multiplied with 5.</p> <p> <strong>Output:</strong> </p> <pre> array([ 0, 1, 2, 3, 4, 5, 30, 35, 40, 45, 50, 55]) </pre> <h3>Example 2: For multidimensional array</h3> <pre> import numpy as np a=np.arange(12) b=np.where([[True, False], [True, True]],[[1, 2], [3, 4]],[[9, 8], [7, 6]]) b </pre> <p> <strong>Output:</strong> </p> <pre> array([[1, 8], [3, 4]]) </pre> <h3>Example 3: Broadcasting x, y, and condition</h3> <pre> import numpy as np x, y = np.ogrid[:3, :4] a=np.where(x &gt; y, x, 10 + y) a </pre> <p> <strong>Output:</strong> </p> <pre> array([[10, 11, 12, 13], [ 1, 11, 12, 13], [ 2, 2, 12, 13]]) </pre> <p> <strong>In the above code</strong> </p> <ul> <li>We have imported numpy with alias name np.</li> <li>We have created an array &apos;a&apos; using np.arange() function. </li> <li>We declared the variable &apos;b&apos; and assigned the returned value of np.where() function.</li> <li>We have passed a multidimensional array of boolean as a condition and x and y as an integer arrays.</li> <li>Lastly, we tried to print the value of b.</li> </ul> <p>In the output, the x value has been compared to y value if it satisfied the condition, then it will be printed x value otherwise, it will print y value, which has passed as an argument in the where() function.</p> <h3>Example 4: Broadcasting specific value</h3> <pre> x=np.array([[0,1,2],[0,2,5],[0,4,8]]) y=np.where(x<4,x,-2) y < pre> <p> <strong>Output:</strong> </p> <pre> array([[ 0, 1, 2], [ 0, 2, -2], [ 0, -2, -2]]) </pre> <hr></4,x,-2)></pre></6,a,5*a)>

مثال 2: لمصفوفة متعددة الأبعاد

 import numpy as np a=np.arange(12) b=np.where([[True, False], [True, True]],[[1, 2], [3, 4]],[[9, 8], [7, 6]]) b 

انتاج:

 array([[1, 8], [3, 4]]) 

مثال 3: البث x، y، والحالة

 import numpy as np x, y = np.ogrid[:3, :4] a=np.where(x &gt; y, x, 10 + y) a 

انتاج:

 array([[10, 11, 12, 13], [ 1, 11, 12, 13], [ 2, 2, 12, 13]]) 

في الكود أعلاه

  • لقد قمنا باستيراد numpy بالاسم المستعار np.
  • لقد أنشأنا مصفوفة 'a' باستخدام الدالة np.arange().
  • لقد أعلنا عن المتغير 'b' وقمنا بتعيين القيمة التي تم إرجاعها للدالة np.where().
  • لقد مررنا مصفوفة متعددة الأبعاد من القيم المنطقية كشرط وx وy كمصفوفتين صحيحتين.
  • وأخيرًا، حاولنا طباعة قيمة b.

في الإخراج، تمت مقارنة قيمة x بقيمة y إذا استوفت الشرط، فسيتم طباعة قيمة x وإلا ستطبع قيمة y، والتي تم تمريرها كوسيطة في الدالة Where().

مثال 4: بث قيمة محددة

 x=np.array([[0,1,2],[0,2,5],[0,4,8]]) y=np.where(x<4,x,-2) y < pre> <p> <strong>Output:</strong> </p> <pre> array([[ 0, 1, 2], [ 0, 2, -2], [ 0, -2, -2]]) </pre> <hr></4,x,-2)>