0% found this document useful (0 votes)
3 views

Array - Colab

Python

Uploaded by

meenajyoti1234jm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array - Colab

Python

Uploaded by

meenajyoti1234jm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import numpy as np

#Creating Arrays
array1=np.array([10,20,30]) # Array of integers
print (array1)

[10 20 30]

array2=np.array([5,-7.4,'a',7.2]) # Array of strings


print(array2)

['5' '-7.4' 'a' '7.2']

# arange () returns and ndarray


array8=np.arange(-2,24,4) # array having elements ([-2,2,6,10,14,18,22])
print(array8)

[-2 2 6 10 14 18 22]

array3=np.array([[2.4,3],[4.91,7],[0,-1]]) # 2D array
print(array3)

[[ 2.4 3. ]
[ 4.91 7. ]
[ 0. -1. ]]

array5=np.zeros((3,4)) # array of zeros


print(array5)

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

array6=np.ones((3,2)) # array of one's


print(array6)

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

array7=np.arange(6) # array having 6 elements ([0,1,2,3,4,5])


print(array7)

[0 1 2 3 4 5]

# Reshaping Arrays
array9=np.arange(12).reshape(4,3) # need to specify the number of rows and number of column
print(array9)
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]

# Array of random numbers


array10=np.random.randn(2,3)
print(array10)

[[ 0.20550432 -0.2685225 -0.05224929]


[ 0.95217388 0.8093504 0.53739298]]

# using seed with random


np.random.seed(7)
array10=np.random.randn(2,3)
print(array10)

[[ 1.6905257 -0.46593737 0.03282016]


[ 0.40751628 -0.78892303 0.00206557]]

# Dimensions of the array


print(array1)
print(array1.ndim)
print(array9)
print(array9.ndim)

[10 20 30]
1
[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]
2

# Slicing Arrays
arr=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
arr_slice=arr[5:8]
print(arr_slice)

[6 7 8]

# change values in arr_slice, and observe that the mutations are reflected
arr_slice[1]=12345
print(arr)

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

#The "bare" slice [:] will assign to all values in an array


arr_slice[:]=64
print(arr)

[ 1 2 3 4 5 64 64 64 9 10 11 12]
# .copy()
# As Numpy has been designed to be able to work with very large arrays,
# Imagine performance and memory problems if Numpy insisted on always copy
# If you want a copy of a slice of an ndarray instead of a view, you will
print(arr)
arr_slice2=arr[5:8]. copy()
print(arr_slice2)

[ 1 2 3 4 5 64 64 64 9 10 11 12]
[64 64 64]

# Individual elements can be accessed recursively .


# We can pass a comma-separated list of indices to select individual element
print(array9)
print(array9[1][2]) # 3rd element of second
print(array9[1,2])

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

# Inner Matrix product using np.dot:


arr=np.random.randn(6,3)
print("\narr\n\n",arr)
print( "\nnp.dot(arr.T, arr)\n\n", np.dot(arr.T,arr))

arr

[[-8.90385858e-04 -1.75472431e+00 1.01765801e+00]


[ 6.00498516e-01 -6.25428974e-01 -1.71548261e-01]
[ 5.05299374e-01 -2.61356415e-01 -2.42749079e-01]
[-1.45324141e+00 5.54580312e-01 1.23880905e-01]
[ 2.74459924e-01 -1.52652453e+00 1.65069969e+00]
[ 1.54335535e-01 -3.87139943e-01 2.02907222e+00]]

np.dot(arr.T, arr)

[[ 2.82698503 -1.79072835 0.35959846]


[-1.79072835 6.32623977 -4.85164052]
[ 0.35959846 -4.85164052 7.98127376]]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy