Skip to content

Commit f3bf071

Browse files
authored
Create CreationOfNumpy.md
1 parent 6d3c0f2 commit f3bf071

1 file changed

Lines changed: 352 additions & 0 deletions

File tree

Lines changed: 352 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
## Numpy Array Create
2+
3+
### Import Required Module Numpy and Rename
4+
5+
6+
```python
7+
import numpy as np
8+
```
9+
10+
#### Numpy Array Using List
11+
12+
13+
```python
14+
#Define Simple List
15+
lst=[10,23,2,3,7,8]
16+
17+
#Create numpy array using lst
18+
np_array=np.array(lst)
19+
20+
#print numpy array
21+
print("Create Numpy Array Using List:\n",np_array)
22+
```
23+
Output:
24+
25+
Create Numpy Array Using List:
26+
[10 23 2 3 7 8]
27+
28+
29+
#### Numpy Array Using Tuple
30+
31+
32+
```python
33+
#Define Simple Tuple
34+
tple=(10,23,2,3,7,8)
35+
36+
#Create numpy array using tuple
37+
np_array=np.array(tple)
38+
39+
#print numpy array
40+
print("Create a Numpy Array Using Tuple:\n",np_array)
41+
```
42+
Output:
43+
44+
Create a Numpy Array Using Tuple:
45+
[10 23 2 3 7 8]
46+
47+
48+
#### Numpy String Array
49+
50+
51+
```python
52+
tple=np.array(['P','Y','T','H','O','N'])
53+
#Create numpy array using tuple
54+
np_array=np.array(tple)
55+
56+
#print numpy array
57+
print("Create Numpy Array Using Characters :\n",np_array)
58+
```
59+
Output:
60+
61+
Create Numpy Array Using Characters :
62+
['P' 'Y' 'T' 'H' 'O' 'N']
63+
64+
65+
#### Create Numpy array using arange() function
66+
67+
68+
```python
69+
#Simple Array Here Only End is Specified
70+
arr=np.arange(6)
71+
print("Array With stop Parameter: ",arr)
72+
print("Type of an Array: ",arr.dtype)
73+
74+
75+
#Create Float Array
76+
arr=np.arange(6.0)
77+
print("\nArray With stop Parameter: ",arr)
78+
print("Type of an Array: ",arr.dtype)
79+
80+
81+
#Numpy Array From Specific Range
82+
arr=np.arange(4,9) #it will include 4 and exclude 9
83+
print("\nArray With Specified Range: ",arr)
84+
print("Type of an Array: ",arr.dtype)
85+
86+
87+
#Simple Numpy array with Step counter(print 5 table)
88+
arr=np.arange(5,51,5)
89+
print("\nArray of 5 Table: ",arr)
90+
print("Type of an Array: ",arr.dtype)
91+
```
92+
Output:
93+
94+
Array With stop Parameter: [0 1 2 3 4 5]
95+
Type of an Array: int32
96+
97+
Array With stop Parameter: [0. 1. 2. 3. 4. 5.]
98+
Type of an Array: float64
99+
100+
Array With Specified Range: [4 5 6 7 8]
101+
Type of an Array: int32
102+
103+
Array of 5 Table: [ 5 10 15 20 25 30 35 40 45 50]
104+
Type of an Array: int32
105+
106+
107+
## important Terminologies Used In Numpy array
108+
109+
110+
```python
111+
#Simple list
112+
lst=[1,2,3]
113+
np_ar=np.array(lst)
114+
115+
```
116+
117+
#### Print The Numpy Array
118+
119+
120+
```python
121+
# print Normal numpy array
122+
print(np_ar)
123+
```
124+
Output:
125+
126+
[1 2 3]
127+
128+
129+
#### Get Shape of Numpy array
130+
Use the following syntax to get the shape of numpy array
131+
numpy_array.shape
132+
133+
```python
134+
#This is 1d Array So Only
135+
shape=np_ar.shape
136+
print("Shape Of an Array(row,columns):",shape)
137+
```
138+
Output:
139+
140+
Shape Of an Array(row, columns): (3,)
141+
142+
143+
#### Get element data types in numpy array
144+
numpy_array.dtype
145+
146+
```python
147+
data_type=np_ar.dtype
148+
print("Data Type Of Numpy Array:",data_type)
149+
```
150+
Output:
151+
152+
Data Type Of Numpy Array: int32
153+
154+
155+
#### Get Dimension Of Numpy Array
156+
- To get the Dimension of numpy array simple call *ndim* with numpy array
157+
numpy_array.ndim
158+
159+
```python
160+
dim=np_ar.ndim
161+
print("The dimension of the Numpy Array:",dim)
162+
```
163+
Output:
164+
165+
The dimension of the Numpy Array: 1
166+
167+
168+
#### Get Element Count(Size How many Element in numpy array)
169+
- To simply get the Count of the present element in the numpy array use *size* as follows
170+
numpy_array.size
171+
172+
```python
173+
elem_count=np_ar.size
174+
print("Element Count:",elem_count)
175+
```
176+
Output:
177+
178+
Element Count: 3
179+
180+
181+
#### Get the element Size in Bytes
182+
183+
184+
```python
185+
size=np_ar.itemsize
186+
print("Item Size in Bytes:",size)
187+
```
188+
Output:
189+
190+
Item Size in Bytes: 4
191+
192+
193+
## Type Of Numpy Array
194+
```
195+
1. Single Dimensional
196+
2. Two Dimensional
197+
3. Three Dimensional
198+
```
199+
200+
### 1. Single Dimensional
201+
202+
![1d](https://github.com/chavarera/PythonScript/blob/master/MachineLearning/Numpy/img/np1d.png)
203+
204+
```python
205+
import numpy as np
206+
#Simple list
207+
lst=[1,2,3,4]
208+
209+
np_1d=np.array(lst)
210+
print("1D Numpy Array:\n",np_1d)
211+
212+
213+
#dimensssions
214+
print("\nDimenssion of np_1d: ",np_1d.ndim)
215+
216+
#Second value from tuple is blank for shape becaue it is 1d array
217+
print("Shape Of Array(row,colums) :",np_1d.shape)
218+
219+
#Get Type of Array Elements
220+
print("Type Of Numpy Array Elements : ",np_1d.dtype)
221+
222+
#get Item Size
223+
print("Item Size: ",np_1d.itemsize)
224+
225+
#get objects Size
226+
print("Object Size: ",np_1d.size)
227+
228+
229+
```
230+
Output:
231+
232+
1D Numpy Array:
233+
[1 2 3 4]
234+
235+
Dimenssion of np_1d: 1
236+
Shape Of Array(row,colums) : (4,)
237+
Type Of Numpy Array Elements : int32
238+
Item Size: 4
239+
Object Size: 4
240+
241+
242+
### 2.Two Dimensssional
243+
![2d](https://github.com/chavarera/PythonScript/blob/master/MachineLearning/Numpy/img/np2d.png)
244+
245+
246+
```python
247+
import numpy as np
248+
#2d List
249+
lst2=[
250+
[1,2],
251+
[3,4],
252+
[5,6]
253+
]
254+
255+
np_2d=np.array(lst2)
256+
print("2D Numpy Array:\n",np_2d)
257+
258+
#dimensssions
259+
print("\nDimenssion of np_2d: ",np_2d.ndim)
260+
261+
#three rows 2 columns
262+
print("Shape Of Array(row,colums) :",np_2d.shape)
263+
264+
#Get Type of Array Elements
265+
print("Type Of Numpy Array Elements : ",np_2d.dtype)
266+
267+
#get Item Size
268+
print("Size of Object Size: ",np_2d.itemsize)
269+
270+
#get objects Size
271+
print("element Count : ",np_2d.size)
272+
273+
274+
```
275+
Output:
276+
277+
2D Numpy Array:
278+
[[1 2]
279+
[3 4]
280+
[5 6]]
281+
282+
Dimenssion of np_2d: 2
283+
Shape Of Array(row,colums) : (3, 2)
284+
Type Of Numpy Array Elements : int32
285+
Size of Object Size: 4
286+
element Count : 6
287+
288+
289+
### 3. Three dimensional
290+
![3d](https://github.com/chavarera/PythonScript/blob/master/MachineLearning/Numpy/img/np3d.png)
291+
292+
293+
```python
294+
import numpy as np
295+
296+
#3d List
297+
lst3=[
298+
[
299+
[3,4,5],
300+
[6,7,8],
301+
[9,10,11]
302+
],
303+
[
304+
[0,1,2],
305+
[12,13,14],
306+
[15,16,17]
307+
],
308+
[
309+
[18,19,20],
310+
[21,22,23],
311+
[24,25,27]
312+
]
313+
]
314+
315+
np_3d=np.array(lst3)
316+
print(np_3d)
317+
318+
#dimensssions
319+
print("\nDimenssion of np_3d: ",np_3d.ndim)
320+
321+
#three rows 2 columns
322+
print("Shape Of Array(row,colums) :",np_3d.shape)
323+
324+
#Get Type of Array Elements
325+
print("Type Of Numpy Array Elements : ",np_3d.dtype)
326+
327+
#get Item Size
328+
print("Size of Object Size: ",np_3d.itemsize)
329+
330+
#get objects Size
331+
print("Element Count: ",np_3d.size)
332+
```
333+
Output
334+
335+
[[[ 3 4 5]
336+
[ 6 7 8]
337+
[ 9 10 11]]
338+
339+
[[ 0 1 2]
340+
[12 13 14]
341+
[15 16 17]]
342+
343+
[[18 19 20]
344+
[21 22 23]
345+
[24 25 27]]]
346+
347+
Dimenssion of np_3d: 3
348+
Shape Of Array(row,colums) : (3, 3, 3)
349+
Type Of Numpy Array Elements : int32
350+
Size of Object Size: 4
351+
Element Count: 27
352+

0 commit comments

Comments
 (0)