Python #6 Packages
Unit 6: Packages
Learn how Python modules and scientific-computing packages help you reuse code, organize programs, analyse data, and solve problems efficiently.
Study each concept, run the examples in the browser, complete the practicals, then plan and test both mini-projects.
Study notes
Modules, imports, packages and common data-science tools.
Run Python
Use browser-based Python; selected labs load packages when needed.
Build projects
Explore data analysis and a simple machine-learning workflow.
Choose a topic ๐
Tap any card to jump to its notes.
Built-in Modules
A module is a Python file containing reusable definitions such as functions, classes and variables. Python ships with a standard library of modules, so common tasks do not need to be programmed from scratch.
mathprovides mathematical functions and constants.randomgenerates pseudo-random values.statisticsprovides common statistical calculations.datetimehandles dates and times;osandsyssupport operating-system and interpreter tasks.
import module makes the module available under its name. Use module.member to access its functions or values.Exam quick notes: A module improves reuse and organization. Standard-library modules are included with Python; third-party packages generally need to be installed in the environment.
User-Defined Modules
A user-defined module is Python code saved in a .py file. Functions and constants can be reused by importing the file into another program.
- Keep related functions together in a clearly named file.
- Import with
import helpersor selected names withfrom helpers import rectangle_area. - Avoid naming your file after a standard module (for example,
random.py), because it can shadow the real module.
types.ModuleType so it can run in the browser.Remember: reusable functions, constants and classes can be placed in modules to reduce duplication and make testing easier.
NumPy
NumPy is a third-party package for efficient numerical computing. Its central object is the multidimensional ndarray, which supports vectorized operations and common mathematical tools.
- Create arrays using
np.array(),np.zeros()ornp.arange(). - Array operations can apply to every element without writing an explicit loop.
- Array shapes and data types matter when working with matrices and scientific data.
SciPy
SciPy builds on NumPy and offers scientific algorithms for tasks such as optimization, integration, statistics, signal processing and linear algebra.
Use SciPy when: you need a ready-made scientific algorithm rather than implementing it manually. The exact submodule depends on the task, such as scipy.integrate, scipy.optimize or scipy.stats.
Pandas
Pandas is used to work with labelled and tabular data. A Series is one-dimensional; a DataFrame is a two-dimensional table with rows and columns.
- Select a column with
df["Mark"]. - Filter rows with a condition such as
df[df["Mark"] >= 85]. - Use
read_csv()andto_csv()for CSV data when file access is available.
Scikit-learn
Scikit-learn provides tools for machine learning, including classification, regression, clustering, preprocessing and model evaluation.
fit()trains a model on examples.predict()produces predictions for new inputs.- Keep test data separate from training data to estimate performance fairly.
- Choose a metric appropriate to the task; accuracy alone may be misleading for imbalanced classes.
X and target vector y before it can run. Package installation and memory availability depend on the runtime.Practical Python Lab ๐งช
Four practicals (minimum requested: three).
The first run downloads the Python runtime. Package-based examples may need additional downloads; allow a little time and keep the page open.
Mini Projects ๐
Minimum two projects included.
Student Performance Analyzer
Use NumPy and Pandas to calculate average marks, identify high performers and summarize subject-wise results.
Requirements
- Represent marks as a table with student names and subject columns.
- Calculate each student's average and each subject's mean.
- Filter students whose average is at least 80.
- Sort students by average in descending order.
- Write a short conclusion based on the results.
Success criteria: correct calculations, clear tabular output, reusable functions, and readable variable names.
Simple Flower Classifier
Explore a supervised machine-learning workflow using Scikit-learn's built-in Iris dataset.
Requirements
- Load the Iris dataset and inspect its features and target classes.
- Split data into training and testing sets.
- Train a simple classifier such as a decision tree or k-nearest neighbors.
- Predict the test labels and calculate accuracy.
- Explain why a test set is needed and state one limitation of accuracy.
Success criteria: reproducible split, correct use of fit() and predict(), evaluation, and a brief interpretation.
Unit 6 Quiz ๐ง
12 multiple-choice questions.
Celebrate each step ๐
Exam quick notes ๐
- Module: reusable Python code, usually in a
.pyfile. - Package: a way to organize related modules; third-party packages may need installation.
- NumPy: numerical arrays, vectorization and array mathematics.
- SciPy: scientific routines such as integration, optimization and statistics.
- Pandas: Series and DataFrames for structured data handling and analysis.
- Scikit-learn: preprocessing, model fitting, prediction and evaluation.
- Good practice: import only what you need, check package availability, use meaningful names and test results.
Comments
Post a Comment