Machine Learning Basics with Scikit-learn
Objective
To introduce students to the fundamental concepts and techniques of machine learning using the Scikit-learn library.
Prerequisites
For convenience you can use the terminal window at the OReilly interactive lab: https://learning.oreilly.com/scenarios/ethical-hacking-advanced/9780137673469X002/
- Basic understanding of Python programming.
- Familiarity with data manipulation libraries like Pandas and NumPy.
- Python and necessary libraries installed: Scikit-learn, Pandas, and NumPy.
Lab Outline
-
Introduction to Machine Learning:
- Brief explanation of machine learning and its types (Supervised, Unsupervised).
- Introduction to Scikit-learn library.
-
Setting Up the Environment:
- Installing Scikit-learn, Pandas, and NumPy:
-
Data Preprocessing:
-
Step 1: Importing Necessary Libraries:
-
Step 2: Loading a Dataset:
-
Step 3: Handling Missing Values (if any):
-
Step 4: Splitting the Dataset into Training and Testing Sets:
-
Building Machine Learning Models:
-
Step 5: Training a Decision Tree Model:
-
Step 6: Training a Logistic Regression Model:
-
Evaluating Models:
-
Step 7: Making Predictions and Evaluating Models:
from sklearn.metrics import accuracy_score # For Decision Tree y_pred_dt = dt_classifier.predict(X_test) dt_accuracy = accuracy_score(y_test, y_pred_dt) # For Logistic Regression y_pred_lr = lr_classifier.predict(X_test) lr_accuracy = accuracy_score(y_test, y_pred_lr) print(f"Decision Tree Accuracy: {dt_accuracy}") print(f"Logistic Regression Accuracy: {lr_accuracy}")
-
Hyperparameter Tuning and Cross-Validation:
-
Step 8: Implementing Grid Search Cross-Validation:
from sklearn.model_selection import GridSearchCV # For Decision Tree param_grid_dt = {'max_depth': [3, 5, 7], 'min_samples_split': [2, 5, 10]} grid_search_dt = GridSearchCV(dt_classifier, param_grid_dt, cv=3) grid_search_dt.fit(X_train, y_train) # Best parameters and score for Decision Tree print(grid_search_dt.best_params_) print(grid_search_dt.best_score_)
-
Conclusion and Further Exploration:
- Discuss the results and explore how to further improve the models.
-
Introduce more advanced machine learning techniques and algorithms.
-
Assignment/Project:
- Assign a project where students have to apply the techniques learned in the lab to a real-world dataset and build a predictive model.
Assessment
- Lab Participation: Active participation in lab exercises.
- Quiz: Conduct a short quiz to assess the understanding of students regarding the concepts taught in the lab.
- Project Evaluation: Evaluate the project based on the application of concepts, the accuracy of the model, and the presentation of results.
Resources
- Scikit-learn documentation for detailed guidance on using the library.
- Online courses and tutorials to further explore machine learning concepts.
By the end of this lab, students should be able to understand and implement basic machine learning concepts using the Scikit-learn library. They should also be capable of building and evaluating simple machine learning models.