Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question
Write a Python function mat_to_prob (R) that returns the matrix P = (P₁, P2, ..., Pm) where Pi is the i-th row of matrix P
, and Pi is the probability vector obtained from R; using the formulation in Question 1. In other words, convert each row of the
input matrix into a probability vector.
Sample inputs and outputs:
• Input: np.array([[4, 6], [3.5, 9.11)
Output:
[[0.11920292 0.88079708]
[0.00368424 0.99631576]]
• Input: np.array([[2, 3.1, 5], [10, 3.7, 12], [4, 5.5, 0]]))
Output:
[[4.15115123e-02 1.24707475e-01 8.33781013e-01]
[1.19176835e-01 2.18844992e-04 8.80604320e-01]
[1.81818026e-01 8.14851861e-01 3.33011331e-03]]
Hint: use numpy.sum with an appropriate axis and keepdims settings. You should also check broadcasting in numpy.
Question 2b
In fact, the function above is called the softmax function, and scipy has an implementation for it. First, read the API at:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.softmax.html.
Then write code to apply this version of the softmax function on the sample matrices above and print out the results.
# The function has been imported for you
from scipy.special import softmax
# Write your code here, remember to print out the result
expand button
Transcribed Image Text:Write a Python function mat_to_prob (R) that returns the matrix P = (P₁, P2, ..., Pm) where Pi is the i-th row of matrix P , and Pi is the probability vector obtained from R; using the formulation in Question 1. In other words, convert each row of the input matrix into a probability vector. Sample inputs and outputs: • Input: np.array([[4, 6], [3.5, 9.11) Output: [[0.11920292 0.88079708] [0.00368424 0.99631576]] • Input: np.array([[2, 3.1, 5], [10, 3.7, 12], [4, 5.5, 0]])) Output: [[4.15115123e-02 1.24707475e-01 8.33781013e-01] [1.19176835e-01 2.18844992e-04 8.80604320e-01] [1.81818026e-01 8.14851861e-01 3.33011331e-03]] Hint: use numpy.sum with an appropriate axis and keepdims settings. You should also check broadcasting in numpy. Question 2b In fact, the function above is called the softmax function, and scipy has an implementation for it. First, read the API at: https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.softmax.html. Then write code to apply this version of the softmax function on the sample matrices above and print out the results. # The function has been imported for you from scipy.special import softmax # Write your code here, remember to print out the result
Expert Solution
Check Mark
Step 1: Algorithm :

Algorithm: mat_to_prob(R)

Input: 
  - R: A 2D numpy array representing the input matrix.

Output:
  - P: A 2D numpy array where each row is a probability vector.

Step 1: Import the required libraries.
   - Import numpy as np
   - From scipy.special, import softmax

Step 2: Define the function mat_to_prob(R) as follows:

   2.1: Calculate P using softmax function:
        - P = softmax(R, axis=1)

   2.2: Return P as the result.

Step 3: End of the function.

Sample Inputs and Outputs:
   - Call mat_to_prob with sample input matrices.
   - Print the resulting probability matrices.

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education