Movatterモバイル変換


[0]ホーム

URL:


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

Commite446d13

Browse files
committed
2024/03/26 - Updating Demo python files
1 parent457f166 commite446d13

File tree

2 files changed

+251
-0
lines changed

2 files changed

+251
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# This file provides demo for numpy module
2+
importnumpyasnp
3+
importrandom
4+
5+
6+
definitialisation_demo():
7+
# To create empty array
8+
arr=np.zeros(5,'i')
9+
print("Empty arr: ",arr," dtype:",arr.dtype," dimension: ",arr.ndim)
10+
11+
str_arr=np.zeros(5,"O")
12+
print("Empty string arr: ",str_arr," dtype:",str_arr.dtype," dimension: ",str_arr.ndim)
13+
14+
arr=np.ones(5,'i')
15+
print("Array with ones : ",arr," dtype:",arr.dtype," dimension: ",arr.ndim)
16+
17+
str_arr=np.full(5,"Test",object)
18+
print("String arr with default values: ",str_arr," dtype:",str_arr.dtype," dimension: ",str_arr.ndim)
19+
20+
zero_like_arr=np.zeros_like(arr)
21+
print("zero_like_arr: ",zero_like_arr," dtype:",
22+
zero_like_arr.dtype," dimension: ",zero_like_arr.ndim)
23+
24+
one_like_arr=np.ones_like(str_arr,dtype='f',shape=2)
25+
print("one_like_arr: ",one_like_arr," dtype:",one_like_arr.dtype,
26+
" dimension: ",one_like_arr.ndim)
27+
28+
full_like_arr=np.full_like(one_like_arr,fill_value="Hi",dtype=object,shape=[1,2])
29+
print("full_like_arr: ",full_like_arr," dtype:",full_like_arr.dtype,
30+
" dimension: ",full_like_arr.ndim)
31+
32+
33+
defone_dimensional_array():
34+
num_list=random.sample(range(0,100),7)
35+
arr=np.array(num_list,'i')
36+
print("Accessing elements using indexing of one dimensional array")
37+
foriinrange(arr.size):
38+
print(f"arr[{i}]:{arr[i]}",end="\t")
39+
40+
print("")
41+
print("Updating value at 2nd position to 39")
42+
arr[1]=39
43+
print("Post updating value at 2nd position: ",arr)
44+
45+
print("Updating values at more than one position using slicing "
46+
" i.e. position 6 and 7 ")
47+
arr[5:7]=29
48+
print("Post updating value at 6th and 7th position: ",arr)
49+
50+
print("Array from 2nd element till 7th element "
51+
"with skip = 2 "
52+
"using slicing: ",arr[1:6:2])
53+
54+
print("To access array using negative indexing ",arr[-3:])
55+
56+
print("To reverse array ",arr[::-1])
57+
58+
59+
deftwo_dimensional_arr():
60+
# empty array with zero values
61+
arr=np.zeros((2,2),dtype='i')
62+
print("2-D array with zero values ",arr)
63+
64+
# initialising array with values
65+
arr=np.array([[1,2], [3,4], [5,6], [7,8], [9,-1]],dtype='i')
66+
print("2-D array with initial values ",arr," dimension: ",arr.ndim,
67+
" Data type: ",arr.dtype," size of array: ",arr.size,
68+
"shape: ",arr.shape)
69+
70+
# accessing elements using index
71+
print("element at 1st row and 2nd column: ",arr[1,1])
72+
73+
# updating value at particular position
74+
print("update value at 1st row and 2nd column to 9")
75+
arr[1,1]=9
76+
print("Array post updating element at 1st row and 2nd column")
77+
print(arr)
78+
79+
# updating value at multiple position
80+
print("update value at 2nd row: 7")
81+
arr[2:3]=7
82+
print("Array post updating element at 3rd row")
83+
print(arr)
84+
85+
new_arr=arr[1:4:2]
86+
# get 2-D array from existing one
87+
print("Getting new 2-D array from existing one using"
88+
"slicing",new_arr," shape: ",new_arr.shape)
89+
90+
new_arr=arr[1:4:2,0:1]
91+
# get 2-D array from existing one with one column
92+
print("Getting new 2-D array with one column "
93+
"from existing one using"
94+
"slicing",new_arr," shape: ",new_arr.shape)
95+
96+
# reverse 2-D array
97+
print("Reverse 2-D array ",arr[::-1])
98+
99+
100+
defcopy_and_view_demo():
101+
arr=np.array([1,2,3,4],dtype=int)
102+
print("Original arr : ",arr," base: ",arr.base)
103+
arr1=arr.copy()
104+
print("Copied arr : ",arr1," base: ",arr1.base)
105+
arr2=arr.view()
106+
print("Viewed arr : ",arr2," base: ",arr2.base)
107+
108+
print("updating element at 1st index "
109+
"in copied array with value 5 and"
110+
"view with value 7")
111+
112+
arr1[1]=5
113+
arr2[2]=7
114+
115+
print("post update")
116+
print(f"original array:{arr}")
117+
print(f"Copied array:{arr1}")
118+
print(f"view:{arr2}")
119+
120+
121+
defshapeDemo():
122+
arr=np.full((3,2),1,dtype='i')
123+
foriinarr:
124+
forjini:
125+
print(j,end="\t")
126+
print("")
127+
128+
129+
defreShapeDemo():
130+
131+
num_list=random.sample(range(1,100),7)
132+
arr=np.array(num_list,dtype='i')
133+
print(f"Input array before resize:{arr} with shape:{arr.shape} and base:{arr.base}")
134+
135+
try:
136+
reshaped_arr=np.reshape(arr,newshape=(2,4))
137+
138+
exceptValueErrorasv:
139+
print("Error received : ",v)
140+
print("Need to resize now input array !!!")
141+
142+
arr=np.resize(arr,8)
143+
print(f"Input array post resize:{arr} with shape:{arr.shape} and base:{arr.base}")
144+
145+
reshaped_arr=np.reshape(arr,newshape=(2,4))
146+
print(f"Reshaped array with new shape:{reshaped_arr.shape} and base:{reshaped_arr.base}")
147+
foriinreshaped_arr:
148+
forjini:
149+
print(j,end="\t")
150+
print("")
151+
152+
finally:
153+
print("-"*50)
154+
155+
defreShapeDemo2():
156+
arr=np.array([[1,2,3], [4,5,6]],dtype='i')
157+
print(f"Input array before reshape:{arr} with shape:{arr.shape} and base:{arr.base}")
158+
arr1=arr.flatten()
159+
arr2=arr.ravel()
160+
print("Flatten existing array: ",arr1," with base: ",arr1.base)
161+
print("ravel existing array: ",arr2," with base: ",arr2.base)
162+
163+
164+
defitrDemo():
165+
arr=np.array([[1,2,3], [4,5,6]],dtype='i')
166+
forxinnp.nditer(arr[::, :2],flags=['buffered'],op_dtypes='S'):
167+
print(x)
168+
169+
defjoinDemo():
170+
arr1=np.array([1,2,3,4,5],dtype='i')
171+
arr2=np.array([7,8,9,10,11],dtype='i')
172+
173+
# using concatenate
174+
print(np.concatenate((arr1,arr2)))
175+
176+
# using stack
177+
print(np.stack((arr1,arr2),axis=1))
178+
179+
# using hstack
180+
print(np.hstack((arr1,arr2)))
181+
182+
# using vstack
183+
print(np.vstack((arr1,arr2)))
184+
185+
defmain():
186+
print("Version of numpy",np.__version__)
187+
reShapeDemo()
188+
189+
190+
if__name__=="__main__":
191+
main()
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
fromnumpyimportrandom
2+
importnumpyasnp
3+
4+
5+
defgenerateRandomNumbers():
6+
print("Random int: ",random.randint(0,10))
7+
print("Random float between 0 and 1: ",random.rand())
8+
print("Random float between 10 and 20: ",random.uniform(10,20))
9+
print("-"*25)
10+
11+
12+
defgenerateRandomArrays():
13+
print("One Dimensional array")
14+
print("Random int array: ",random.randint(low=5,high=12,size=5))
15+
print("Random float array: ",random.rand(3))
16+
print("Random float array of 5 elements between 10 and 20: ",random.uniform(10,20,5))
17+
print("-"*25)
18+
19+
20+
defgenerateRandomArrays2():
21+
print("Two Dimensional array")
22+
print("Random int array: ",random.randint(low=5,high=12,size=(2,3)))
23+
print("Random float array: ",random.rand(3,2))
24+
print("Random float array of 5 elements between 10 and 20: ",random.uniform(10,20, (2,2)))
25+
print("-"*25)
26+
27+
28+
defgenerateRandomArraysWithChoice():
29+
print("generateRandomArraysWithChoice")
30+
print("Random int array with choice : ",random.choice([10,20,30,40,11],3))
31+
print("Random int array with choice and probability: ",random.choice([10,20,30],6,p=[0.25,0.45,0.30]))
32+
print("Random float array with choice : ",random.choice([10.5,12.0,11.25,31.5],3))
33+
print("-"*25)
34+
35+
defshuffle_and_permutation_demo():
36+
arr=np.array([1,2,3,4,5])
37+
print("Array before shuffling: ",arr)
38+
random.shuffle(arr)
39+
print("Array post shuffling: ",arr)
40+
41+
arr=np.array([6,7,8,9])
42+
print("Input Array before permutation: ",arr)
43+
permutation_arr=random.permutation(arr)
44+
print("Input Array post permutation: ",arr)
45+
print("Permutation Array post permutation: ",permutation_arr)
46+
print("-"*25)
47+
48+
49+
50+
51+
defmain():
52+
print("Version of numpy",np.__version__)
53+
generateRandomNumbers()
54+
generateRandomArrays()
55+
generateRandomArrays2()
56+
generateRandomArraysWithChoice()
57+
shuffle_and_permutation_demo()
58+
59+
if__name__=="__main__":
60+
main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp