logistic_regression
.py
keyboard_arrow_up
School
University of Michigan *
*We aren’t endorsed by this school
Course
545
Subject
Computer Science
Date
Apr 3, 2024
Type
py
Pages
3
Uploaded by CaptainLobsterPerson219
"""EECS545 HW2: Logistic Regression."""
import numpy as np
import math
def hello():
print('Hello from logistic_regression.py')
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def naive_logistic_regression(X: np.ndarray, Y: np.ndarray, max_iters = 100) -> np.ndarray:
"""Computes the coefficients w from the datset (X, Y).
This implementation uses a naive set of nested loops over the data.
Specifically, we are required to use Newton's method (w = w - inv(H)*grad).
Inputs:
- X: Numpy array of shape (num_data, num_features+1).
The first column of each row is always 1.
- Y: Numpy array of shape (num_data) that has 0/1.
- max_iters: Maximum number of iterations
Returns:
- w: Numpy array of shape (num_features+1) w[i] is the coefficient for the i-
th
column of X. The dimension should be matched with the second dimension of X.
"""
N, d = X.shape
w = np.zeros(d, dtype=X.dtype)
for iter in range(max_iters):
grad = np.zeros(d)
H = np.zeros((d, d))
for data_x, data_y in zip(X, Y):
###################################################################
# TODO: Implement this function using Newton's method. #
# Specifically, you are required to update the following two #
# variables described below. #
# * grad: accumulated gradient over data samples. #
# * H: accumulated Hessian matrix over data samples. #
# Please do not check a convergence condition for this time. #
# Also, please do not introduce any additional python inner loop. #
# note: You are allowed to use predefined sigmoid function above. #
###################################################################
H += -sigmoid(np.dot(w, data_x))*(1 - sigmoid(np.dot(w, data_x))) * (np.outer(data_x, data_x))
grad += (data_y - sigmoid(np.dot(w, data_x))) * data_x
###################################################################
# END OF YOUR CODE #
###################################################################
w = w - np.matmul(np.linalg.inv(H), grad)
return w
def vectorized_logistic_regression(X: np.ndarray, Y: np.ndarray, max_iters = 100) -
> np.ndarray:
"""Computes the coefficients w from the dataset (X, Y).
This implementation will vectorize the implementation in naive_logistic_regression,
which implements Newton's method (w = w - inv(H)*grad).
Inputs:
- X: Numpy array of shape (num_data, num_features+1).
The first column of each row is always 1.
- Y: Numpy array of shape (num_data) that has 0/1.
- max_iters: Maximum number of iterations
Returns:
- w: Numpy array of shape (num_features+1) w[i] is the coefficient for the i-
th
column of X. The dimension should be matched with the second dimension of X.
"""
N, d = X.shape
w = np.zeros(d, dtype=X.dtype)
for iter in range(max_iters):
#######################################################################
# TODO: Implement this function using Newton's method. #
# Because this version does not have any inner loop, compared to the #
# previous (naive_logistic_regression) task, you are required to #
# compute grad and H at once. #
# * grad: gradient from all data samples. #
# * H: Hessian matrix from all data samples. #
# The shape of grad and H is the same as 'naive_logistic_regression'. #
# Please do not check a convergence condition for this time. #
# Also, please do not introduce any additional python inner loop. #
# You are allowed to use predefined sigmoid function above. #
#######################################################################
d = X @ w
D = np.diag(-sigmoid(d) * (1-sigmoid(d)))
grad = X.T @ (Y - sigmoid(X @ w)) # hint: grad.shape should be (d, ) at the end of this block
# print("here", X.shape, D.shape, grad.shape)
H = X.T @ D @ X # hint: H.shape should be (d, d) at the end of this block
# print("here2", H.shape)
#######################################################################
# END OF YOUR CODE #
#######################################################################
w = w - np.matmul(np.linalg.inv(H), grad)
return w
def compute_y_boundary(X_coord: np.ndarray, w: np.ndarray) -> np.ndarray:
"""Computes the matched y coordinate value for the decision boundary from
the x coordinate and coefficients w.
Inputs:
- X_coord: Numpy array of shape (d, ). List of x coordinate values.
- w: Numpy array of shape (3, ) that stores the coefficients.
Returns:
- Y_coord: Numpy array of shape (d, ).
List of y coordinate values with respect to the coefficients w.
"""
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Questions
ANSWER MUST BE IN PYTHON3. METHOD HEADER BELOW.
#!/bin/python3
import math
import os
import random
import re
import sys
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
def read_dataframe():
# Return a pandas dataframe from stdin
return pd.read_csv(sys.stdin)
def read_matrix():
# Return column names and data as a matrix
df = read_dataframe()
return list(df.columns), df.values
def read_list():
# Return column names and data as a list of lists
# Each element in the list corresponds to columns in the data
col_names, val = read_matrix()
return col_names, val.T.tolist()
#
# Complete the 'main' function below.
#
# The function is expected to return a STRING.
#
def main():
# Write your code here
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
result = main()
fptr.write(result + '\n')
fptr.close()
arrow_forward
Q3: You have M matrix: write the output
of the following MATLAB commands
M=
M= 5 7 9 14
0 5 38 1
7 4 19 5
6 82 1 0
arrow_forward
7-2 Discussion: Interpreting Multiple Regression Models
Previous Next
In this discussion, you will apply the statistical concepts and techniques covered in this week's reading about multiple regression. You will not be completing work in Jupyter Notebook this week. Instead, you will be interpreting output from your Python scripts for the Module Six discussion. If you did not complete the Module Six discussion, please complete that before working on this assignment.
Last week's discussion involved development of a multiple regression model that used miles per gallon as a response variable. Weight and horsepower were predictor variables. You performed an overall F-test to evaluate the significance of your model. This week, you will evaluate the significance of individual predictors. You will use output of Python script from Module Six to perform individual t-tests for each predictor variable. Specifically, you will look at Step 5 of the Python script to answer…
arrow_forward
Solve using integer programming
arrow_forward
Python(multiple linear regression using Ordinary Least Square method)
Question:
Implement multiple linear regression using Ordinary Least Square method from scratch in Python.
****You are required to at least write your own the fit(), predict(), r2_score() method.
Use the following dataset: titanic_train.csv, titanic_test.csv (both can be found online ,files are large so can't attach)
Target: Predict whether or not the passenger survived the sinking of the Titanic.
Remove unnecessary features if any.
Note:
do not copy paste any answer available online or in archive I want a personalised answer from an expert
do display the output of an running program
add comments for better understanding of solution
arrow_forward
C++
arrow_forward
NOTE: WRITE IN C#
Write a distributed probability random number generator using one of the probability sampling methods (like rejection sampling). Explain the codes and the sampling implementation in detail.
Example:
İnputs :
(3, 0.4)
(5, 0.5)
Output:
Generated random number is 3, it has %40 probability
Generated random number is 5, it has %50 probability
NOTE: WRITE IN C#
NOTE: MAKE A GENERIC SOLUTION THAT APPLIES FOR ANY GIVEN NUMBER AND PROBABILITY. NOT JUST BASED ON THIS EXAMPLE !!!
arrow_forward
Please convert Mutual Recursion to Java. See attached image. Thank you.
arrow_forward
#In the lines below, I wrote a quick program used to simulate a stock price over two years. As an#investor, I want to know what the longest run was of consecutive gains was in the dataset (e.g.#10.2,13,42,89,85 has a run of three where the price rose before a drop.) Print the length of the longest#run and the members of that run.import numpy as npvec = np.random.uniform(80, 120, 720)print(vec)
arrow_forward
in c++.using recursion. without find( begin and end) function.
arrow_forward
The goal is to rewrite the function, below, such that passes in a different list of parameters, particularly eliminating the need to pass low and high for each recursive call to binary_search.
defbinary_search(nums,low,high,item):
mid=(low+high)//2iflow>high:returnFalse #The item doesn't exist in the list!elifnums[mid]==item:returnTrue# The item exists in the list!elifitem<nums[mid]:returnbinary_search(nums,low,mid-1,item)else:returnbinary_search(nums,mid+1,high,item)
The new function should be prototyped below. The number of changes between the given version, and the one requested is not significant.
defbinary_search(nums,item):pass# Remove this and fill in with your code
Tip: If you consider that high and low are used to create a smaller version of our problem to be processed recursively, the version requested will do the same thing, just through a different, more Pythonic technique.
arrow_forward
Nationality. Prove the function works by testing it
2. BLACKJACK: Sample two cards from a "deck of cards" (ace, 2-10, jack, queen, king). Find the total
of the two cards (ace counts as 11 and facecards count as 10). If the total is 21 print "BLACKJACK!"
otherwise print "Try again". Keep drawing pairs of two cards until you get blackjack. Set your seed
to 30 at the start of this problem
? Write 2
that ohe
to frame for NAe If NA is found it should replace NA with the
arrow_forward
Solve these questions
arrow_forward
import numpy as npfrom scipy.optimize import minimizeimport matplotlib.pyplot as pltimport pickle
#Implement the function calAnaliticalSolution
def calAnaliticalSolution(X,y): # Inputs: # X = N x d # y = N x 1 # Output: # w = d x 1 (which is the parameter calculated from data X and label y using alalytical solution) # IMPLEMENT THIS METHOD - REMOVE THE NEXT LINE w = np.zeros((X.shape[0],1)) return w
#Implement the function calRegressionError
def calRegressionError(w,Xtest,ytest): # Inputs: # w = d x 1 # Xtest = N x d # ytest = N x 1 # Output: # mse = scalar value (which is the error of the regression model) # IMPLEMENT THIS METHOD - REMOVE THE NEXT LINE mse = 0 return mse
Xtrain,ytrain,Xtest,ytest = pickle.load(open('diabetes.pickle','rb')) x1 = np.ones((len(Xtrain),1))x2 = np.ones((len(Xtest),1))Xtrain_i =…
arrow_forward
Write in c language please,
The Fibonacci sequence begins with 0 and then 1 follows. All subsequent values are the sum of the previous two, for example: 0, 1, 1, 2, 3, 5, 8, 13. Complete the Fibonacci() function, which has an index n as a parameter and returns the nth value in the sequence. Any negative index values should return -1.
Ex: If the input is:
7
the output is:
Fibonacci(7) is 13
Note: Use a for loop and DO NOT use recursion.
arrow_forward
def findOccurrences(s, ch):
lst = []
for i in range(0, len(s)):
if a==s[i]:
lst.append(i)
return lst
Use the code above instead of enumerate in the code posted below.
n=int(input("Number of rounds of Hangman to be played:"))
for i in range(0,n):
word = input("welcome!")
guesses = ''
turns = int(input("Enter the number of failed attempts allowed:"))
def hangman(word):
secrete_word = "-" * len(word)
print(" the secrete word " + secrete_word)
user_input = input("Guess a letter: ")
if user_input in word:
occurences = findOccurrences(word, user_input)
for index in occurences:
secrete_word = secrete_word[:index] + user_input + secrete_word[index + 1:]
print(secrete_word)
else:
user_input = input("Sorry that letter was not found, please try again: ")
def findOccurrences(s, ch):
return [i for i, letter in enumerate(s) if letter == ch] *** enumerate not discussed in…
arrow_forward
Python only** Use recursive function*
Define countBetween with 2 parameters
Use def to define countBetween with 2 parameters
thas two parameters: a starting number and an ending number, and counts up (i.e., prints consecutive integers) from the starting number to the ending number. If the numbers are the same, it just prints that number; if the ending number is lower than the starting number, it prints nothing.
Do not use any kind of loop
Within the definition of countBetween with 2 parameters, do not use any kind of loop.
Call countBetween
Within the definition of countBetween with 2 parameters, call countBetween in at least one place.
Python only** Use recursive function*
Define countThereAndBack with 2 parameters
Use def to define countThereAndBack with 2 parameters
after reaching the ending number, it also counts back down to the starting number. It accepts the same two parameters defining starting and ending numbers it should print nothing if the starting number is…
arrow_forward
Can you help please the previous question is there
arrow_forward
please include full solutions and provide explanation. will give thumbs up for complete answer
arrow_forward
.Code with comments and output screenshot. Also explain in detail. Thank you!
arrow_forward
dont write code. just write algorithm or related theory of the following question. Narrative 2: We need to have a generic module which reads in file and gives the output in the format which would facilitate other programs to do comparison like the one we saw above to carry out analysis and comparison. Response Required: Write a program that reads a given text, outputs the text as is, and also prints the number of lines and the number of times each letter appears in the text. An uppercase letter and a lowercase letter are treated as being the same; that is, they are tallied together. Since there are 26 letters, we use an array of 26 components to perform the letter count. We also need a variable to store the line count. The text is stored in a file, which we will call textin.txt. The output will be stored in a file, which we will call textout.txt Input: A file containing the text to be processed. Output: A file containing the text, number of lines, and the number of times a letter…
arrow_forward
pizzaStats Assignment Description
For this assignment, name your R file pizzaStats.R
For all questions you should load tidyverse and lm.beta. You should not need to use any other libraries.
Load tidyverse with
suppressPackageStartupMessages(library(tidyverse))
Load lm.beta withsuppressPackageStartupMessages(library(lm.beta))
Run a regression predicting whether or not wine was ordered from temperature, bill, and pizza.
Assign the coefficients of the summary of the model to Q1. The output should look something like this:
Estimate Std. Error z value Pr(>|z|)(Intercept) -1.09 1.03 -1.06 0.29temperature -0.04 0.01 -3.20 0.00bill 0.03 0.01 3.75 0.00pizzas 0.19 0.06 3.27 0.00
Here is the link of the pizza.csv https://drive.google.com/file/d/1_m2TjPCkPpMo7Z2Vkp32NiuZrXBxeRn6/view?usp=sharing
arrow_forward
Write a program in C that will find the largest number from a 4X4 matrix and divide each element of the odd columns of that matrix by that largest number and store in a new matrix then print the matrix. Take inputs from user. Use the following prototype:void normalizeMatrix(int rowSize, int colSize, float mat[rowSize][colSize])void printMatrix(int rowSize, int colSize, float mat[rowSize][colSize])
arrow_forward
def fun(nums: List[int], key): count = 0 for v in nums: if v == key count = count + 1 return count
a. What does this function do?
b. Write an equivalent function using recursion.
c. What's advantages and disadvantages of both implementations in terms of time and space complexity.
arrow_forward
please help with the python problem, thank you
arrow_forward
# Help me finish the getMin getMax get StandardDeviation getSortedResampleMeans getResampleMeanPercentiles by filling in the blank of these part
# please edit the return 0.0 and return null part.
import java.util.Arrays;
public class Statistics {
public static double getMax(double[] data) { // TODO - implement
return 0.0;
}
public static double getSum(double[] data) {
return 0.0;
}
public static double getMean(double[] data) {
return 0.0;
}
public static double getStdDev(double[] data) {
return 0.0;
}
public static double[] getResample(double[] data) {
return null;
}
public static double[] getSortedResampleMeans(double[] data, int numResamples) {
return null;
}
public static double getPercentile(double[] data, double percentile) {
double exactIndex = percentile * data.length / 100.0;
int index = ((Math.abs(exactIndex - Math.round(exactIndex)) <= 1e-14) ? (int) Math.round(exactIndex) : (int) Math.floor(percentile * (data.length + 1) / 100.0) - 1);
index = (index < 0) ? 0 :…
arrow_forward
write code in Phython
arrow_forward
Numpy
arrow_forward
Consider the following function:
public void bSearch(int[] A, int value, int start, int end) {
if (end value) {
return bSearch(A, value, start, mid);
} else if (value > A[mid]) {
return bSearch(A, value, mid+1, end);
} else {
return mid;
}
}
The mutation program P' changes the part as shown below.
} else if (value > A[mid]) {
return bSearch(A, value, mid+2, end);
} else {
return mid;
}
A test input is given t1: {A = [2, 3, 5, 7, 10, 11], value = 6, start=0, end=5}.
Which statement below best describes the mutant with regards to the test input?
O a. t1 resolves mutant P
O b. The mutant P' with regards to test input t1 is live.
arrow_forward
This is a task that needs to be done in matlab
arrow_forward
*Data Structures
arrow_forward
Graph data structure. Answer in C++ asap.
arrow_forward
use pandas python & numpy
I want number 3)
arrow_forward
Using truth table and functions please complete task 3. Schematic truth table should match). Must have 3 ins and 7 outs.
Will upvote!(:
arrow_forward
Implement the following function which compares two strings without using the library function. Prototype: int compare(char *str1, char *str2). The compare function returns 0 if str1 is equal to str2, returns 1 if str1 is greater than str2, and returns -1 if str2 is greater than str1. Then print the information in the main function.
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Related Questions
- ANSWER MUST BE IN PYTHON3. METHOD HEADER BELOW. #!/bin/python3 import math import os import random import re import sys import numpy as np import pandas as pd from sklearn.linear_model import LinearRegression def read_dataframe(): # Return a pandas dataframe from stdin return pd.read_csv(sys.stdin) def read_matrix(): # Return column names and data as a matrix df = read_dataframe() return list(df.columns), df.values def read_list(): # Return column names and data as a list of lists # Each element in the list corresponds to columns in the data col_names, val = read_matrix() return col_names, val.T.tolist() # # Complete the 'main' function below. # # The function is expected to return a STRING. # def main(): # Write your code here if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') result = main() fptr.write(result + '\n') fptr.close()arrow_forwardQ3: You have M matrix: write the output of the following MATLAB commands M= M= 5 7 9 14 0 5 38 1 7 4 19 5 6 82 1 0arrow_forward7-2 Discussion: Interpreting Multiple Regression Models Previous Next In this discussion, you will apply the statistical concepts and techniques covered in this week's reading about multiple regression. You will not be completing work in Jupyter Notebook this week. Instead, you will be interpreting output from your Python scripts for the Module Six discussion. If you did not complete the Module Six discussion, please complete that before working on this assignment. Last week's discussion involved development of a multiple regression model that used miles per gallon as a response variable. Weight and horsepower were predictor variables. You performed an overall F-test to evaluate the significance of your model. This week, you will evaluate the significance of individual predictors. You will use output of Python script from Module Six to perform individual t-tests for each predictor variable. Specifically, you will look at Step 5 of the Python script to answer…arrow_forward
- Solve using integer programmingarrow_forwardPython(multiple linear regression using Ordinary Least Square method) Question: Implement multiple linear regression using Ordinary Least Square method from scratch in Python. ****You are required to at least write your own the fit(), predict(), r2_score() method. Use the following dataset: titanic_train.csv, titanic_test.csv (both can be found online ,files are large so can't attach) Target: Predict whether or not the passenger survived the sinking of the Titanic. Remove unnecessary features if any. Note: do not copy paste any answer available online or in archive I want a personalised answer from an expert do display the output of an running program add comments for better understanding of solutionarrow_forwardC++arrow_forward
- NOTE: WRITE IN C# Write a distributed probability random number generator using one of the probability sampling methods (like rejection sampling). Explain the codes and the sampling implementation in detail. Example: İnputs : (3, 0.4) (5, 0.5) Output: Generated random number is 3, it has %40 probability Generated random number is 5, it has %50 probability NOTE: WRITE IN C# NOTE: MAKE A GENERIC SOLUTION THAT APPLIES FOR ANY GIVEN NUMBER AND PROBABILITY. NOT JUST BASED ON THIS EXAMPLE !!!arrow_forwardPlease convert Mutual Recursion to Java. See attached image. Thank you.arrow_forward#In the lines below, I wrote a quick program used to simulate a stock price over two years. As an#investor, I want to know what the longest run was of consecutive gains was in the dataset (e.g.#10.2,13,42,89,85 has a run of three where the price rose before a drop.) Print the length of the longest#run and the members of that run.import numpy as npvec = np.random.uniform(80, 120, 720)print(vec)arrow_forward
- in c++.using recursion. without find( begin and end) function.arrow_forwardThe goal is to rewrite the function, below, such that passes in a different list of parameters, particularly eliminating the need to pass low and high for each recursive call to binary_search. defbinary_search(nums,low,high,item): mid=(low+high)//2iflow>high:returnFalse #The item doesn't exist in the list!elifnums[mid]==item:returnTrue# The item exists in the list!elifitem<nums[mid]:returnbinary_search(nums,low,mid-1,item)else:returnbinary_search(nums,mid+1,high,item) The new function should be prototyped below. The number of changes between the given version, and the one requested is not significant. defbinary_search(nums,item):pass# Remove this and fill in with your code Tip: If you consider that high and low are used to create a smaller version of our problem to be processed recursively, the version requested will do the same thing, just through a different, more Pythonic technique.arrow_forwardNationality. Prove the function works by testing it 2. BLACKJACK: Sample two cards from a "deck of cards" (ace, 2-10, jack, queen, king). Find the total of the two cards (ace counts as 11 and facecards count as 10). If the total is 21 print "BLACKJACK!" otherwise print "Try again". Keep drawing pairs of two cards until you get blackjack. Set your seed to 30 at the start of this problem ? Write 2 that ohe to frame for NAe If NA is found it should replace NA with thearrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education