Data Science · मराठी आवृत्ती
4.1 Arrays आणि shape
Array मधल्या प्रत्येक position वरील quantity आणि price एकमेकांशी multiply करूया. स्वतंत्र Python loop न लिहिता हे काम होतं; याला vectorised operation म्हणतात.
import numpy as np
qty = np.array([2, 1, 2, 1, 1, 3])
print(qty.shape, qty.dtype) # (6,) int64
prices = np.array([32.0, 90.0, 120.0, 58.0, 1299.0, 65.0])
line_total = qty * prices # vectorised
print(line_total)
print(line_total.sum())
आता काल्पनिक city × day orders चा 2-D array पाहू. Rows म्हणजे Pune, Nashik, Nagpur आणि columns म्हणजे तीन दिवस.
# rows: Pune, Nashik, Nagpur; cols: three days
counts = np.array([
[210, 230, 900], # Diwali spike on day 3
[120, 130, 400],
[150, 160, 450],
], dtype=np.int32)
print(counts.shape) # (3, 2) wait — (3, 3)
print(counts.mean(axis=1)) # mean per city
दुरुस्ती: या array चा shape (3, 3) आहे — 3 rows आणि 3 columns. Code मधल्या जुन्या comment मधला (3, 2) भाग योग्य नाही.
Jupyter मध्ये steps
import numpy as nprun करा.- वरचे
qtyआणिpricesतयार करा. line_total = qty * pricesआणिline_total.mean()काढा.- दिवसानुसार totals साठी
counts.sum(axis=0)print करा.
Output मध्ये position नुसार multiplication दिसेल. City-day grid चा shape (3, 3) असेल.