Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please solve it in Python. Need answers ASAP. Also please show testing results and output. Thank you! 1. Define a function to analyze a numpy
Please solve it in Python. Need answers ASAP. Also please show testing results and output.
Thank you!
1. Define a function to analyze a numpy array Assume we have an array X which contains term frequency of each document. In this array, each row presents a document, each column denotes a word, and each value, say xij, denotes the frequency of the word j in document i. Therefore, if there are m documents, n words, X has a shape of (m, n). Define a function named analyze_tf which: - take X as an input. calculate the document frequency df; for word j, e.g. how many documents contain word j. Save the result to array df (df has shape of (n.)). divides word frequency Xij by the total number of words in document i. Save the result as an array named tf (tf has shape of (m,n)). for each Xij, calculates tf_id fij The reason is, if a word appears in most documents, it does not have the discriminative power and often is df; called a stop word. The inverse of df can downgrade the weight of such words. tf_id f has shape of (m, n) Now, please print the following: o print the index of the longest document o print the indexes of words with the top 3 largest d f values o for the longest document, print the indexes of words with top 3 largest values in the tf_idf array. return the tf_idf array. Note, for all the steps, do not use any loop. Just use array functions and broadcasting for high performance computation. In [1]: Nimport numpy as np import pandas as pd In [2] : def analyze_tf(x): tf idf = None # add your code here #print index of the Longest documents #print indexes of words with the top 3 Largest df values #return index of top_3 wordsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started