Ids 6 Experiments

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

1.

Creating numpy arrays

a)Python program to demonstrate basic ndarray

import numpy as np

1.Creating array object

arr = np.array( [[ 1, 2, 3],

[ 4, 2, 5]] )

print(arr) ;

2.Printing type of arr object

print("Array is of type: ", type(arr))

3.Printing array dimensions (axes)

print("No. of dimensions: ", arr.ndim)

4.Printing shape of array

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

5.Printing size (total number of elements) of array

print("Size of array: ", arr.size)

6.Printing type of elements in array

print("Array stores elements of type: ", arr.dtype)

Output :

[[ 1, 2, 3],

[ 4, 2, 5]]

Array is of type: <class 'numpy.ndarray'>

No. of dimensions: 2

Shape of array: (2, 3)

Size of array: 6

Array stores elements of type: int64


b)python program to demonstrate array of zeros

1.creating single dimensional array

import numpy as np

array_1d = np.zeros(3)

print(array_1d)

Output:

[0. 0. 0.]

Notice that the elements are having the default data type as the float. That’s why the zeros are 0.

2. Creating Multi-dimensional array

import numpy as np

array_2d = np.zeros((2, 3))

print(array_2d)

Output:

[[0. 0. 0.]

[0. 0. 0.]]

3. NumPy zeros array with int data type

import numpy as np

array_2d_int = np.zeros((2, 3), dtype=int)

print(array_2d_int)

Output:

[[0 0 0]

[0 0 0]]
4. NumPy Array with Tuple Data Type and Zeroes

We can specify the array elements as a tuple and specify their data types too.

import numpy as np

array_mix_type = np.zeros((2, 2), dtype=[('x', 'int'), ('y', 'float')])

print(array_mix_type)

print(array_mix_type.dtype)

Output:

[[(0, 0.) (0, 0.)]

[(0, 0.) (0, 0.)]]

[('x', '<i8'), ('y', '<f8')]

c) python program to demonstrate array of ones

1. Creating one-dimensional array with ones

import numpy as np

array_1d = np.ones(3)

print(array_1d)

Output:

[1. 1. 1.]

Notice that the elements are having the default data type as the float. That’s why the ones are 1. in the
array.

2. Creating Multi-dimensional array

import numpy as np

array_2d = np.ones((2, 3))

print(array_2d)

Output:
[[1. 1. 1.]

[1. 1. 1.]]

3. NumPy ones array with int data type

import numpy as np

array_2d_int = np.ones((2, 3), dtype=int)

print(array_2d_int)

Output:

[[1 1 1]

[1 1 1]]

4. NumPy Array with Tuple Data Type and Ones

We can specify the array elements as a tuple and specify their data types too.

import numpy as np

array_mix_type = np.ones((2, 2), dtype=[('x', 'int'), ('y', 'float')])

print(array_mix_type)

print(array_mix_type.dtype)

Output:

[[(1, 1.) (1, 1.)]

[(1, 1.) (1, 1.)]]

[('x', '<i8'), ('y', '<f8')]

d) python program to demonstrate random numbers in ndarray

1.using randint

import numpy as np

# if the shape is not mentioned the output will just be a random integer in the given range

rand_int = np.random.randint(5,10)
print("First array", rand_int)

rand_int2 = np.random.randint(10,90,(4,5)) # random numpy array of shape (4,5)

print("Second array", rand_int2)

rand_int3 = np.random.randint(50,75,(2,2), dtype='int64')

print("Third array", rand_int3)

Output:

First array 7

Second array [[86 10 37 84 40]

[66 49 29 73 37]

[39 87 36 20 87]

[76 86 89 69 50]]

Third array [[74 73]

[71 50]]

2.using randn

import numpy as np

rand_n = np.random.randn() # outputs a single floating point number

print("First array", rand_n)

rand_n2 = np.random.randn(4,5) # outputs an 4x5 array filled with random floating point numbers

print("Second array", rand_n2)

Output:

First array -0.16000824895754412

Second array [[ 1.50870984 -0.30902038 -0.93408267 2.85782319 1.28046521]

[-0.42138647 -0.0910151 -2.24334255 0.06135505 -0.11190143]

[-0.45479495 -0.80909493 -0.46962061 0.21875305 0.45955272]

[ 0.31418762 0.66268862 -0.27700588 -0.5103291 -0.68195657]]


3.using rand

import numpy as np

rand = np.random.rand() # outputs single floating-point value .

print("First array", rand)

rand2 = np.random.rand(3,2) # outputs an 3x2 array filled with random floating point values.

print("Second array", rand2)

Output:

First array 0.7578858928993336

Second array [[0.15647101 0.13870584]

[0.89344256 0.68226333]

[0.46247578 0.09658549]]

e)python program to demonstrate array of your choice

import numpy as np

# create an array of integers from 1 to 5

array1 = np.array([1, 2, 3, 4, 5])

# choose a random number from array1

random_choice = np.random.choice(array1)

print(random_choice)

# Output: 3

Code:

f)python program to demonstrate matrix in numpy

import numpy as np

#creating matrix from string

A = np.matrix('1 2 3; 4 5 6')
print("Array created using string is :\n", A)

Output:

1.

import numpy as np

arr = np.arange(0,10,2,float)

print(arr)

Output:

[0. 2. 4. 6. 8.]

2.

import numpy as np

arr = np.arange(10,100,5,int)

print("The array over the given range is ",arr

Output:

The array over the given range is [10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95]

2.The shape and Reshaping of numpy array

a) python program to demonstrate dimensions, shape and size of numpy array

import numpy as np

1.Creating array object

arr = np.array( [[ 1, 2, 3],

[ 4, 2, 5]] )

print(arr) ;
2.Printing type of arr object

print("Array is of type: ", type(arr))

3.Printing array dimensions (axes)

print("No. of dimensions: ", arr.ndim)

4.Printing shape of array

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

5.Printing size (total number of elements) of array

print("Size of array: ", arr.size)

6.Printing type of elements in array

print("Array stores elements of type: ", arr.dtype)

Output :

[[ 1, 2, 3],

[ 4, 2, 5]]

Array is of type: <class 'numpy.ndarray'>

No. of dimensions: 2

Shape of array: (2, 3)

Size of array: 6

Array stores elements of type: int64

b) reshaping numpy array

import numpy as np

a_1d = np.arange(3)

print(a_1d)

Output

# [0 1 2]
a_2d = np.arange(12).reshape((3, 4))

print(a_2d)

Output

# [[ 0 1 2 3]

# [ 4 5 6 7]

# [ 8 9 10 11]]

a_3d = np.arange(24).reshape((2, 3, 4))

print(a_3d)

Output

# [[[ 0 1 2 3]

# [ 4 5 6 7]

# [ 8 9 10 11]]

# [[12 13 14 15]

# [16 17 18 19]

# [20 21 22 23]]]

c) flattening numpy array

1.Using flatten() method example with a multidimensional array

import numpy as np

a = np.array([[1, 2], [3, 4]])

b = a.flatten()

print(b)

Output:

[1 2 3 4]
2.Using numpy flatten() method to flatten an array using column-major order

import numpy as np

a = np.array([[1, 2], [3, 4]])

b = a.flatten(order='F')

print(b)

Output:

[1 3 2 4]

d) transposing numpy array

import numpy as np

arr1 = np.array([[1, 2, 3], [4, 5, 6]])

print(f'Original Array:\n{arr1}')

arr1_transpose = arr1.transpose()

print(f'Transposed Array:\n{arr1_transpose}')

Output:

Original Array:

[[1 2 3]

[4 5 6]]

Transposed Array:

[[1 4]

[2 5]

[3 6]]
3.Expanding and squeezing numpy array

a)Expanding numpy array

# import numpy

import numpy as np

# using Numpy.expand_dims() method

gfg = np.array([1, 2])

print(gfg.shape)

gfg = np.expand_dims(gfg, axis = 0)

print(gfg.shape)

Output :

(2, )

(1, 2)

# import numpy

import numpy as np

# using Numpy.expand_dims() method

gfg = np.array([[1, 2], [7, 8]])

print(gfg.shape)

gfg = np.expand_dims(gfg, axis = 0)

print(gfg.shape)

Output :

(2, 2)

(1, 2, 2)
b) squeezing numpy array

import numpy as np

x = np.arange(9).reshape(1,3,3)

print 'Array X:'

print x

print '\n'

y = np.squeeze(x)

print 'Array Y:'

print y

print '\n'

print 'The shapes of X and Y array:'

print x.shape, y.shape

Array X:

[[[0 1 2]

[3 4 5]

[6 7 8]]]

Array Y:

[[0 1 2]

[3 4 5]

[6 7 8]]

The shapes of X and Y array:

(1, 3, 3) (3, 3)
c) sorting numpy array

# importing libraries

import numpy as np

# sort along the first axis

a = np.array([[12, 15], [10, 1]])

arr1 = np.sort(a, axis = 0)

print ("Along first axis : \n", arr1)

# sort along the last axis

a = np.array([[10, 15], [12, 1]])

arr2 = np.sort(a, axis = -1)

print ("\nAlong first axis : \n", arr2)

a = np.array([[12, 15], [10, 1]])

arr1 = np.sort(a, axis = None)

print ("\nAlong none axis : \n", arr1)

Output :

Along first axis :

[[10 1]

[12 15]]

Along first axis :

[[10 15]

[ 1 12]]

Along none axis :

[ 1 10 12 15]
4.Indexing and slicing numpy array

a) slicing ID arrays

# import numpy module

import numpy as np

# Create NumPy arrays

arr = np.array([3, 5, 7, 9, 11, 15, 18, 22])

# Use slicing to get 1-D arrays elements

arr2 = arr[1:6]

print(arr2)

# OutPut

# [ 5 7 9 11 15]

# Starting position from 3 to the end

arr2 = arr[3:]

print(arr2)

# Output

# [ 9 11 15 18 22]

# Returns first 5 values (0 to 4 index)

arr2 = arr[:5]

print(arr2)

# Output

# [ 3 5 7 9 11]

# Use step value to get elements

arr2 = arr[::3]

print(arr2)

# Output
#[ 3 9 18]

# Use negative slicing to get elements

arr2 = arr[-4:-2]

print(arr2)

# Output

# [11 15]

# Use slicing with interval

arr2 = arr[3::4]

print(arr2)

# Output

# [ 9 22]

# Create NumPy arrays

arr = np.array([[3, 5, 7, 9, 11],

[2, 4, 6, 8, 10]])

b) Use slicing a 2-D arrays

arr2 = arr[1:,1:3]

print(arr2)

# Output

#[[4 6]]

c) Slicing 3-D arrays

arr = np.array([[[3, 5, 7, 9, 11],

[2, 4, 6, 8, 10]],

[[5, 7, 8, 9, 2],

[7, 2, 3, 6, 7]]])
arr2 = arr[0,1,0:2]

print(arr2)

# Output:

# [2 4]

5.Stacking and Concatenating numpy array

a) stacking ndarrays

import numpy as np

a = np.array([1, 2, 3])

b = np.array([5,6,7])

np.vstack((a,b))

Output

array([[1, 2, 3],

[5, 6, 7]])

a = np.array([[1, 2],[3,4]])

b = np.array([[5,6],[7,8]])

np.hstack((a,b))

Output

array([[1, 2, 5, 6],

[3, 4, 7, 8]])

a = np.array([[1, 2],[3,4]])

b = np.array([[5,6],[7,8]])

np.dstack((a,b))

Output
array([[[1, 5],

[2, 6]],[[3, 7],

[4, 8]]])

b) Concatenating numpy array

import numpy as np

arr1 = np.array([[1, 2], [5, 6]])

arr2 = np.array([[7, 8], [3,4]])

a = np.concatenate((arr1, arr2))

print(a)

Output

[[1 2]

[5 6]

[7 8]

[3 4]]

import numpy as np

arr1 = np.array([[1, 2], [5, 6]])

arr2 = np.array([[7, 8], [3,4]])

a = np.append(arr1,arr2, axis=1)

print(a)

Output

[[1 2 7 8]

[5 6 3 4]]
c) broadcasting numpy array

import numpy as np

a = np.array([1,2,3,4])

b = np.array([10,20,30,40])

c=a*b

print c

Its output is as follows −

[10 40 90 160]

import numpy as np

a = np.array([[0.0,0.0,0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]])

b = np.array([1.0,2.0,3.0])

print 'First array:'

print a

print '\n'

print 'Second array:'

print b

print '\n'

print 'First Array + Second Array'

print a + b

First array:

[[ 0. 0. 0.]

[ 10. 10. 10.]

[ 20. 20. 20.]

[ 30. 30. 30.]]


Second array:

[ 1. 2. 3.]

First Array + Second Array

[[ 1. 2. 3.]

[ 11. 12. 13.]

[ 21. 22. 23.]

[ 31. 32. 33.]]

6.operations using pandas

a) creating dataframe

# import pandas as pd

import pandas as pd

# list of strings

lst = ['Geeks', 'For', 'Geeks', 'is',

'portal', 'for', 'Geeks']

# Calling DataFrame constructor on list

df = pd.DataFrame(lst)

print(df)

Output:

# Python code demonstrate creating


# DataFrame from dict narray / lists

# By default addresses.

import pandas as pd

# intialise data of lists.

data = {'Name':['Tom', 'nick', 'krish', 'jack'],

'Age':[20, 21, 19, 18]}

# Create DataFrame

df = pd.DataFrame(data)

# Print the output.

print(df)

Output:

# Import pandas package

import pandas as pd

# Define a dictionary containing employee data

data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],

'Age':[27, 24, 22, 32],

'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],

'Qualification':['Msc', 'MA', 'MCA', 'Phd']}


# Convert the dictionary into DataFrame

df = pd.DataFrame(data)

# select two columns

print(df[['Name', 'Qualification']])

Output

b)concate

# importing the module

import pandas as pd

# creating the Series

series1 = pd.Series([1, 2, 3])

display('series1:', series1)

series2 = pd.Series(['A', 'B', 'C'])


display('series2:', series2)

# concatenating

display('After concatenating:')

display(pd.concat([series1, series2]))

Output:

# importing the module

import pandas as pd

# creating the Series

series1 = pd.Series([1, 2, 3])


display('series1:', series1)

series2 = pd.Series(['A', 'B', 'C'])

display('series2:', series2)

# concatenating

display('After concatenating:')

display(pd.concat([series1, series2],

axis = 1))

Output:

# importing the module

import pandas as pd

# creating the DataFrames

df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],

'B': ['B0', 'B1', 'B2', 'B3']})


display('df1:', df1)

df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],

'B': ['B4', 'B5', 'B6', 'B7']})

display('df2:', df2)

# concatenating

display('After concatenating:')

display(pd.concat([df1, df2],

keys = ['key1', 'key2']))

# importing the module

import pandas as pd

# creating the DataFrames


df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],

'B': ['B0', 'B1', 'B2', 'B3']})

display('df1:', df1)

df2 = pd.DataFrame({'C': ['C0', 'C1', 'C2', 'C3'],

'D': ['D0', 'D1', 'D2', 'D3']})

display('df2:', df2)

# concatenating

display('After concatenating:')

display(pd.concat([df1, df2],

axis = 1))

Output:
d) adding new column

# Import pandas package

import pandas as pd

# Define a dictionary containing Students data

data = {'Name': ['Jai', 'Princi', 'Gaurav', 'Anuj'],

'Height': [5.1, 6.2, 5.1, 5.2],

'Qualification': ['Msc', 'MA', 'Msc', 'Msc']}

# Convert the dictionary into DataFrame


df = pd.DataFrame(data)

# Declare a list that is to be converted into a column

address = ['Delhi', 'Bangalore', 'Chennai', 'Patna']

# Using 'Address' as the column name

# and equating it to the list

df['Address'] = address

# Observe the result

print(df)

Output:

7.operations using pandas

a)filling nan with string

import pandas as pd

import numpy as np

#create DataFrame with some NaN values

df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],

'points': [np.nan, 11, 7, 7, 8, 6, 14, 15],

You might also like