Question: Logistic Regression 1. Create a python file named mylogistic-0.py. Import the following package. import numpy as np from sklearn import linear_model import matplotlib.pyplot as plt

Logistic Regression

1. Create a python file named mylogistic-0.py. Import the following package.

import numpy as np

from sklearn import linear_model

import matplotlib.pyplot as plt

2. Add the following lines that are used for creating the data set. What is the printout?

X = np.array([[3.1, 7.2], [4, 6.7], [2.9, 8], [5.1, 4.5], [6, 5], [5.6, 5], [3.3, 0.4], [3.9, 0.9], [2.8, 1], [0.5, 3.4], [1, 4], [0.6, 4.9]])

y = np.array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

print(X.shape,y.shape)

3. Add the following lines and run the program. Is it reasonable to design such an experiment? What is your suggestion (writing down your opinion here is fine and there is no need for implementation)?

classifier = linear_model.LogisticRegression(solver='liblinear', C=1)

classifier.fit(X, y)

y_pred = classifier.predict(X)

accuracy = 100.0 * (y == y_pred).sum() / X.shape[0]

print("Accuracy of the logistic classifier =", round(accuracy, 2), "%")

4. Develop a python program, named changelabel.py. Requirements: (1) Preprocess the dataset of multivar_data_nb.txt: convert the original labels 0 and 1 into the new label 0; convert the original labels 2 and 3 into the label 1. (2) The conversion is saved to new_multivar_data_nb.txt. Hint: May use np.expand_dims, np.append, and np.savetext.

5. Create a new python program named mylogistic-1.py. It has the following requirements:

1) Use the new generated data set in the previous question. Draw the data points on a scatter plot. Class 0 marked as o while class 1 marked as x. Hint: use matplotlib that you have learned in previous labs.

2) Use model_selection.train_test_split to split the training (80%) and testing data (20%).

3) Use linear_model.LogisticRegression(solver='liblinear', C=1) to train the training data set.

4) Predict the accuracy using the testing data set.

5) Paste the programs below, changelabel.py and mylogistic-1.py.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!