程序代写代做代考 Excel algorithm C Hive Homework 3 INF 552, Instructor: Mohammad Reza Rajati

Homework 3 INF 552, Instructor: Mohammad Reza Rajati
1. Time Series Classification
An interesting task in machine learning is classification of time series. In this problem, we will classify the activities of humans based on time series obtained by a Wireless Sensor Network.
(a) Download the AReM data from: https://archive.ics.uci.edu/ml/datasets/ Activity+Recognition+system+based+on+Multisensor+data+fusion+\%28AReM\ %29 . The dataset contains 7 folders that represent seven types of activities. In each folder, there are multiple files each of which represents an instant of a human performing an activity.1 Each file containis 6 time series collected from activities
of the same person, which are called avg rss12, var rss12, avg rss13, var rss13, vg rss23, and ar rss23. There are 88 instances in the dataset, each of which con- tains 6 time series and each time series has 480 consecutive values.
(b) Keep datasets 1 and 2 in folders bending1 and bending 2, as well as datasets 1, 2, and 3 in other folders as test data and other datasets as train data.
(c) Feature Extraction
Classification of time series usually needs extracting features from them. In this problem, we focus on time-domain features.
i. Research what types of time-domain features are usually used in time series classification and list them (examples are minimum, maximum, mean, etc).
ii. Extract the time-domain features minimum, maximum, mean, median, stan- dard deviation, first quartile, and third quartile for all of the 6 time series in each instance. You are free to normalize/standardize features or use them directly.2
Your new dataset will look like this:
where, for example, 1st quart6, means the first quartile of the sixth time series in each of the 88 instances.
iii. Estimate the standard deviation of each of the time-domain features you extracted from the data. Then, use Python’s bootstrapped or any other method to build a 90% bootsrap confidence interval for the standard deviation of each feature.
iv. Use your judgement to select the three most important time-domain features (one option may be min, mean, and max).
1Some of the data files need very minor cleaning. You can do it by Excel or Python. 2You are welcome to experiment to see if they make a difference.
Instance
min1
max1
mean1
median1 · · · 1st quart6
3rd quart6
1
2
3
.
.
.
.
. … .
.
88
1

Homework 3 INF 552, Instructor: Mohammad Reza Rajati
(d) Binary Classification Using Logistic Regression3
i. Assume that you want to use the training set to classify bending from other activities, i.e. you have a binary classification problem. Depict scatter plots of the features you specified in 1(c)iv extracted from time series 1, 2, and 6 of each instance, and use color to distinguish bending vs. other activities. (See p. 129 of the textbook).4
ii. Break each time series in your training set into two (approximately) equal length time series. Now instead of 6 time series for each of the training instances, you have 12 time series for each training instance. Repeat the experiment in 1(d)i, i.e depict scatter plots of the features extracted from both parts of the time series 1,2, and 12. Do you see any considerable difference in the results with those of 1(d)i?
iii. Break each time series in your training set into l ∈ {1, 2, . . . , 20} time series of approximately equal length and use logistic regression5 to solve the binary classification problem, using time-domain features. Remember that breaking each of the time series does not change the number of instances. It only changes the number of features for each instance. Calculate the p-values for your logistic regression parameters in each model corresponding to each value of l and refit a logistic regression model using your pruned set of features.6 Alternatively, you can use backward selection using sklearn.feature selection or glm in R. Use 5-fold cross-validation to determine the best value of the pair (l, p), where p is the number of features used in recursive feature elimination. Explain what the right way and the wrong way are to perform cross-validation in this problem.7 Obviously, use the right way! Also, you may encounter the problem of class imbalance, which may make some of your folds not having any instances of the rare class. In such a case, you can use stratified cross validation. Research what it means and use it if needed.
In the following, you can see an example of applying Python’s Recursive Feature Elimination, which is a backward selection algorithm, to logistic re- gression.
# Recursive Feature Elimination
from sklearn import datasets
from sklearn . feature selection import RFE
3Some logistic regression packages have a built-in L2 regularization. To remove the effect of L2 regular- ization, set λ = 0 or set the budget C → ∞ (i.e. a very large value).
4You are welcome to repeat this experiment with other features as well as with time series 3, 4, and 5 in each instance.
5If you encountered instability of the logistic regression problem because of linearly separable classes, modify the Max-Iter parameter in logistic regression to stop the algorithm immaturely and prevent from its instability.
6R calculates the p-values for logistic regression automatically. One way of calculating them in Python is to call R within Python. There are other ways to obtain the p-values as well.
7This is an interesting problem in which the number of features changes depending on the value of the parameter l that is selected via cross validation. Another example of such a problem is Principal Component Regression, where the number of principal components is selected via cross validation.
2

Homework 3
INF 552, Instructor: Mohammad Reza Rajati
iv.
v.
vi. vii.
from sklearn . linear model import LogisticRegression # load the iris datasets
dataset = datasets . load iris ()
# create a base classifier used to evaluate a subset of attributes
model = LogisticRegression ()
# create the RFE model and select 3 attributes
rfe =RFE(model, 3)
rfe = rfe.fit(dataset.data, dataset.target) # summarize the selection of the attributes
print ( rfe . support ) print(rfe.ranking )
Report the confusion matrix and show the ROC and AUC for your classifier on train data. Report the parameters of your logistic regression βi’s as well as the p-values associated with them.
Test the classifier on the test set. Remember to break the time series in your test set into the same number of time series into which you broke your training set. Remember that the classifier has to be tested using the features extracted from the test set. Compare the accuracy on the test set with the cross-validation accuracy you obtained previously.
Do your classes seem to be well-separated to cause instability in calculating logistic regression parameters?
From the confusion matrices you obtained, do you see imbalanced classes? If yes, build a logistic regression model based on case-control sampling and adjust its parameters. Report the confusion matrix, ROC, and AUC of the model.
(e) Binary Classification Using L1-penalized logistic regression
i. Repeat 1(d)iii using L1-penalized logistic regression,8 i.e. instead of using p- values for variable selection, use L1 regularization. Note that in this problem, you have to cross-validate for both l, the number of time series into which you break each of your instances, and λ, the weight of L1 penalty in your logistic regression objective function (or C, the budget). Packages usually perform cross-validation for λ automatically.9
ii. Compare the L1-penalized with variable selection using p-values. Which one performs better? Which one is easier to implement?
(f) Multi-class Classification (The Realistic Case)
i. Find the best l in the same way as you found it in 1(e)i to build an L1- penalized multinomial regression model to classify all activities in your train- ing set.10 Report your test error. Research how confusion matrices and ROC
8For L1-penalized logistic regression, you may want to use normalized/standardized features
9Using the package Liblinear is strongly recommended.
10New versions of scikit learn allow using L1-penalty for multinomial regression.
3

Homework 3
ii.
iii.
2. ISLR 3.7.4
3. ISLR, 4.7.3
4. ISLR 4.7.7
5. Extra Practice (you do not need to submit the answers): ISLR 3.7.3, 3.7.5, 4.7.4, 4.7.9
INF 552, Instructor: Mohammad Reza Rajati
curves are defined for multiclass classification and show them for this problem
if possible.11
Repeat 1(f)i using a Na ̈ıve Bayes’ classifier. Use both Gaussian and Multi-
nomial priors and compare the results.
Which method is better for multi-class classification in this problem?
11For example, the pROC package in R does the job. 4