Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7670fbc

Browse files
Intial Commit
1 parent 4bb9865 commit 7670fbc

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

‎NumPy/2_Arrays.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,96 @@
1+
# NumPy is used to work with arrays. The array object in NumPy is called ndarray. We can create a NumPy ndarray object by using the array() function.
2+
# 1 Introction to numpy
3+
import numpy as np
4+
arr = np.array([1, 2, 3, 4, 5])
5+
print(arr)
6+
print(type(arr))
7+
"""
8+
output:
9+
[1 2 3 4 5]
10+
<class 'numpy.ndarray'>
11+
"""
12+
_________________________________________________
113

14+
# Use a tuple to create a NumPy array:
15+
import numpy as np
16+
arr = np.array((1, 2, 3, 4, 5))
17+
print(arr) #[1 2 3 4 5]
18+
__________________________________________________
19+
20+
# Dimensions in Arrays :
21+
"""
22+
A dimension in arrays is one level of array depth (nested arrays).
23+
nested array: are arrays that have arrays as their elements.
24+
> 0-D Arrays :
25+
0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
26+
"""
27+
import numpy as np
28+
arr = np.array(42)
29+
print(arr) # 42
30+
31+
# 1-D Arrays :An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
32+
33+
import numpy as np
34+
arr = np.array([1, 2, 3, 4, 5])
35+
print(arr) #[1 2 3 4 5]
36+
37+
#2-D Arrays
38+
"""
39+
An array that has 1-D arrays as its elements is called a 2-D array.
40+
These are often used to represent matrix or 2nd order tensors.
41+
NumPy has a whole sub module dedicated towards matrix operations called numpy.mat
42+
"""
43+
import numpy as np
44+
arr = np.array([[1, 2, 3], [4, 5, 6]])
45+
print(arr)
46+
# output [[1 2 3][4 5 6]]
47+
48+
# 3-D arrays
49+
"""
50+
An array that has 2-D arrays (matrices) as its elements is called 3-D array.
51+
These are often used to represent a 3rd order tensor.
52+
"""
53+
import numpy as np
54+
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
55+
print(arr)
56+
"""
57+
[[[1 2 3]
58+
[4 5 6]]
59+
60+
[[1 2 3]
61+
[4 5 6]]]
62+
"""
63+
64+
# Check Number of Dimensions : NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
65+
import numpy as np
66+
a = np.array(42)
67+
b = np.array([1, 2, 3, 4, 5])
68+
c = np.array([[1, 2, 3], [4, 5, 6]])
69+
d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])
70+
print(a.ndim)
71+
print(b.ndim)
72+
print(c.ndim)
73+
print(d.ndim)
74+
"""
75+
Output:
76+
0
77+
1
78+
2
79+
3
80+
"""
81+
# Higher Dimensional Arrays
82+
"""
83+
An array can have any number of dimensions.
84+
When the array is created, you can define the number of dimensions by using the ndmin argument.
85+
Example
86+
Create an array with 5 dimensions and verify that it has 5 dimensions:
87+
"""
88+
import numpy as np
89+
arr = np.array([1, 2, 3, 4], ndmin=5)
90+
print(arr)
91+
print('number of dimensions :', arr.ndim)
92+
93+
"""
94+
[[[[[1 2 3 4]]]]]
95+
number of dimensions : 5
96+
"""

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /