Data Science · मराठी आवृत्ती · Python basics
P12.2 NumPy Arrays कसे बनवायचे: array, arange, zeros, linspace
हे का शिकायचं?
Data नेहमी list मधून येत नाही; कधी number ची रांग किंवा शून्यांचा array हवा असतो.
हे काय आहे?
np.array(list), np.arange(start, stop, step), np.zeros(n) आणि np.linspace(a, b, n). .shape आणि .dtype ने आकार आणि type कळतो.
कसं करायचं?
Step 1. योग्य function निवडा.
Step 2. shape आणि dtype तपासा.
उदाहरणं: copy करा आणि चालवा
उदाहरण A: बनवा आणि तपासा
हा code चालवा:
import numpy as np
rev = np.array([12.5, 14.0, 13.2, 15.8])
print(rev)
print(rev.shape, rev.dtype)
print(rev[0], rev[-1])
तुम्हाला दिसणारा output:
[12.5 14. 13.2 15.8]
(4,) float64
12.5 15.8
याचा अर्थ: shape ने लांबी आणि dtype ने type कळला.
उदाहरण B: Ranges आणि zeros
हा code चालवा:
import numpy as np
days = np.arange(1, 8)
counts = np.zeros(7)
print(days)
print(counts)
तुम्हाला दिसणारा output:
[1 2 3 4 5 6 7]
[0. 0. 0. 0. 0. 0. 0.]
याचा अर्थ: arange ने numbers ची रांग आणि zeros ने शून्यांचा array मिळाला.