Numpy overview: array(), arange(), linspace(), flatten(), reshape(), sum(), average(), min(), max()

NumPy ધીમા Python loops ને ચુસ્ત રીતે ગોઠવાયેલા numerical arrays થી બદલી નાખે છે જે વિશાળ datasets ને compiled C code ની ઝડપે process કરે છે.

12 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Python List નો Speed Bottleneck

તમારા Semester 2 BCA204 collections labs માં, તમે શીખ્યા કે Python lists ખૂબ જ flexible છે કારણ કે એ mixed data types રાખે છે અને dynamically વિસ્તરે છે. પણ, જ્યારે હજારો transaction amounts કે laboratory observations સંભાળવાના હોય, ત્યારે standard Python lists ભયંકર ધીમી પડી જાય છે. કારણ કે list items અલગ અલગ type wrappers સાથે machine memory માં અસ્તવ્યસ્ત વેરાયેલા હોય છે, એમને process કરવા ધીમા element-by-element iteration loops જોઈએ. NumPy આ વિખરાયેલા storage layout ને એક કડક, single-type data block થી સંપૂર્ણપણે કેમ બદલી નાખે છે જે mathematical calculations સેંકડો ગણી ઝડપથી ચલાવે છે?

Theory

વેરાયેલા Envelopes સામે નક્કર Egg Tray

એક standard Python list ને એક ઘરના જુદા જુદા ઓરડાઓમાં વેરાયેલા અલગ અલગ postal envelopes ના સંગ્રહ જેવી વિચારો; દરેક envelope માં એક કાગળની ચબરખી હોય છે જે એક શબ્દથી લઈને એક decimal number સુધી કંઈ પણ રાખી શકે. બધા વાંચવા, તમારે એક પછી એક દરેક જગ્યાએ ચાલીને જવું પડે. એક NumPy array એક ભારે, ઢાળેલી concrete egg tray જેવી છે જ્યાં દરેક ખાનું બિલકુલ એક જ કદનું છે અને માત્ર એક ચોક્કસ પ્રકારની વસ્તુ (જેમ કે integers) રાખે છે. કારણ કે ખાનાં એક જ સળંગ block માં એકબીજા સાથે બરાબર ચોંટીને બેસે છે, એક machine આખી હરોળ પર એક જ high-speed operation માં ફરી વળી શકે છે.

Theory

NumPy Array Mechanics ઔપચારિક રીતે

NumPy (Numerical Python) library ndarray (N-dimensional array) રજૂ કરે છે, એક ઝડપી, જગ્યા બચાવતું multidimensional sequence container. Standard lists થી વિપરીત, એક NumPy array સખ્તાઈથી homogeneous છે, અંદરના દરેક element નો data type બિલકુલ એક જ હોવો જોઈએ (જેમ કે float64 કે int32). Arrays પાસે એક નિશ્ચિત સંરચનાત્મક configuration હોય છે જે એમના shape attribute (દરેક dimension નું કદ દર્શાવતું એક tuple) અને એમના dtype (આંતરિક data type allocation wrapper) થી વ્યાખ્યાયિત થાય છે.

At a glance

Table 1: મુખ્ય NumPy સંરચનાત્મક initialization અને shape transformation operations.

Creation અને Shape ToolFunctional Mathematical ક્રિયાPocketMoney Budget Blueprint
np.array(sequence)Standard sequences (lists/tuples) ને ઝડપી ndarray container માં ફેરવે છે.np.array([30, 50, 120])
np.arange(start, stop, step)એક half-open range ની અંદર સરખા અંતરે values ધરાવતી array generate કરે છે.np.arange(0, 100, 20) -> [0, 20, 40, 60, 80]
np.linspace(start, stop, num)એક closed span પર સરખા અંતરે fractional values ની નિર્દિષ્ટ સંખ્યા generate કરે છે.np.linspace(0, 10, 5) -> [0. , 2.5, 5. , 7.5, 10.]
matrix.reshape(rows, cols)આંતરિક ક્રમિક values બદલ્યા વગર dimension shapes ફરીથી ગોઠવે છે.arr.reshape(2, 3) એક 6-item લીટીને એક grid matrix માં બદલે છે.
matrix.flatten()એક multidimensional grid matrix ને પાછું એક 1D sequence લીટીમાં સંકોચે છે.grid.flatten() એક grid ને પાછું એક line sequence માં ફેરવે છે.

Theory

Worked Example: Ledger Metrics ને ફરીથી ગોઠવવા

ચાલો જોઈએ કે આપણો ચાલુ project, PocketMoney, financial logs ને સંરચિત કરવા NumPy arrays કેવી રીતે વાપરે છે. આપણે tracking IDs ની એક સળંગ range initialize કરીશું, સંરચનાત્મક grids ગણીશું, અને એકંદર financial habits આંકવા sum(), average(), min(), અને max() જેવી high-speed aggregation methods લાગુ કરીશું.

Practical

PocketMoney Financial Array Matrix

import numpy as np

# Step 1: Initialize a list of transaction values into a fast homogeneous array
spends_arr = np.array([30, 45, 60, 25, 90, 50])

# Step 2: Restructure the 6-element line array into a 2-row, 3-column matrix grid
spend_matrix = spends_arr.reshape(2, 3)

# Step 3: Run fast structural statistical aggregations
total_outflow = np.sum(spend_matrix)
mean_expense  = np.average(spend_matrix)
peak_purchase = np.max(spend_matrix)

print("Reshaped Matrix:\n", spend_matrix)
print("Total Sum of Outflow:", total_outflow)
print("Average Spend Metric:", mean_expense)
print("Maximum Single Spend:", peak_purchase)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Grid Transformation ને ટ્રેસ કરો

NumPy array transformations ધ્યાનથી તપાસો. .reshape(2, 3) call દરમિયાન elements rows અને columns માં કેવી રીતે વહેંચાશે? ગણાયેલી statistical values શું છે?

Show the answer

script output કરશે:

Reshaped Matrix:

[[30 45 60]

[25 90 50]]

Total Sum of Outflow: 300

Average Spend Metric: 50.0

Maximum Single Spend: 90

કેમ? numbers ની 1D લીટી એક 2x3 matrix grid structure માં હરોળ-દર-હરોળ ભરાય છે. sum operation બધા elements ઉમેરે છે (30+45+60+25+90+50 = 300). average ગણતરી એ કુલ સરવાળાને 6 કુલ ખાનાંથી ભાગે છે, 50.0 પાછું આપતાં. આખા સંગ્રહની અંદરની મહત્તમ value સ્પષ્ટપણે 90 તરીકે ઓળખાય છે.

Quiz

જો તમે બરાબર 8 elements ધરાવતી એક 1D NumPy array ને declaration command matrix.reshape(3, 3) વાપરીને એક matrix structure માં reshape કરવાનો પ્રયાસ કરો તો શું થાય છે?

  1. એ એક 3x3 matrix બનાવે છે, છેલ્લા ખૂટતા block ખાનાંને 0 થી ભરતાં.
  2. એ 3x3 dimensions માં સાફ બેસવા છેલ્લા elements કાપી નાખે છે.
  3. એ એક ValueError ફેંકે છે કારણ કે target shape નું કદ મૂળ array ના કદ સાથે મળવું જ જોઈએ.
  4. એ array ને dynamically એક list collection માં બદલી નાખે છે.
Show the answer

એ એક ValueError ફેંકે છે કારણ કે target shape નું કદ મૂળ array ના કદ સાથે મળવું જ જોઈએ.

.reshape() થી array dimensions બદલતી વખતે, નવી rows અને columns ના ગુણાકારનું કુલ કદ elements ની મૂળ સંખ્યા સાથે બિલકુલ સરખું હોવું જ જોઈએ. એક 3x3 layout ને બરાબર 9 elements જોઈએ. 9 ખાનાંમાં 8 elements બેસાડવાનો પ્રયાસ તરત એક runtime ValueError trigger કરે છે.

Quiz

આ generation expression વિચારો: items = np.arange(5, 20, 5). આ array object ની અંદર કયા elements generate થશે?

  1. [5, 10, 15, 20]
  2. [5, 10, 15]
  3. [10, 15, 20]
  4. [5, 6, 7, 8, 9]
Show the answer

[5, 10, 15]

np.arange() function એક half-open mathematical boundary range વાપરે છે જ્યાં ઉપરની stop limit હંમેશા બહાર રખાય છે. 5 થી શરૂ કરીને 5 ના એક step value થી increment કરતાં 5, 10, અને 15 generate થાય છે, પણ 20 ની boundary limit ને અડતાં પહેલાં જ અટકી જાય છે.

Watch out

Classic ફાંદો: ચૂપચાપ Text માં રૂપાંતર

university laboratory examinations માં સૌથી વારંવાર marks-ગુમાવતી ભૂલ એ છે mixed types વાળી એક standard Python list (જેમ કે [10, 20, 'Samosa']) સીધી np.array() માં આપવી. NumPy arrays દરેક cell માટે અલગ data types જાળવી શકતી નથી. Crash થવાને બદલે, NumPy ચૂપચાપ દરેકે દરેક item ને એક text string wrapper માં ફેરવી નાખે છે, જેથી sum() કે average() જેવાં તમારાં mathematical tools તરત crash થાય છે કારણ કે integers ફરીથી text તરીકે લખાઈ ગયા હોય છે!

Theory

Matrices ને Semester 3 સાથે જોડવા

High-performance vector matrices automated analytics ના મુખ્ય mathematical layers બનાવે છે. Semester 3 (BCA302/BCA303) માં, જ્યારે multidimensional data tables, images, કે sensor feeds process કરશો, ત્યારે તમે તમારા raw data records ને NumPy arrays ની અંદર વીંટાળશો. આ એક પણ nested loop લખ્યા વગર આખા dataset પર એકસાથે high-speed multi-row calculations શક્ય બનાવે છે.

Summary

Key takeaways

  • NumPy ndarrays અતિશય processing speed માટે numbers ને computer memory ના સળંગ blocks માં સંઘરે છે.
  • Arrays સખ્તાઈથી homogeneous છે, એટલે કે દરેક item નો data primitive type એકસરખો હોવો જોઈએ.
  • arange method નિર્દિષ્ટ steps સાથે sequences બનાવે છે, જ્યારે linspace ranges ને ચોક્કસ fractional ટુકડાઓમાં વહેંચે છે.
  • reshape અને flatten tools નીચેના data elements ફરીથી ફાળવ્યા વગર multidimensional સીમાઓ બદલે છે.
  • sum, average, min, અને max જેવી statistical methods જટિલ matrices પર તરત values એકઠી કરે છે.
  • Memory Hook: Python lists વેરાયેલા envelopes છે; NumPy arrays નક્કર concrete trays છે!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Libraries

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Numpy overview: array(), arange(), linspace(), flatten(), reshape(), sum(), average(), min(), max() · Programming Skills · Gri-Learn