简体   繁体   中英

There is an error with sklearn (LogisticRegression model selection)

    import numpy as np
    import matplotlib.pyplot as plt
    import pandas as pd
    from sklearn import datasets
    from sklearn.model_selection import train_test_split
    from sklearn.linear_model import LogisticRegression
    from sklearn.preprocessing import StandardScaler


    Dt = pd.read_csv("D:\wisc_bc_data.csv")
    '''
    print(Dt.shape)     
    print(Dt.head())
    '''
     def changer(x):
         if x == 'B':
            return 0
         else:
            return 1
     Dt['diagnosis'] = Dt['diagnosis'].map(lambda x: changer(x))
     features = Dt[2:12]
     Diagnosis = Dt['diagnosis']
     train_features, test_features, train_labels, test_labels = train_test_split(features, Diagnosis) 'this line emits error code'

     '''
     this is my code and i used dataset from here: https://gomguard.tistory.com/52
     '''

I'd like to split data for logistic regression. However, There was an error code like this:

ValueError Traceback (most recent call last) in ----> 1 train_features, test_features, train_labels, test_labels = train_test_split(features, Diagnosis)

D:\\python\\lib\\site-packages\\sklearn\\model_selection_split.py in train_test_split(*arrays, **options) 2116 raise TypeError("Invalid parameters passed: %s" % str(options)) 2117 -> 2118 arrays = indexable(*arrays) 2119 2120 n_samples = _num_samples(arrays[0])

D:\\python\\lib\\site-packages\\sklearn\\utils\\validation.py in indexable(*iterables) 246 """ 247 result = [_make_indexable(X) for X in iterables] --> 248 check_consistent_length(*result) 249 return result 250

D:\\python\\lib\\site-packages\\sklearn\\utils\\validation.py in check_consistent_length(*arrays) 210 if len(uniques) > 1: 211 raise ValueError("Found input variables with inconsistent numbers of" --> 212 " samples: %r" % [int(l) for l in lengths]) 213 214

ValueError: Found input variables with inconsistent numbers of samples: [10, 569] ​ how can I fix it?

I think features = Dt[2:12] results in your error. Your attempt is to slice features, but the python interprets the code to slice records. So, change the code as Dt.iloc[:, 2:12 ].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM