<jupyter_start><jupyter_text>**Artificial Intelligence - MSc**CS6501 - MACHINE LEARNING AND APPLICATIONS**Business Analytics - MSc**ET5003 - MACHINE LEARNING APPLICATIONS ***Annual Repeat***Instructor: <NAME>RepMLA_Lab-1.3Student ID: Student name:   IMPORT STATEMENTS<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import math as m<jupyter_output><empty_output><jupyter_text>Problem 11Create a function that returns the sum of all even elements in a 2D matrix.Examples>sum_of_evens([  [1, 0, 2],  [5, 5, 7],  [9, 4, 3]]) ➞ 6>>sum_of_evens([  [1, 1],  [1, 1]]) ➞ 0>>sum_of_evens([  [42, 9],  [16, 8]]) ➞ 66>>sum_of_evens([  [],  [],  []]) ➞ 0Notes    Submatrices will be of equal length.    Return 0 if the 2D matrix only consists of empty submatrices.<jupyter_code>def sum_of_evens(arr):
    #return arr of even values only
    arr=np.where(np.mod(arr, 2), 0, arr)
    #sum the values in the arr
    val=np.sum(arr)
    #return the summation of the array
    return val
arr_dic={'a':[ [1, 0, 2], [5, 5, 7], [9, 4, 3] ],
         'b':[ [1, 1], [1, 1] ],
         'c':[ [42, 9], [16, 8] ],
         'd':[ [], [], [] ]}
##create empty dictionary to hold values with associated keys
sum_dic={}
for k, v in arr_dic.items():
    val=sum_of_evens(v)
    sum_dic[k]=val
print(sum_dic)<jupyter_output>{'a': 6, 'b': 0, 'c': 66, 'd': 0.0}<jupyter_text>Problem 12In each input list, every number repeats at least once, except for two. Write a function that returns the two unique numbers.Examples>return_unique([1, 9, 8, 8, 7, 6, 1, 6]) ➞ [9, 7]>>return_unique([5, 5, 2, 4, 4, 4, 9, 9, 9, 1]) ➞ [2, 1]>>return_unique([9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]) ➞ [5, 6]NotesKeep the same ordering in the output.<jupyter_code>def find_unique(arr):
    ##Identify unique values
    unique_value, value_ind, value_counts=np.unique(arr, return_index=True, return_counts=True)
#     print(unique_value, value_ind, value_counts)
    ##get ind position of original array where value counts=1
    ind=value_ind[np.where(value_counts==1)]
#     print(ind)
    
    ##return values at the index use 'sorted' to keep order
    return ([arr[item] for item in sorted(ind)])    
arr_dic={'a': [1, 9, 8, 8, 7, 6, 1, 6],
        'b': [5, 5, 2, 4, 4, 4, 9, 9, 9, 1],
        'c': [9, 5, 6, 8, 7, 7, 1, 1, 1, 1, 1, 9, 8]}

for k, v in arr_dic.items():
    unique=find_unique(v)
    print(unique)<jupyter_output>[9, 7]
[2, 1]
[5, 6]<jupyter_text>Problem 13This is a reverse coding challenge. Normally you're given explicit directions with how to create a function. Here, you must generate your own function to satisfy the relationship between the inputs and outputs.Your task is to create a function that, when fed the inputs below, produces the sample outputs shown.>mystery_func(152) ➞ 10>>mystery_func(832) ➞ 48>>mystery_func(19) ➞ 9>>mystery_func(133) ➞ 9<jupyter_code>input_arr=[152, 832, 19, 133]

##AS PER INFO FROM @CIARAN COX THE OUTPUT CAN BE GENERATED BY MULTIPLYING ALL INPUT VALUES
multi_dic={}
for item in input_arr:
    my_s=str(item)
    s_list=[]
    for char in my_s:
        s_list.append(int(char))
    multi=np.prod(s_list)
    multi_dic[item]=multi
multi_dic<jupyter_output><empty_output><jupyter_text>Problem 14Build a function that creates histograms. Every bar needs to be on a new line and its length corresponds to the numbers in the list passed as an argument. The second argument of the function represents the character that needs to be used.> histogram(lst, char) -> strExamples>histogram([1, 3, 4], "\") ➞ "\\n\\\\n\\\\">>\>>\>>\>>histogram([6, 2, 15, 3], "=") ➞ "======\n==\n===============\n===">>======>>==>>===============>>===>>histogram([1, 10], "+") ➞ "+\n++++++++++">>+>>++++++++++<jupyter_code>def histogram(my_list, my_char):
    
    bar_dic={}
    for i in range(0, len(my_list)):
        bar_list=[]
        for j in range(0, my_list[i]):
            bar_list.append(my_char)
        bar_dic[i]=bar_list
    return bar_dic
hist_dic={'#':[1,3,4],
         '=':[6,2,15,3],
         '+':[1,10]}

for k, v in hist_dic.items():
    ##create dictionary of bars (each key is bar), value(list of items)
    dic=histogram(v, k)
    print('NEW HISTOGRAM')
    for k2,v2 in dic.items():
        #print(joined list and new line comands)
        print(''.join(v2), '\n')<jupyter_output><empty_output><jupyter_text>Problem 15This challenge will deal further with writing recursive functions by covering the Euclidean Algorithm. The "Euclidean Algorithm" is a method for finding the greatest common divisor (GCD) of two numbers. It was originally described by the Greek mathematician Euclid.AlgorithmFor the sake of simplicity I'll refer to the first number as "a", the second number as "b", and the remainder as "r". The algorithm can be broken down into four steps:    Ensure that "a" >= "b". If "a" < "b", swap them.    Find the remainder. Divide "a" by "b" and set "r" as the remainder.    Is "r" zero? If so terminate the function and return "b" (the second number).    Set "a" = "b" and "b" = "r" and start the algorithm over again.InstructionsCreate a recursive function that returns the GCD between two positive numbers using the Euclidean Algorithm.Examples>euclidean(8, 6) ➞ 2>>euclidean(25, 5) ➞ 5>>euclidean(49, 14) ➞ 7Notes    Remember that to find the remainder of two numbers use the modulus % operator.    Both numbers will be positive and neither number will be null.<jupyter_code>##There is already a function in math module that provide gcd
values=[(8, 6), (25, 5), (49,14)]

for tup in values:
    print(m.gcd(tup[0],tup[1]))
def get_gcd(a, b):
    
    if a < b:
        i=b
        j=a
    else:
        i=a
        j=b
        
    #get the remainder
    z=i%j
    ##if there is no remainder then greatest common denom is j
    if z==0:
        z=j
    return z
for tup in values:
    print(get_gcd(tup[0], tup[1]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14997458057956278, 'fraction_numerical': 0.03914590747330961, 'mean_word_length': 3.841673502871206, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 74, 'https://': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '46065', 'license': 'Apache-2.0', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2097, 'n_words': 740, 'path': 'WEEK_1/RepMLA_Lab_1_3.ipynb', 'repo_name': 'alisonoc/ML_APP_UL', 'size': '5909'}