sum-24-7 -بدر الشهراني
sum-24-7 -بدر الشهراني
sum-24-7 -بدر الشهراني
Create One Dimensional Array - NumpPy NumPy - Create 1D Array One dimensional array contains elements
only in one dimension. In other words, the shape of the numpy array should contain only one value in the 1 To
create a one dimensional array in numpy, you can use either of the numpy.array(), numpy.arange(), or numpy.
linspace() functions based on the cr
1. Create 1D NumPy Array using array() function Numpy array() functions takes a list of elements as
argument and returns a one-dimensional array. In this example, we will import numpy library and use
array() function to crate a one dimensional numpy array.
import numpy as np
[ 5 8 12]
NumPy arange() function takes start, end of a range and the interval as arguments and returns a one-
dimensional array. [start, start+interval, start+2*interval, ...] In this example, we will import numpy library and
use arange() function to crate a one dimensional numpy array.
import numpy as np
[ 5 7 9 11 13]
import numpy as np
[ 5 6 7 8 9 10 11 12 13]
import numpy as np
[ 5 6 7 8 9 10 11 12 13]
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 1/8
ص11:42 2024/7/9 sum-24-5 - Colab
import numpy as np
Numpy linspace() functions takes start, end and the number of elements to be created as arguments and
creates a one-dimensional array. In this example, we will import numpy library and use linspace() function to
crate a one dimensional numpy array. Python Program
import numpy as np
import numpy as np
import numpy as np
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 2/8
ص11:42 2024/7/9 sum-24-5 - Colab
[ 5. 11.66666667 18.33333333 25. ]
[ 5. 11.66666667 18.33333333 25. ]
import numpy as np
print(arr)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
import numpy as np
print(arr)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
import numpy as np
print(arr)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
import numpy as np
print(arr)
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 3/8
ص11:42 2024/7/9 sum-24-5 - Colab
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
import numpy as np
print(arr)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
import numpy as np
print(arr)
[[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 0.]]
import numpy as np
print(arr)
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 4/8
ص11:42 2024/7/9 sum-24-5 - Colab
import numpy as np
print(arr)
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
import numpy as np
a = np.random.rand(3)
print(a)
print("----------")
a = np.random.rand(3)
print(a)
import numpy as np
print(a)
print("-----")
print(a)
print(a)
print(a)
print(a)
import numpy as np
print(a)
print("-----")
print(a)
print(a)
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 5/8
ص11:42 2024/7/9 sum-24-5 - Colab
0.77000146 0.77546191 0.0279297 0.79211155 0.40233823 0.97738271
0.56422999 0.37329387 0.07425008 0.88131305 0.85728523 0.50645869
0.42407888 0.9872956 ]
[0.9753685 0.42430802 0.87307186 0.37113292 0.76374741 0.81365548
0.77000146 0.77546191 0.0279297 0.79211155 0.40233823 0.97738271
0.56422999 0.37329387 0.07425008 0.88131305 0.85728523 0.50645869
0.42407888 0.9872956 ]
2. Create 2D array with random values To create a 2-dimensional numpy array with random values, pass
the required lengths of the array along the two dimensions to the rand() function In this example, we
will create 2-dimensional numpy array of length 2 in dimension-0, and length 4 in dimension-1 with
random values. Python Program
import numpy as np
print(a)
3. Create 3D array with random values To create a 3-dimensional numpy array with random values, pass
the lengths along three dimensions of the array to the rand() function. In this example, we will create 3-
dimensional numpy array of lengths 4, 2, 3 along the three dimensions with random values. Puchon
Program
import numpy as np
print(a)
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 6/8
ص11:42 2024/7/9 sum-24-5 - Colab
import numpy as np
print(arr)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
import numpy as np
print(output)
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
------------
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
import numpy as np
[0. 0. 0. 0. 0. 0. 0. 0.]
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 7/8
ص11:42 2024/7/9 sum-24-5 - Colab
import numpy as np
print(arr)
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
import numpy as np
arr = np.arange(10,20)
print(arr)
[10 11 12 13 14 15 16 17 18 19]
import numpy as np
arr = np.arange(10,200)
print(arr)
[ 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
190 191 192 193 194 195 196 197 198 199]
https://colab.research.google.com/drive/17XN2bbf2e2OBtx-bOvW9Qusnwgp2652D#scrollTo=E6u5_n99eJ2N&printMode=true 8/8