Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Data Analysis using Python/Numpy Excercise.ipynb
3074 views
Kernel: Python 3

Write a NumPy program to test whether none of the elements of a given array is zero.

import numpy as np x = np.array([1, 2, 3, 4]) print("Original array:",x) print("Test if none of the elements of the said array is zero\n:",np.all(x)) y = np.array([0, 1, 2, 3]) print("Original array:",y) print("Test if none of the elements of the said array is zero:\n",np.all(y))
Original array: [1 2 3 4] Test if none of the elements of the said array is zero : True Original array: [0 1 2 3] Test if none of the elements of the said array is zero: False

Write a NumPy program to create an array of 5 zeros, 5 ones, 5 fives.

import numpy as np a1=np.zeros(5) a2=np.ones(5) a3=np.ones(5)*5 np.concatenate((a1,a2,a3), axis=0)
array([0., 0., 0., 0., 0., 1., 1., 1., 1., 1., 5., 5., 5., 5., 5.])

Write a NumPy program to generate an array of 15 random numbers from a standard normal distribution

import numpy as np rand_num = np.random.normal(0,1,15) print("15 random numbers from a standard normal distribution:") print(rand_num)

Write a NumPy program to create a vector with values ranging from 10 to 40 and print all values except the first and last.

import numpy as np v = np.arange(10,40) print("Original vector:") print(v) print(" \nExcluding first and last") print(v[1:-1])

Write a NumPy program to create a vector with values from 1 to 15 and change the sign of the numbers in the range from 10 to 15.

import numpy as np x = np.arange(15) print("Original vector:") print(x) print("After changing the sign of the numbers in the range from 9 to 15:") x[(x >= 10) & (x <= 15)] *= -1 print(x)

Write a NumPy program to create a 3x4 matrix filled with values from 10 to 21

import numpy as np a= np.arange(10,22).reshape((3, 4)) print(a)
[[10 11 12 13] [14 15 16 17] [18 19 20 21]]

Write a NumPy program to compute the inner product of two given vectors

import numpy as np x = np.array([4, 5]) y = np.array([7, 10]) print("Original vectors:") print(x) print(y) print("Inner product of said vectors:") print(np.dot(x, y))

Write a NumPy program to create a structured array from given student name, height, class and their data types. Now sort the array on height.

import numpy as np data_type = [('name', 'S15'), ('class', int), ('height', float)] students_details = [('James', 5, 48.5), ('Nail', 6, 52.5),('Paul', 5, 42.10), ('Pit', 5, 40.11)] # create a structured array students = np.array(students_details, dtype=data_type) print("Original array:") print(students) print("Sort by height") print(np.sort(students, order='height'))

Write a NumPy program to add, subtract, multiply, divide arguments element-wise.

import numpy as np print("Add:") print(np.add(1.0, 4.0)) print("Subtract:") print(np.subtract(1.0, 4.0)) print("Multiply:") print(np.multiply(1.0, 4.0)) print("Divide:") print(np.divide(1.0, 4.0))

Write a NumPy program to get the minimum and maximum value of a given array along the second axis.

import numpy as np x = np.arange(4).reshape((2, 2)) print("\nOriginal array:") print(x) print("\nMaximum value along the second axis:") print(np.amax(x, 1)) print("Minimum value along the second axis:") print(np.amin(x, 1))

Write a NumPy program to display all the dates for the month of May, 2019

import numpy as np print("May, 2019") print(np.arange('2019-05', '2019-06', dtype='datetime64[D]'))

Write a NumPy program to capitalize the first letter, lowercase, uppercase, swapcase, title-case of all the elements of a given array

import numpy as np x = np.array(['ml', 'PYTHON', 'Data Science', 'Numpy'], dtype=np.str) print("Original Array:") print(x) capitalized_case = np.char.capitalize(x) lowered_case = np.char.lower(x) uppered_case = np.char.upper(x) swapcased_case = np.char.swapcase(x) titlecased_case = np.char.title(x) print("\nCapitalized: ", capitalized_case) print("Lowered: ", lowered_case) print("Uppered: ", uppered_case) print("Swapcased: ", swapcased_case) print("Titlecased: ", titlecased_case)