Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
104 views

Assignment 4 - Jupyter Notebook

This document contains the code solutions to 9 questions on NumPy. It creates and manipulates NumPy arrays, calculates statistics, and plots data. Key steps include creating integer and random arrays, selecting columns, inserting/deleting columns, defining functions to analyze arrays and plot trigonometric functions, reading in CSV data and plotting it, and calculating BMI from the height and weight data.

Uploaded by

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

Assignment 4 - Jupyter Notebook

This document contains the code solutions to 9 questions on NumPy. It creates and manipulates NumPy arrays, calculates statistics, and plots data. Key steps include creating integer and random arrays, selecting columns, inserting/deleting columns, defining functions to analyze arrays and plot trigonometric functions, reading in CSV data and plotting it, and calculating BMI from the height and weight data.

Uploaded by

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

4/30/22, 2:32 PM Assignment 4 - Jupyter Notebook

Name:Bivek Kumar sah


Section:D

1.Create a 3 X 5 integer array (any values) and print its attributes (length, size, dimensions).

In [1]:  import numpy as np


a=np.arange(0,15).reshape(3,5)
a

Out[1]: array([[ 0, 1, 2, 3, 4],

[ 5, 6, 7, 8, 9],

[10, 11, 12, 13, 14]])

In [2]:  len(a)

Out[2]: 3

In [3]:  a.ndim

Out[3]: 2

In [4]:  a.size

Out[4]: 15

2.Using NumPy Create a 4 X 5 integer array from a range between 100 to 200 such that
thedifference between each element is 5.

Expected_Output:
[[100 105 110 115 120]
[125 130 135 140 145]
[150 155 160 165 170]
[175 180 185 190 195]]

In [5]:  import numpy as np


a=np.arange(100,200,5).reshape(4,5)
print(a)

[[100 105 110 115 120]

[125 130 135 140 145]

[150 155 160 165 170]

[175 180 185 190 195]]

3.Given the following Numpy array: my_array = np.array([[11 ,22, 33], [44, 55,
66], [77, 88, 99]]) ,
Write a Python syntax to return array of items in the second column from all
rows. Your codeshould return the following
output: [22 55 88]

localhost:8888/notebooks/Downloads/Numpy_Studentcopy/Numpy_Studentcopy/Assignment 4.ipynb# 1/6


4/30/22, 2:32 PM Assignment 4 - Jupyter Notebook

In [6]:  import numpy as np


my_array = np.array([[11 ,22, 33], [44, 55, 66], [77, 88, 99]])
print(my_array)

[[11 22 33]

[44 55 66]

[77 88 99]]

In [7]:  print(my_array[::,1])

[22 55 88]

4.Delete the third column from the given array my_array and insert the
following new_column in its place.
Original array:
[[54 23 89]
[80 12 24]
[37 49 71]]
Expected Output:
[[54 23 11]
[80 12 11]
[37 49 11]]

In [8]:  import numpy as np


my_arrays=np.array([[54, 23, 89],
[80, 12, 24],
[37, 49, 71]])

In [9]:  my_arrays[::,2]=11

In [10]:  print(my_arrays)

[[54 23 11]

[80 12 11]

[37 49 11]]

5.Write a function random_array(size, minVal=0, maxVal=1) which returns a NumPy


array of the given size containing random
numbers in the interval given by the defaultarguments minVal and maxVal.

localhost:8888/notebooks/Downloads/Numpy_Studentcopy/Numpy_Studentcopy/Assignment 4.ipynb# 2/6


4/30/22, 2:32 PM Assignment 4 - Jupyter Notebook

In [11]:  import numpy as np


x = np.random.rand(4)
y = np.random.rand(4)
print("Original array:")
print(x)
print(y)
x[x.argmin()]=0
y[y.argmax()]=1
print("Minimum Value of 0: ")
print(x)
print("Maximum value of 1:")
print(y)

Original array:

[0.6589135 0.77323215 0.53770003 0.77722922]

[0.12807815 0.99444614 0.78467301 0.30807548]

Minimum Value of 0:

[0.6589135 0.77323215 0. 0.77722922]

Maximum value of 1:

[0.12807815 1. 0.78467301 0.30807548]

6.Write a function analyze(array) which prints out the minimum ,maximum , mean,
and median values of the provided array and returns a dictionary containing
these values underappropriate keys.

Function call:

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

Expected Output:

{"minimum":1,"maximum":5,"mean":3,"median":3}

In [12]:  import numpy as np


arr=np.array([1,5,2,4,3])
a=np.amin(arr)
b=np.amax(arr)
mean_arr=np.mean(arr)
median_arr=np.median(arr)
print({("Minimum:",a),("Maximum:",b),("mean:",mean_arr),("median:",median_arr

{('mean:', 3.0), ('Minimum:', 1), ('Maximum:', 5), ('median:', 3.0)}

7. Write a function trigonometric_plot(x, plot_type) , which takes in a NumPy


array x and gives the plot of either sin(x) ,
cos(x) or tan(x) depending on the value assigned tothe plot_type argument. Your
function should use the Matplotlib library.

Input 1:

my_array = np.linspace(-5,5,75)
trigonometric_plot(my_array, sin)

Expected output: Should plot a sin curve
localhost:8888/notebooks/Downloads/Numpy_Studentcopy/Numpy_Studentcopy/Assignment 4.ipynb# 3/6
4/30/22, 2:32 PM Assignment 4 - Jupyter Notebook

Input 2:

my_array = np.linspace(-5,5,75)
trigonometric_plot(my_array, cos)

Expected output: Should plot a cosine curve

In [13]:  import numpy as np


%matplotlib inline
import matplotlib.pyplot as plt
my_array = np.linspace(-5,5,75)
plt.plot(my_array, np.sin(my_array))

Out[13]: [<matplotlib.lines.Line2D at 0x17f8391d0a0>]

In [14]:  import numpy as np


%matplotlib inline
import matplotlib.pyplot as plt
my_array = np.linspace(-5,5,75)
plt.plot(my_array, np.cos(my_array))

Out[14]: [<matplotlib.lines.Line2D at 0x17f840c5cd0>]

localhost:8888/notebooks/Downloads/Numpy_Studentcopy/Numpy_Studentcopy/Assignment 4.ipynb# 4/6


4/30/22, 2:32 PM Assignment 4 - Jupyter Notebook

8. Read the given file weight_height_data.csv. Plot the given data ( height vs
weight) as a scatterplot using the Matplotlib
library.

In [15]:  import csv


import matplotlib.pyplot as plt
FILE_PATH ="weight_height_data.csv"
data=[]
height=[]
weight=[]
with open(FILE_PATH, 'r') as file:
data_file= csv.reader(file)
for line in data_file:
data.append(line)
for i in data[1:]:
height.append(float (i[1]))
weight.append(float (i[2]))
plt.scatter (weight, height,marker=".")
plt.grid()
plt.title('Height Vs Weight', fontsize=20)
plt.xlabel('weight', color="r")
plt.ylabel('Height', color="r")

Out[15]: Text(0, 0.5, 'Height')

9. Compute the BMI for each pair of data-points from Q.8 above. Add the BMI
values as a newcolumn. BMI = (weight in kgs)
/(height in meters)^2 (Hint: Be careful with the units!)

NOte:i was try without using pandas but

localhost:8888/notebooks/Downloads/Numpy_Studentcopy/Numpy_Studentcopy/Assignment 4.ipynb# 5/6


4/30/22, 2:32 PM Assignment 4 - Jupyter Notebook

In [18]:  import pandas as pd


dp=pd.read_csv("weight_height_data.csv")
dp["weight in Kg"]=dp["Weight"]/0.45
dp["height in meters"]=dp["Height"]/39.37
dp["BMI"]=dp["weight in Kg"]/(dp["height in meters"])**2
print(dp)

Gender Height Weight weight in Kg height in meters BM


I

0 Male 73.847017 241.893563 537.541252 1.875718 152.78359


1

1 Male 68.781904 162.310473 360.689939 1.747064 118.17251


3

2 Male 74.110105 212.740856 472.757457 1.882400 133.41797


8

3 Male 71.730978 220.042470 488.983267 1.821970 147.30290


7

4 Male 69.881796 206.349801 458.555112 1.775001 145.54398


3

.. ... ... ... ... ...


...

95 Male 65.117485 165.717112 368.260249 1.653987 134.61406


4

96 Male 71.701234 193.094164 429.098141 1.821215 129.37018


6

97 Male 66.832878 180.683887 401.519749 1.697559 139.33409


8

98 Male 66.471275 172.773723 383.941606 1.688374 134.68771


5

99 Male 67.565430 177.876700 395.281556 1.716165 134.21105


5

[100 rows x 6 columns]

10. Finally, compute the average BMI using the data from Q.9 above.

In [19]:  Sum_BMI=dp["BMI"].sum()
Num_BMI=len(dp["BMI"].index)
Ave_BMI=Sum_BMI/Num_BMI
print("the average BMI is",Ave_BMI)

the average BMI is 134.80962951645836

In [ ]:  ​

localhost:8888/notebooks/Downloads/Numpy_Studentcopy/Numpy_Studentcopy/Assignment 4.ipynb# 6/6

You might also like