<jupyter_start><jupyter_text>DATABASE AND ANALYTICS PROGRAMMING: LAB-4 Submission by: __<NAME>__ (x19194137) ---  Q1-EXCEPTION HANDLING: ---  Q1 (a)Create a text file and manually add some data to the file  Q1 (b)Write Python code to- open the file for write only access- attempt to read the contents of the file  Q1 (c)Note the type of Error that has been raised.  Q1 (d)Modify your code to- use a try / except / finally construct that will catch the exception, print a user-friendly error message, and clean up the file resource  Q1 (e)Investigate how you would create your own Exception class. Then create your own Exception class and use it in your code from the previous exercise. ---  Q2-NUMPY EXERCISE A: ---  Q2 (a)Create an array with the arange function and reshape the array as follows:    b = arange(24).reshape(2,3,4)    This gives us a 3-dimensional data structure – you can think of it as being like 2spreadsheet sheets where each sheet contains 3 rows of data and each rowcontains 4 columns.Using indexing and slicing perform the following tasks:1) Choose the first set of 3 rows and 4 columns of data2) Choose the second row of data from the second set of 3 rows of data3) Choose all the data from the second column for both the first and secondsets of rows and columns of data<jupyter_code>import numpy as np

b = np.array(np.arange(24)).reshape(2, 3, 4)
b
c = b[0, :, :]
c
d = b[1, :, :]
d
e = b[:, :, 1]
e<jupyter_output><empty_output><jupyter_text>Q2 (b)Use the ravel function to flatten the data. What’s the difference between raveland flatten?<jupyter_code>flat_b_ravel = np.ravel(b, 'C')
flat_b_ravel
flat_b_flatten = b.flatten('C')
flat_b_flatten
b<jupyter_output><empty_output><jupyter_text>Q2 (c)Reshape the data so that there are 6 rows of 4 columns per row.<jupyter_code>reshaped_b = np.array(np.arange(24)).reshape(1, 6, 4)
reshaped_b
b.reshape(1, 6, 4)
b<jupyter_output><empty_output><jupyter_text>Q2 (d)Get the transpose of the new data structure.<jupyter_code>transpose_b = np.transpose(b)
transpose_b<jupyter_output><empty_output><jupyter_text>Q2 (e)Restack the rows of the transposed data structure in reverse order >(hint: look atthe row_stack function).  Q2 (f)Split the resulting data structure horizontally >(hint: look at the hsplit function). ---  Q3-NUMPY EXERCISE B: --- > NOTE:> The AAPL.csv contains some stock price data for Apple.> The MSFT.csv contains some stock price data for Microsoft.  Q3 (a)Use the ___loadtxt___ command to load data from __AAPL.csv__ from columns 5 and 7 (i.e.,the close price and the volume).<jupyter_code>dtype_a = np.dtype([('Close', 'f8'), ('Volume', 'f8')])
col5, col7 = np.loadtxt('AAPL.csv', delimiter=",", dtype=dtype_a, skiprows=1, usecols=(4, 6), unpack=True)
np.shape(col5)
np.shape(col7)<jupyter_output><empty_output><jupyter_text>Q3 (b)Based on the data provided, calculate the volume weighted average price for thestock (i.e., calculate the average price using the volume as weight values).<jupyter_code>dtype_b = np.dtype([('High', 'f8'), ('Low', 'f8'), ('Close', 'f8'), ('Volume', 'f8')])
high, low, close, volume = np.loadtxt('AAPL.csv', delimiter=",", dtype=dtype_b, skiprows=1, usecols=(2, 3, 4, 6), unpack=True)
vwap = np.sum(((high+low+close)/3)*volume)/np.sum(volume) #volume weighted average price
vwap<jupyter_output><empty_output><jupyter_text>Q3 (c)Calculate the median value of the closing prices >(hint: use the median function).<jupyter_code>np.median(close)<jupyter_output><empty_output><jupyter_text>Q3 (d)Calculate the variance value of the closing prices.<jupyter_code>np.var(close, ddof=0)
np.var(close, ddof=1)
# Confirm which ddof to use in the variance function, there is a statistical difference between the two.<jupyter_output><empty_output><jupyter_text>Q3 (e)Again, use the __loadtxt__ command to load data from columns 3 and 4 (i.e., the highprices and the low prices).<jupyter_code>dtype_e = np.dtype([('High', 'f8'), ('Low', 'f8')])
col3, col4 = np.loadtxt('AAPL.csv', delimiter=",", dtype=dtype_e, skiprows=1, usecols=(2, 3), unpack=True)
np.shape(col3)
np.shape(col4)<jupyter_output><empty_output><jupyter_text>Q3 (f)Use the __max__ and __min__ functions to get the highest high and the lowest low value.<jupyter_code>max(high)
min(low)
np.max(high)
np.min(low)<jupyter_output><empty_output><jupyter_text>Q3 (g)Load data from column 5 of __AAPL.csv__. Also, load data from column 5 of __MSFT.csv__.<jupyter_code>col5_AAPL = np.loadtxt('AAPL.csv', delimiter=",", dtype='float', skiprows=1, usecols=4)
col5_MSFT = np.loadtxt('MSFT.csv', delimiter=",", dtype='float', skiprows=1, usecols=4)
np.shape(col5_AAPL)
np.shape(col5_MSFT)<jupyter_output><empty_output><jupyter_text>Q3 (h)Calculate the covariance matrix of the closing prices of AAPL and MSFT >(hint: usethe cov function).<jupyter_code>cov_matrix = np.cov(col5_AAPL, col5_MSFT)
cov_matrix<jupyter_output><empty_output><jupyter_text>Q3 (i)View the values on the diagonal > (hint: diagonal).<jupyter_code>np.diagonal(cov_matrix)<jupyter_output><empty_output><jupyter_text>Q3 (j)Calculate the correlation coefficient of the closing prices of AAPL and MSFT >(hint:corrcoef).<jupyter_code>R_matrix = np.corrcoef(cov_matrix)
R_matrix<jupyter_output><empty_output><jupyter_text>---  Q4-REGULAR EXPRESSIONS:Write a Python program that will identify URLs using regular expressions. ---<jupyter_code>import re

full_text = "Kabhi kabhi apun ko lagta hai ki https://stackoverflow.com/ hi bhagwaan hai"

urls_in_full_text = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', full_text)

print("Original string: ", full_text)
print("Urls: ", urls_in_full_text)<jupyter_output>Original string:  Kabhi kabhi apun ko lagta hai ki https://stackoverflow.com/ hi bhagwaan hai
Urls:  ['https://stackoverflow.com/']
{'fraction_non_alphanumeric': 0.12601136168015148, 'fraction_numerical': 0.020485453606472716, 'mean_word_length': 5.924910607866508, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 70, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '296752', 'license': 'MIT', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2094, 'n_words': 634, 'path': '.ipynb_checkpoints/Lab4-checkpoint.ipynb', 'repo_name': 'singhad/DAP_Lab', 'size': '5817'}