तो जैसा आपने पिछले topic में पढ़ा कि NumPy का use Array के साथ काम करने के लिए किया जाता है। इस topic में आप जानेंगे कि किस तरह से numPy module का use करके Array create और use करते हैं।


NumPy में array Object बनाने के लिए array method का use किया जाता जो ndarray object return करता है।

NumPy Create Array

Copy Fullscreen Close Fullscreen Run
#import numpy module.
import numpy as np
arr = np.array([12,23,34,45])
#print it's type.
print('Type :', type(arr))
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
Type : <class 'numpy.ndarray'>

जैसा कि आप example में देख सकते हैं , कि array method numpy module का ndarray class का object return किया है।

Note : type() function , Python का predefined function है जिसका use किसी भी variable का Data Type जानने के लिए किया जाता है।

Important *

Array create करने के लिए यह जरूरी नहीं है कि , array() method में सिर्फ list ही pass की जाए , आप list , tuple या set कोई भी array-like object pass कर सकते हैं लेकिन return हमें ndarray Object ही होगा।

For Example

Copy Fullscreen Close Fullscreen Run
#import numpy module.
import numpy as np
arr_list = np.array([12,23,34,45])
arr_tuple = np.array((12,23,34,45))
arr_set = np.array({12,23,34,45})
print('List Array Type :', type(arr_list))
print('Tuple Array Type :', type(arr_tuple))
print('Set Array Type :', type(arr_set))
Output
C:\Users\Rahulkumar\Desktop\python>python module_numpy.py
List Array Type : <class 'numpy.ndarray'>
Tuple Array Type : <class 'numpy.ndarray'>
Set Array Type : <class 'numpy.ndarray'>

Array Dimensions

अभी ऊपर जो भी आपने examples आपने देखे वो सभी one dimensional Array examples थे। आप अपनी need के according कितने ही dimension का array create कर सकते हैं।

0-dimension Array

single value को लेकर बनाया गया array 0 - dimensional कहलाता है। किसी भी array में single value अपने आप में 0 - dimensional array ही होता है।
For Example :

import numpy as np
arr = np.array(90)
print(arr)

//Output : 90

1-Dimensional Array

ऐसा array जिसमे 0 - dimensional arrays as a elements हैं इसे uni-dimensional Array भी कहते हैं।-

Copy Fullscreen Close Fullscreen Run
import numpy as np
arr = np.array([12,23,3,423,2])
print(arr)
Output
C:\Users\Rahulkumar\Desktop\python>python file.py
[ 12  23   3 423   2]

2-dimensional Array

ऐसा array जिसमे 1 - dimensional arrays as a elements हों , मतलब हर एक array element एक 1 - dimensional array होता है।/p>

Copy Fullscreen Close Fullscreen Run
import numpy as np
arr = np.array([ [12,23,3],[43,2,45], [2,3,4] ])
print(arr)
Output
C:\Users\Rahulkumar\Desktop\python>python file.py
[[12 23  3]
 [43  2 45]
 [ 2  3  4]]

3-dimentional Array

ऐसा array जिसमे 2 - dimensional arrays as a elements हों यह सबसे rare array structure है।

Copy Fullscreen Close Fullscreen Run
import numpy as np
arr = np.array([ [[12,23,3],[43,2,45]], [[2,3,4],[23,34,23]] ])
print(arr)
Output
C:\Users\Rahulkumar\Desktop\python>python file.py
[[[12 23  3]
  [43  2 45]]

 [[ 2  3  4]
  [23 34 23]]]

अब आप अच्छी तरह से समझ गए होंगे कि numpy का use करके कैसे array create कर। next topic में आप पढ़ेंगे कि array elements को कैसे access करते हैं।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers