Open a venv (ctrl+shift+p -> Python: Create Environment) with any modern python version (preferably >= 3.11.0 to avoid any possible dependency conflicts) and then run the LR_model_requirements.txt file.
When installing packages from LR_model_requirements.txt, you can run the following in a venv:
pip install -r requirements.txt
If you're on VSCode, you can install the dependencies after opening a new virtual environment.
Once all dependencies are installed, you can run the program in the terminal with py LR_model.py or python3 LR_model.py if you do not have the shortcut with your version of Python.
digits = load_digits(); X, y = digits.data, digits.target loads the data from the MNIST dataset (using scikit-learn). load_digits() loads the digits dataset (returns a Bunch object). digits.data returns the flattened pixel values of the images. digits.target returns the labels for each flattened image.
X = X / 16.0 normalizes the images to [0, 1] range (Min-Max Scaling to [0,1] instead of 0 to 16.0 - we use 16.0 because load_digits()'s max pixel value is 16.0 -> X.max() returns 16.0). We do not use it here since the accuracy of the model is better without normalization, but in larger-scale models, it would drastically increase the speed of convergence.
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, shuffle=True ) This is where we set the training and testing data explicitly, specifically a 80:20 split. We also set a random seed (generally 0,1, or 42 in documentation) in order to get consistent results.
model = LogisticRegression(max_iter=1000); model.fit(X_train, y_train) sets the model to use LogisticRegression, then we fit the data to the model.
max_iter: The max_iter parameter specifies the maximum number of iterations that the solver is allowed to take in order to converge to a solution.
y_pred = model.predict(X_test); accuracy = np.mean(y_pred == y_test) # calculate accuracy We save the model's predictions and compare it to the actual value to determine accuracy.