Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Programming Notes 2

dfldsfmkdfde

Uploaded by

Duy Bui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Programming Notes 2

dfldsfmkdfde

Uploaded by

Duy Bui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

``` different systems.

Example:
```bash
Detailed Explanation: random Module conda install package_name
The random module is useful for generating random numbers: ```

- **random.random()**: Returns a random float between 0 and 1.


- **random.randint(a, b)**: Returns a random integer between a and b. 3. numpy Package
- **random.randrange(a, b, step)**: Returns a random number in the specified range
numpy (Numerical Python) is a fundamental package for scientific computing in Python.
with a step.
It provides support for
- **random.choice(sequence)**: Returns a random element from the sequence.
large, multi-dimensional arrays and matrices, along with mathematical functions to
- **random.shuffle(sequence)**: Shuffles the elements in the sequence.
operate on these arrays.

Example:
- To install numpy:
```python
```bash
import random
pip install numpy
my_list = [1, 2, 3, 4, 5]
```
random.shuffle(my_list)
- Importing numpy:
print(my_list) # Output: Shuffled list
```python
print(random.randint(10, 20)) # Output: Random integer between 10 and 20
import numpy as np
```
```

Week 8-Packages 4. n-dimensional Arrays

numpy arrays are collections of elements of the same type in n dimensions. They
improve performance over traditional
Python lists by storing data more efficiently.
1. External Packages
Example:
Packages are collections of modules that provide tools for performing complex ```python
programming tasks. Python has an extensive set of external packages that can be used import numpy as np
for tasks such as data analysis, machine learning, and scientific computing. These array_2d = np.array([[1, 2], [3, 4]])
packages are typically found in the Python Package Index (PyPI) and can be installed print(array_2d)
using package managers like pip or conda. ```
2. Python Package Repository and Managers
5. ndarray Class
To manage external packages, Python uses package managers like pip and conda.
The ndarray class is used to represent numpy arrays. Some of its attributes include:
- **pip**: The most common Python package manager used to install and manage
packages from PyPI. - **ndim**: Number of dimensions (axes).
Example: - **shape**: Size of the array in each dimension.
```bash - **size**: Total number of elements.
pip install package_name --user - **dtype**: Data type of the elements.
``` - **itemsize**: Size of each element in bytes.

- **conda**: A modern package manager that can install and manage packages across Example:
```python series_example = pd.Series([1, 2, 3, 4])
numpy_array = np.array([1, 2, 3, 4]) dataframe_example = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
print(numpy_array.shape) # Output: (4,) ```
```

9. Exploring DataFrames
6. Creating numpy Arrays
Once a DataFrame is created, it can be explored using various functions:
There are several ways to create numpy arrays:
- **head()**: View the first few rows.
- **np.array()**: Converts a Python list into an array. - **tail()**: View the last few rows.
- **np.zeros()**: Creates an array filled with zeros. - **describe()**: Get summary statistics.
- **np.ones()**: Creates an array filled with ones. - **columns**: List the column names.
- **np.arange()**: Creates an array with a range of values.
- **np.linspace()**: Creates an array with evenly spaced values. Example:
```python
Example: df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
```python print(df.head())
array_zeros = np.zeros((2, 3)) print(df.describe())
array_range = np.arange(0, 10, 2) ```
```

7. pandas Package Week 9: Sorting and Aggregation in


pandas is a powerful data analysis and manipulation package built on top of numpy. It
provides data structures like Series
Pandas
and DataFrames for handling large datasets, including tabular data.

- To install pandas: 1. Sorting


```bash Sorting helps in understanding data by determining the order of elements. The
pip install pandas .sort_values() function sorts a DataFrame by specific attributes in ascending or
``` descending order.
- Importing pandas:
Function: the_df.sort_values(by, ascending=True)
```python
- "by": column(s) to sort by (string or list)
import pandas as pd
- "ascending": sort order (True for ascending, False for descending)
```
Example:
the_df = the_df.sort_values("Name", ascending=False) # sorts by "Name" in descending
8. pandas Data Structures
order
pandas provides two primary data structures:

- **Series**: A one-dimensional labeled array. 2. Grouping and Aggregation


- **DataFrame**: A two-dimensional, tabular data structure. Grouping allows for categorizing data based on attributes. Aggregation summarizes
groups with statistical functions.
Example:
```python
import pandas as pd
Function: the_df.groupby(by).agg(func) 7. Pivoting and Shaping Data
- "by": column(s) to group by (string or list) Pivoting and reshaping rearranges data based on unique values or structure needs.
- "func": function to apply to grouped data (e.g., sum, count)
Function:
Example: - pivot(): reshapes based on unique values in columns
the_df = the_df.groupby("category").sum() # groups by "category" and sums numerical - melt(): converts wide data to long format
columns
Example:
the_df.pivot(index="ID", columns="category", values="value") # pivots to show
3. Deriving Data categories as columns
Deriving data creates new columns by performing operations on existing ones.

Example: 8. Combining Data


the_df["NewCol"] = the_df["ColA"] + the_df["ColB"] * 0.1 # derives a new column DataFrames can be merged using functions like concat(), join(), and merge().

Function:
4. Data Type Conversion - concat(): joins DataFrames along a particular axis
Data types are often converted for compatibility using the .astype() function. - merge(): SQL-style joins on common columns

Function: the_df[col].astype(dtype) Example:


- "col": column to convert result_df = pandas.concat([df1, df2], axis=1) # concatenates along columns
- "dtype": target data type (e.g., int, float, string)

Example: 9. Text Data Manipulation


the_df["Amount"] = the_df["Amount"].astype(float) # converts "Amount" to float String functions can be applied to manipulate text data.

Function: the_df[col].str.function()
5. Mapping Functions - Functions: len(), lower(), upper(), replace()
Apply functions row-wise, column-wise, or element-wise to modify data.
Example:
Functions: the_df["Name"] = the_df["Name"].str.upper() # converts names to uppercase
- apply(): row/column-wise
- applymap(): element-wise across DataFrame
- map(): element-wise across Series 10. Time Series Data
Time-based data is handled with the datetime64 data type and accessor functions.
Examples:
the_df.apply(lambda x: x * 2) # doubles each element row/column-wise Function:
the_df['Col'].map(lambda x: x.lower()) # converts strings to lowercase in a specific - pd.to_datetime(): converts strings to datetime objects
column - dt accessor: extracts components (e.g., year, month, day)

Example:
6. Renaming and Filtering Columns the_df["date"] = pd.to_datetime(the_df["date"]) # converts string to datetime
Renaming and filtering are useful for clarity and data reduction. the_df["year"] = the_df["date"].dt.year # extracts the year component

Function for renaming: the_df.rename(columns={old_name: new_name})


Filtering columns: the_df = the_df[['keep_column1', 'keep_column2']]

Example:
the_df = the_df.rename(columns={"old_col": "new_col"}) # renames a column
the_df = the_df[["col1", "col2"]] # filters the DataFrame to keep only certain columns
Week 10: Detailed Code for Data
fig = plt.figure(figsize=(3.5, 2.5)) # Create figure
ax = fig.subplots(2, 1, sharex=True) # Create two subplots sharing the x-axis

Visualisation with Python # Plot in the first subplot


ax[0].plot(x, y, 'r--', label='Y') # Dashed red line
ax[0].set_title("set_1") # Set title
ax[0].set_xlim((0, 12)) # Set limits
1. Single Line Plot Example
ax[0].set_ylim((0, 25)) # Set y-limits
ax[0].legend()
import matplotlib.pyplot as plt
# Plot in the second subplot
x = list(range(10))
ax[1].plot(x, z, 'ob', label='Z') # Blue circle markers
y = list(range(0, 20, 2))
ax[1].set_title("set_2") # Set title
z = list(range(20, 40, 2))
ax[1].set_xlim((0, 12)) # Same x-axis limits
ax[1].set_ylim((0, 50)) # Set y-limits
fig = plt.figure(figsize=(3.5, 2.5)) # Create figure
ax[1].legend()
ax = fig.subplots() # Add subplot to the figure
# Adjust layout to avoid overlap
# Plot two datasets on the same axes
plt.tight_layout()
ax.plot(x, y, 'r', label='set_1') # Red line for first set
plt.show()
ax.plot(x, z, 'b', label='set_2') # Blue line for second set

# Set title and axis labels


ax.set_title("Number") 3. Scatter Plot
ax.set_xlabel("X")
ax.set_ylabel("Y")
import numpy as np
import matplotlib.pyplot as plt
# Set limits for X and Y axes
ax.set_xlim((0, 12))
x = list(range(100))
ax.set_ylim((0, 50))
y = list(range(100)) + 50 * np.random.random(100) # Random values added

# Add legend
fig = plt.figure(figsize=(5, 5))
ax.legend()
ax = fig.subplots()

# Show the plot


# Create a scatter plot
plt.show()
ax.scatter(x, y, color='r')

# Set axis labels


ax.set_xlabel("X")
2. Dual Plot Example
ax.set_ylabel("Y")

import matplotlib.pyplot as plt


# Show plot
plt.show()
x = list(range(10))
y = list(range(0, 20, 2))
z = list(range(20, 40, 2))
4. Bar Chart
x = np.random.randn(1000) # Random normal data
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(5, 5))
x = ["a", "b", "c", "d"] ax = fig.subplots()
y = [20, 4, 10, 15]
# Create a histogram
fig = plt.figure(figsize=(5, 5)) ax.hist(x, label="Random Data", color="r")
ax = fig.subplots()
# Set axis labels and legend
# Create a bar chart ax.set_xlabel("Value")
ax.bar(x, y, label="Score") ax.set_ylabel("Frequency")
ax.legend()
# Set axis labels and legend
ax.set_xlabel("Code") plt.tight_layout()
ax.set_ylabel("Value") plt.show()
ax.legend()

plt.tight_layout()
7. Pandas Plotting Example
plt.show()

import pandas as pd
import matplotlib.pyplot as plt
5. Pie Chart
# Create a DataFrame
import matplotlib.pyplot as plt df = pd.DataFrame({
'length': [1.5, 0.5, 1.2, 0.9, 3],
x = [2, 4, 10, 15] 'width': [0.7, 0.2, 0.15, 0.2, 1.1]
labels = ["x1", "x2", "x3", "x4"] }, index=['pig', 'rabbit', 'duck', 'chicken', 'horse'])

fig = plt.figure(figsize=(5, 5)) # Plot DataFrame


ax = fig.subplots() df.plot(title="DataFrame Plot")
plt.show()
# Create a pie chart
ax.pie(x, labels=labels, autopct="%.1f%%")
8. Pandas Series Plotting Example
ax.legend()

import pandas as pd
plt.tight_layout()
import matplotlib.pyplot as plt
plt.show()

# Create a Series
6. Histogram srs = pd.Series([1, 2, 3, 3])

import numpy as np # Plot the Series as a histogram


import matplotlib.pyplot as plt srs.plot(kind='hist', title="Series Plot")
plt.show() • 'w': Opens a file for writing (overwriting existing content).

• 'a': Opens a file for appending new data to the end.

Lecture 11: File Input and Output 5. Reading Example


file_ptr = open("example.txt", "r")
1. Introduction to File I/O the_lines = file_ptr.readlines()
print(the_lines)
When handling more data than can be processed through screen input/output, or when
file_ptr.close()
interacting with other systems, we use files. Python's built-in `open()` function creates
file objects that allow reading and writing operations.
for each_line in the_lines:
File objects are created using: print(each_line.strip()) # Removes newline characters

the_ptr = open(file_name, 'mode')


Explanation: This code reads the file into a list and prints each line after stripping
The optional third argument 'buffer' is used for handling large files. newline characters using `strip()` method.

2. Types of Files 6. Writing Example


Files are categorized into two types: file_ptr = open("output.txt", "w")
file_ptr.write("I create this file using Python.")
• Text files: Plain text or specialized text-based data (CSV, JSON).
file_ptr.write("I write headers and values.")
• Binary files: Files containing binary data (images, videos, etc.). file_ptr.write("")
file_ptr.write("Name Age Number")
Regardless of file type, the process is the same: Open the file, process it (read/write), file_ptr.write("Jim 16 2090943")
and close the file. file_ptr.close()

3. File Object Methods Explanation: This example demonstrates writing to a file. Escape sequences like '\n' are
Python provides several methods to operate on files: used for newlines, and '\t' for tabs.

• the_ptr = open(filename, 'mode'): Opens the file with the specified mode (read, write,
etc.). 7. Binary Files – Access Modes
Binary files are accessed in the same way as text files, but with 'b' added to the mode:
• the_ptr.read(size): Reads a specified number of bytes from the file.
• 'rb': Opens a binary file for reading.
• the_ptr.readline(): Reads the next line from the file.
• 'wb': Opens a binary file for writing (overwriting content).
• the_ptr.readlines(): Reads the entire file line-by-line as a list.
• 'ab': Opens a binary file for appending.
• the_ptr.write(line_str): Writes the string to the file.

• the_ptr.close(): Closes the file when done, saving changes.


8. Binary Files Example
file_ptr = open("example.bin", "wb")
4. Text Files – Access Modes the_list = list(range(12))
Access modes specify how a file will be handled: list_bin = bytearray(the_list)
file_ptr.write(list_bin)
• 'r': Opens a file for reading. file_ptr.close()
12. JSON Files
file_ptr = open("example.bin", "rb") JSON (JavaScript Object Notation) is a text-based format used to store and transmit key-
the_list = list(file_ptr.read()) value data, similar to Python dictionaries.
print(the_list)
file_ptr.close()
13. JSON Example
import json
Explanation: Binary data is written to and read from a binary file. A list is converted to a
bytearray before writing to the file.
# JSON string
json_data = """
9. CSV Files {
CSV (Comma Separated Values) files are text files with a structured format for tabular "number": 118,
data, where rows are separated by newlines and columns by commas. "name": "Jim Smith",
"age": 42,
"city": ["Perth", "Melbourne"],
10. CSV Reading Example "Status": null
import csv }
"""
file_ptr = open("example.csv", "r")
csv_reader = csv.reader(file_ptr) # Convert JSON string to a dictionary
the_dict = json.loads(json_data)
for each_row in csv_reader: print(the_dict)
print(each_row)
# Modify and write the dictionary back to a JSON file
file_ptr.close() the_dict["Status"] = True
the_dict["Postcode"] = [6000, 3000]
with open("example.json", "w") as json_file:
Explanation: The `csv.reader()` function reads the contents of a CSV file row by row, json.dump(the_dict, json_file)
returning each row as a list.

Explanation: This code converts a JSON string into a Python dictionary using
11. CSV Writing Example `json.loads()`. The dictionary is then modified and written back to a JSON file using
import csv `json.dump()`.

file_ptr = open("example.csv", "w")


csv_writer = csv.writer(file_ptr)
14. Excel Files
Excel files (.xlsx) can be read and written using the `pandas` library or `openpyxl`
data = [["Name", "Age"], ["Jim", 16], ["Helen", 34]] module. Pandas provides an easy-to-use interface for working with tabular data.
for each_row in data:
csv_writer.writerow(each_row)
15. Excel Example with Pandas
file_ptr.close() import pandas as pd

# Read from an Excel file


Explanation: The `csv.writer()` function writes rows of data to a CSV file, where each df = pd.read_excel("example.xlsx")
row is a list.
# Write to an Excel file
df.to_excel("output.xlsx", index=False) encapsulates both data (attributes) and behaviors (methods). The state of an object
refers to its attributes, while the methods define its behavior.

Explanation: This example reads an Excel file into a Pandas DataFrame using
`pandas.read_excel()` and writes it back to another Excel file using `to_excel()`. 2. Key Features of OOP
OOP introduces several key features that make it a powerful paradigm for complex
problem-solving:
16. CSV DictReader Example
The `csv.DictReader` function reads a CSV file and maps each row into a Python  • **Encapsulation**: Hiding internal details by exposing only a public interface
dictionary where the keys are taken from the first row (header). This is especially useful (methods) for interacting with an object's data (attributes).
when working with CSV files that have column headers, allowing easy access to values  • **Inheritance**: Allows a new class (subclass) to inherit properties and behaviors
by their field names. from an existing class (superclass). This promotes code reuse and reduces
redundancy.
import csv
 • **Polymorphism**: The ability of an object to take on different forms, either by
method overloading (same method name with different parameters) or subclassing
# Open the CSV file in read mode
(different implementation in child classes).
file_ptr = open("example.csv", "r")
csv_reader = csv.DictReader(file_ptr) • **Abstraction**: Simplifies complex logic by hiding unnecessary details and showing
only essential features.
# Iterate over each row and print it as a dictionary
for each_row in csv_reader:
print(each_row) 3. Creating a Class
A class serves as a blueprint for creating objects. It defines the structure (attributes and
file_ptr.close() methods) that will be shared by all objects created from that class.

Example:
Explanation: In this example, the `csv.DictReader()` reads the CSV file row by row. Each class Pet:
row is returned as an OrderedDict, where the keys correspond to the column headers. """A simple class to represent a pet."""
The `for` loop iterates over each row, allowing you to access the data by field names
(headers). This method is useful for structured CSV files where each column represents def __init__(self, species, breed):
a specific attribute, such as 'Name', 'Age', and 'Score'. self.species = species
self.breed = breed
Key points:

 • The first row of the CSV file must contain the headers (field names). Explanation: In this example, we define a `Pet` class with two attributes: `species` and
 • The values in the dictionary can be accessed by their respective header names (e.g., `breed`. The `__init__` method is called when an object is instantiated and is used to
`each_row['Name']`). initialize the object's attributes.
 • This method simplifies accessing specific fields in CSV rows without needing to
manage index positions manually.
4. Instantiating an Object
Week 12-Oriented Programming Once a class is defined, you can create instances (objects) of that class by calling it like a
function.

Example:
1. Introduction to Object Orientation my_pet = Pet("Dog", "Pug")
Object-oriented programming (OOP) is a programming paradigm that focuses on
modeling real-world entities as 'objects'. Each object is an instance of a class, which
Explanation: Here, `my_pet` is an instance of the `Pet` class, and it represents a dog of the
breed 'Pug'.
5. Object Methods def greeting():
Methods are functions defined within a class that operate on instances of the class. They return "Hello, world!"
can access and modify an object's attributes.

Example: Explanation: The `greeting()` method is defined as a static method using the
class Student: `@staticmethod` decorator. It does not access any instance-specific data.
def __init__(self, first_name, last_name, student_number):
self.first_name = first_name
9. Inheritance
self.last_name = last_name
Inheritance allows a class to inherit attributes and methods from another class. This
self.student_number = student_number
promotes code reuse by creating specialized subclasses from more general classes.
def full_name(self): Example:
return f"{self.first_name} {self.last_name}" class Programmer(Student):
def __init__(self, first_name, last_name, student_number, language):
super().__init__(first_name, last_name, student_number)
Explanation: In this example, the `full_name()` method combines the `first_name` and
self.language = language
`last_name` attributes and returns the full name of the student.

def __str__(self):
6. Accessor Methods (Getters) return f"{self.first_name} {self.last_name} ({self.language})"
Accessor methods, or getters, are used to retrieve the value of an attribute while
maintaining encapsulation. Explanation: In this example, `Programmer` is a subclass of `Student`. It inherits the
Example: attributes and methods from `Student` and adds a new attribute, `language`.
def get_first_name(self):
return self.first_name
10. Aggregation of Classes
Aggregation occurs when an object of one class contains objects from another class as
Explanation: This accessor method returns the value of the `first_name` attribute. part of its attributes.

Example:
7. Mutator Methods (Setters) class Lecture:
Mutator methods, or setters, are used to modify the value of an attribute. They help def __init__(self, students, lecture_name):
enforce encapsulation by controlling how attributes are modified. self.students = students
self.lecture_name = lecture_name
Example:
def set_first_name(self, first_name): def number_of_students(self):
self.first_name = first_name return len(self.students)

Explanation: This mutator method allows the `first_name` attribute to be updated. Explanation: In this example, the `Lecture` class contains a list of `Student` objects as one
of its attributes, demonstrating aggregation.

8. Static Methods
Static methods do not operate on class attributes or require an instance of the class.
They are typically used as utility or helper functions within a class.

Example:
@staticmethod

You might also like