1
$\begingroup$

I am using this code that I found here for logistic regression for binary classification with 2 classes.

Git Repo

The data that I am testing with is calling for multi-class classification (6 classes).

Can I use the same code found above if I have 6 classes? Such that the prediction falls into one of the classes or activities (walking, walkingupstairs, walkingdownstairs, sitting, standing, lying).

Kaggle

Any advice is highly appreciated.

Update

I have tried to change the model in set_weights and train_model from

model=linear_model.SGDClassifier()

to

model = LogisticRegression(solver='lbfgs', multi_class='ovr', max_iter=1000, random_state=20)

but its not working !!!!!!

TypeError: '(array([402, 286, 419, 194, 644, 151, 127, 567, 359,   7, 616, 209, 465,
       546, 144, 182, 582, 528, 263, 192, 185, 690, 241, 211,  36, 296,
       143, 637, 401, 264,  74, 365, 385, 700,  28, 514, 406, 606, 328,
       410, 673, 726, 260, 276, 572, 103, 398, 267, 256, 550, 563, 321,
        46, 203, 463, 195, 702, 405, 343, 392, 476, 670,  60, 427, 474,
       197, 645,  55, 610, 175, 647, 367, 641, 364, 573, 593, 213, 251,
       366, 489, 262, 706, 268, 140, 730, 345, 425, 512, 487, 608,  61,
       319, 115, 284,  99, 409, 456, 464, 310, 727, 313, 447, 125, 400,
       481, 181, 722, 322, 352, 325, 375, 171, 347, 714, 318, 294, 655,
       383, 164, 141,  89, 253, 513, 293, 633, 530, 568, 350, 157, 218,
         5, 468, 155, 547,   1, 496,  76, 624, 395, 643, 397, 139, 266,
       721, 255, 196, 170, 583, 622, 349, 459,  49, 515, 179, 215,  27,
       698, 704, 581,  71, 537, 661, 107, 129, 500, 399,  38, 339, 600,
       564, 679, 326, 615, 231, 305, 176, 617, 225, 467, 353,  35, 723,
        82,  92, 334,  16, 230, 686, 440, 455, 101, 435, 521, 189, 295,
       220, 205, 362, 363, 242, 493, 154, 495, 420, 597, 710, 660,  79,
       344, 628, 707, 720, 450, 333, 243, 239, 236, 559, 689,  50, 120,
       108, 498, 329, 372, 477, 497, 502, 626, 360,  62, 436, 148, 434,
       540, 370, 542, 109, 407,  41, 130, 575, 638, 177, 183,  40, 691,
       439, 158, 671,  53, 556, 548, 279, 228, 506, 156, 659, 636,  95,
       371,  19, 412,  39, 348, 377, 566,  14, 443, 240, 390, 428, 100,
       229, 532, 501, 237, 404, 475, 423, 551, 553, 257,  17, 664, 373,
        66, 729, 601, 159, 499, 314,  86, 433, 522, 524,  96, 351, 238,
       165, 642, 719,  42, 186, 217, 336, 374, 571, 299, 278, 316, 718,
       309, 437,  75, 393, 574, 162, 557, 713, 529,   2,  32, 672, 486,
        29,  10,  84, 270, 453, 442, 562,  26, 356, 288, 461, 509, 711,
       355, 346,  45, 701, 519, 135, 525, 303, 152, 577, 684, 651,  91,
         6, 591, 460, 161, 222, 254, 387, 160, 283, 683, 488, 607, 479,
        78, 538, 394, 199, 269,  31, 457, 630,  68, 535, 623, 658, 180,
       611,   0,  63, 511, 482, 212,  13,   9, 248, 214, 668, 190, 579,
       517, 149, 484, 552, 444, 682, 124, 386, 697, 118, 543, 297, 112,
       424, 308,  21, 138, 411, 648, 639, 470, 210,  56, 280,  18,  33,
       667, 324, 445, 508, 272, 249, 545, 287, 202,  43, 632, 709, 678,
       235, 587, 430, 654, 656, 223, 687, 491,  25, 187,  85, 677, 458,
       408,   3, 134,  30,  23,  15, 614, 657, 292, 131, 233,  12, 724,
       198, 145,  59, 178, 734, 304, 403, 712, 396, 596, 332, 142, 281,
       301, 216, 116, 376, 106,  88, 646, 174, 503, 603,  34, 389, 358,
       110, 341, 167, 416, 302, 146, 681, 485, 317, 570, 361, 438, 219,
       388, 422,  70, 415,  73, 132, 618, 733, 191, 728, 426, 380, 505,
       688, 261, 111, 588, 699,  77, 122, 137, 166, 612, 227,  94, 695,
       592, 717, 330, 716, 446, 311,  93, 494, 173, 452, 674, 282,  24,
       431, 391,  57, 539, 207,  69, 413, 555, 441,  90, 676, 153, 421,
       201,   4, 629, 432, 472, 594, 554, 451, 731, 478, 119, 483, 634,
       289, 666, 273, 703, 640,  22, 578, 705, 580, 265, 449, 369, 354,
       384, 662, 105, 584, 234, 586, 277, 680, 585, 469, 298, 480, 121,
       536, 417, 527,   8, 315, 306, 605, 275,  11, 454, 516, 340,  54,
       549, 693, 113,  58, 589, 184, 665, 320,  47, 102, 448, 337,  52,
       274, 627, 418, 259,  87, 569, 685, 471, 379, 381,  37, 526, 631,
       590, 669, 725, 378, 128, 533,  65,  72, 342, 335, 504, 221,  48,
       675, 252, 307, 692, 245, 708, 188, 200, 561, 150, 715,  83, 599,
       732, 531, 114, 285, 625,  20, 126, 382, 635, 104, 123, 291, 598,
       541, 649,  51, 534, 414, 331, 694, 163, 206, 258, 652, 224, 576,
       204,  80,  64, 518, 133, 602, 650, 492, 558, 250, 290, 312, 327,
       208, 338, 246, 271, 696,  98,  44, 117, 609, 323, 466,  81, 462,
       247, 621, 544, 613, 604, 523, 147, 429, 193, 595, 244, 619, 520,
       300, 565, 653, 172, 473, 226, 168, 620, 368,  67, 169, 490, 510,
       507, 232, 560, 663, 357,  97, 136]),)' is an invalid key
$\endgroup$

1 Answer 1

0
$\begingroup$

Yes, You can use the same model. Just change the number of classes. That's it. It will produce probabilities for each class.

use the probabilities to extract the class index with max probability and that will be the class for the input.

Let's dig a little bit deeper. Let's say you need to predict 2 classes (0, 1),then SGDClassifier produces class directly whether 0 or 1

If the dataset has multiple classes to predict. You can still use SGDClassifier.

SGDClassifier supports multi-class classification by combining multiple binary classifiers in a “one versus all” (OVA) scheme. For each of the classes, a binary classifier is learned that discriminates between that and all other classes. we compute the confidence score (i.e. the signed distances to the hyperplane) for each classifier and choose the class with the highest confidence.

check this link

$\endgroup$
5
  • 1
    $\begingroup$ Please explain a bit more with links to Sklearn SGDClassifer supporting multi-class. So that it can become a generic answer. Also, that it's not inherently multiclass but O-v-R @DivyaReddy $\endgroup$
    – 10xAI
    Commented Sep 19, 2020 at 10:20
  • $\begingroup$ @DivyaReddy I have tried to change the model in set_weights and train_model from model=linear_model.SGDClassifier() to model=OneVsRestClassifier(SVC()), but its not working !!!!!! $\endgroup$ Commented Sep 19, 2020 at 17:27
  • $\begingroup$ what is the error? edit the question with the error and where the code goes wrong $\endgroup$ Commented Sep 20, 2020 at 4:52
  • $\begingroup$ @ Divya reddy I have tried to change the model in set_weights and train_model from model=linear_model.SGDClassifier() to model = LogisticRegression(solver='lbfgs', multi_class='ovr', max_iter=1000, random_state=20) but its not working !!!!!! $\endgroup$ Commented Sep 20, 2020 at 8:55
  • $\begingroup$ how are you are passing data for training? I guess, it's a slicing problem. ex: data['a','b','c'] will produce that error. data[['a','b','c']] is correct $\endgroup$ Commented Sep 20, 2020 at 10:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.