Showing posts with label arrays. Show all posts
Showing posts with label arrays. Show all posts

Numpy - Creating arrays

 # numpy


import numpy as np

import random


# Creating a random 2d array object of size 2 x 3


M1 = np.random.randint(10, size=(2,3))


print("Generated array object")

print(M1)


# About array object


print("About array object")

print("Type : ", type(M1))

  

# Array dimensions (axes)

print("Number of dimensions : ", M1.ndim)

  

# Shape shape of array

print("Shape of array: ", M1.shape)

  

# Size (total number of elements)

print("Size of array (total number of elements) : ", M1.size)

  

# Data type of elements in array

print("Data type : ", M1.dtype)



# Manipulation of array data


print("Data manipulation")

print("Sum of all elements : ",np.sum(M1))


print("Sum of column elements")

print(np.sum(M1,axis=0))


print("Sum of row elements")

print(np.sum(M1,axis=1))


# Transpose of the 2d array


print("Transpose")

print(M1.T)


"""

Sample output


>python 8_5_numpy1.py


Generated array object

[[1 6 9]

 [0 6 3]]

 

About array object

Type :  <class 'numpy.ndarray'>

Number of dimensions :  2

Shape of array:  (2, 3)

Size of array (total number of elements) :  6

Data type :  int32


Data manipulation

Sum of all elements :  25

Sum of column elements

[ 1 12 12]

Sum of row elements

[16  9]

Transpose

[[1 0]

 [6 6]

 [9 3]]

 

"""

Shell programming array biggest and smallest element


# Shell programming
# Read and display array.
# Find biggest and smallest in array

echo "Enter 5 values : "
for i in 0 1 2 3 4
do
read a[$i]
done

echo "Entered values are "
for (( i=0; i<5; i++))
do
echo -e "\t ${a[$i]}"
done

big=${a[0]}
small=${a[0]}

for ((i=1;i<5;i++))
do
if [ $big -lt ${a[i]} ]
then
     big=${a[$i]}
fi

if [ $small -gt ${a[$i]} ]
then
     small=${a[$i]}
fi
done

echo "Biggest is $big"
echo "Smallest is $small"