sum-24-7 -بدر الشهراني

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

‫ ص‬11:42 2024/‫‏‬7/‫‏‬9 sum-24-5 - Colab

‫علي محمد علي النصيري‬

Create One Dimensional Array - NumpPy

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

# Create numpy array


a = np.array([5, 8, 12])
print(a)

[ 5 8 12]

2. Create 1D NumPy Array using arange() function

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

# Create numpy array


a = np.arange(5, 14, 2)
print(a)

[ 5 7 9 11 13]

import numpy as np

# Create numpy array


a = np.arange(5, 14, 1)
print(a)

[ 5 6 7 8 9 10 11 12 13]

import numpy as np

# Create numpy array


a = np.arange(5, 14, 1)
print(a)

[ 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

# Create numpy array


a = np.arange(100, 141, 9)
print(a)

[100 109 118 127 136]

Create 1D NumPy Array using linspace() function

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

# Create numpy array


a = np.linspace(5, 25, 6)
print(a)
print(a)
a = np.linspace(5, 25, 3)
print(a)
print(a)

[ 5. 9. 13. 17. 21. 25.]


[ 5. 9. 13. 17. 21. 25.]
[ 5. 15. 25.]
[ 5. 15. 25.]

import numpy as np

# Create numpy array


a = np.linspace(5, 25, 6)
print(a)
print(a)
a = np.linspace(5, 25, 6)
print(a)
print(a)

[ 5. 9. 13. 17. 21. 25.]


[ 5. 9. 13. 17. 21. 25.]
[ 5. 9. 13. 17. 21. 25.]
[ 5. 9. 13. 17. 21. 25.]

import numpy as np

# Create numpy array


a = np.linspace(5, 25, 4)
print(a)
print(a)
print("--------------")
a = np.linspace(5, 25, 4)
print(a)
print(a)

[ 5. 11.66666667 18.33333333 25. ]


[ 5. 11.66666667 18.33333333 25. ]
--------------

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. ]

*Create 2D Array in NumPy *

import numpy as np

# Create a 2D array with shape (3,4)


arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

print(arr)

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

import numpy as np

# Create a 2D array with shape (3,4)


shape = (5,10)
arr = np.zeros(shape)

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

# Create a 2D array with shape (3,4)


shape = (3,4)
arr = np.zeros(shape)

print(arr)

[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]

import numpy as np

# Create a 2D array with shape (3,4)


shape = (14,12)
arr = np.zeros(shape)

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

# Create a 2D array with shape (3,4)


shape = (3,4)
arr = np.ones(shape)

print(arr)

[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

import numpy as np

# Create a 2D array with shape (3,4)


shape = (20,2)
arr = np.zeros(shape)

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

# Create a 2D array with shape (3,4)


shape = (30,40)
arr = np.ones(shape)

print(arr)

[[1. 1. 1. ... 1. 1. 1.]


[1. 1. 1. ... 1. 1. 1.]
[1. 1. 1. ... 1. 1. 1.]
...
[1. 1. 1. ... 1. 1. 1.]
[1. 1. 1. ... 1. 1. 1.]
[1. 1. 1. ... 1. 1. 1.]]

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

# Create a 2D array with shape (3,4)


shape = (3,4)
arr = np.empty(shape)

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)

[0.33468655 0.89647597 0.58242161]


----------
[0.08431869 0.97441257 0.24811703]

import numpy as np

#Numpy array with random values


a = np.random.rand(3)

print(a)
print("-----")
print(a)
print(a)
print(a)
print(a)

[0.78316444 0.47466325 0.19296492]


-----
[0.78316444 0.47466325 0.19296492]
[0.78316444 0.47466325 0.19296492]
[0.78316444 0.47466325 0.19296492]
[0.78316444 0.47466325 0.19296492]

import numpy as np

#Numpy array with random values


a = np.random.rand(20)

print(a)
print("-----")
print(a)
print(a)

[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 ]
-----
[0.9753685 0.42430802 0.87307186 0.37113292 0.76374741 0.81365548

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

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

#Numpy array with random values


a = np.random.rand(21,4)

print(a)

[[0.9728228 0.60493952 0.70992727 0.47533529]


[0.7401564 0.58920467 0.07141052 0.29593457]
[0.948731 0.0800185 0.41552548 0.3853291 ]
[0.10342382 0.50486714 0.42035513 0.62259728]
[0.3725728 0.44368981 0.7567235 0.46931826]
[0.04467906 0.85213147 0.34492996 0.19159697]
[0.29387807 0.72650269 0.47124426 0.84498022]
[0.21993151 0.95359294 0.62346976 0.40589577]
[0.68652152 0.21737391 0.58166491 0.71909624]
[0.85705679 0.05560612 0.18807187 0.24575983]
[0.35550479 0.15189758 0.70033521 0.9638072 ]
[0.16042783 0.98947048 0.73583898 0.35730269]
[0.22556975 0.08619799 0.81290534 0.25960407]
[0.41554788 0.25857421 0.11070703 0.03080991]
[0.98564505 0.36620429 0.20850685 0.83212169]
[0.21336938 0.95855348 0.20324906 0.40641934]
[0.45421757 0.05213705 0.66380757 0.25138625]
[0.74886242 0.35986591 0.76211976 0.52236205]
[0.52321232 0.92126032 0.15513505 0.62644835]
[0.01516882 0.09741761 0.89198697 0.58996162]
[0.04929888 0.1082129 0.85965953 0.64530683]]

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

#Numpy array with random values


a = np.random.rand(4 ,2 ,3)

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

[[[0.08822009 0.70107741 0.15022917]


[0.29004028 0.95241479 0.09775538]]

[[0.85600433 0.90885448 0.62579327]


[0.9530647 0.50579679 0.61575154]]

[[0.9112181 0.48045952 0.08543276]


[0.00595827 0.45875638 0.99416546]]

[[0.41695663 0.8914733 0.16053188]


[0.6756383 0.49928826 0.59509986]]]

import numpy as np

#Numpy array with random values


arr = np.array ([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

print(arr)

[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]

import numpy as np

#Reshape (3, 4) array to (6, 2)


arr = np.array ([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(arr)
print("------------")
shape = (6, 2)
output = np.reshape(arr, shape)

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

#Create numpy array with zeros


a = np.zeros(8)

# print numpy array


print(a)

[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

#Create 2D numpy array with zeros


a = np.zeros((3, 4))

# print numpy array


print(a)
import numpy as np

shape [[0. 0.5)


= (3, 0. 0.]
[0. 0. 0. 0.]
arr = np.ones(shape)
[0. 0. 0. 0.]]

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

You might also like