1
$\begingroup$

I have $3$ datasets I'd like to run a logistic regression on split into

  1. $X_1, y_1$

  2. $X_2, y_2$

  3. $X_3, y_3$

How can I run a loop so that I can run an automated logistic regression using the X_train, X_test split function from SkLearn and in doing so printing 3 separate accuracy results for each dataset?

To run on one data set of X, y is as follows:

X_train, X_test, y_train, y_test = train_test_split(X, y, 
test_size=0.25, shuffle=False)

logreg = LogisticRegression()
logreg.fit(X_train,y_train)
y_pred = logreg.predict(X_test)

print('Accuracy:',metrics.accuracy_score(y_test, y_pred))
$\endgroup$
  • $\begingroup$ you could make a list of your files and loop over the files to make a regression for each... $\endgroup$ – Peter Jun 21 at 18:35
1
$\begingroup$

Having $(X1,y1), (X2,y2), (X2,y2)$ saved in a dictionary:

dictionary = {X1:y1, X2:y2, X3:y3}
accuracies = []
for k,v in dictionary.items():
    X_train, X_test, y_train, y_test = train_test_split(k, v, 
    test_size=0.25, shuffle=False)
    logreg = LogisticRegression()
    logreg.fit(X_train,y_train)
    y_pred = logreg.predict(X_test)
    accuracies.append(metrics.accuracy_score(y_test, y_pred))

But it's not really a good way especially when the datasets are too big.


So:

accuracies = []
for i in range(1,4):
    X = 'X{}'.format(i)
    y = 'y{}'.format(i)
    X_train, X_test, y_train, y_test = train_test_split(vars()[X], vars()[y], 
    test_size=0.25, shuffle=False)
    logreg = LogisticRegression()
    logreg.fit(X_train,y_train)
    y_pred = logreg.predict(X_test)
    accuracies.append(metrics.accuracy_score(y_test, y_pred))

Note: in order to access the varibale names which are different in a small part, like here for X1,X2,..., we can use the mentioned method:

import numpy as np
X1= np.arange(1,10)                                                                                                                                
y1=[i**2 for i in X1]                                                                                                                                
X2= np.arange(-5,5)                                                                                                                                
y2=[i**2 for i in X2]                                                                                                                          
for i in range(1,3): 
    X = 'X{}'.format(i) 
    y = 'y{}'.format(i) 
    print('X_{}'.format(i) , vars()[X])
    print('y_{}'.format(i) , vars()[y])

Output:

X_1 [1 2 3 4 5 6 7 8 9]
y_1 [1, 4, 9, 16, 25, 36, 49, 64, 81]
X_2 [-5 -4 -3 -2 -1  0  1  2  3  4]
y_2 [25, 16, 9, 4, 1, 0, 1, 4, 9, 16]
$\endgroup$
  • $\begingroup$ Thanks, but when I run I get the following error: TypeError: unhashable type: 'numpy.ndarray' $\endgroup$ – Novice Python charmer Jun 26 at 19:09
  • $\begingroup$ @Novice Python charmer My fault, the first approach of using arrays as dictionary keys might have risen the problem. Have you tried the second approach? $\endgroup$ – Fatemeh Asgarinejad Jun 26 at 23:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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