Skip to content

Commit

Permalink
Merge branch 'main' into pytorch-tensor-nonzero
Browse files Browse the repository at this point in the history
  • Loading branch information
Sriparno08 authored Dec 27, 2024
2 parents a785a87 + 0bf782d commit 828b916
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
66 changes: 66 additions & 0 deletions content/numpy/concepts/array-broadcasting/array-broadcasting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
Title: 'Array Broadcasting'
Description: 'Refers to the process of expanding the shape of a smaller array to match the shape of a larger array during arithmetic operations.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'NumPy'
- 'Math'
- 'Methods'
- 'Arrays'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

In NumPy, **array broadcasting** refers to the process of expanding the shape of a smaller [array](https://www.codecademy.com/resources/docs/numpy/ndarray) to match the shape of a larger array during arithmetic operations. This is helpful when there is a need to perform mathematical operations on two arrays of different shapes.

## Example

The following example demonstrates the usage of array broadcasting:

```py
import numpy as np

# Create an array of size (1 x 4)
arr1 = np.array([[11, 12, 13, 14]])

# Create an array of size (2 x 4)
arr2 = np.array([[21, 22, 23, 24], [25, 26, 27, 28]])

# Add the arrays
res = arr1 + arr2

# Print the result
print(res)
```

In the above example, the shape of the smaller array `arr1` (1 x 4) is expanded to the shape of the larger array `arr2` (2 x 4) during addition. After expansion, the array `arr1` looks like `[[11, 12, 13, 14], [11, 12, 13, 14]]`.

The above code produces the following output:

```shell
[[32 34 36 38]
[36 38 40 42]]
```

## Codebyte Example

The following codebyte example demonstrates the usage of array broadcasting:

```codebyte/python
import numpy as np
# Create an array of size (1 x 3)
arr1 = np.array([[31, 32, 33]])
# Create an array of size (2 x 3)
arr2 = np.array([[41, 42, 43], [44, 45, 46]])
# Add the arrays
res = arr1 + arr2
# Print the result
print(res)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
Title: 'SQLAlchemy'
Description: 'SQLAlchemy is a flexible SQL toolkit and Object-Relational Mapping (ORM) library for Python, designed to enable efficient and Pythonic database interactions.'
Subjects:
- 'Data Science'
- 'Web Development'
Tags:
- 'Database'
- 'SQL'
- 'Python'
CatalogContent:
- 'learn-python-3'
- 'paths/data-science'
---

**SQLAlchemy** is a widely used Python library that provides both high-level ORM and low-level SQL query capabilities. It simplifies database interactions by allowing developers to use Python objects for database manipulation while also supporting raw SQL queries. SQLAlchemy supports various relational databases like MySQL, PostgreSQL, SQLite, and others, and is highly customizable to suit complex database models. With SQLAlchemy, developers can interact with databases in a flexible and efficient manner, making it an essential tool for web development and data science projects.

## Syntax

```pseudo
from sqlalchemy import create_engine
# Create an engine instance
engine = create_engine('dialect+driver://username:password@host:port/database')
# Example of querying using SQLAlchemy
with engine.connect() as connection:
result = connection.execute("SELECT * FROM users")
for row in result:
print(row)
```

- `create_engine`: Initializes the connection to the database with the provided database URI.
- `dialect`: Specifies the type of database (e.g., `mysql`, `postgresql`, `sqlite`).
- `driver` (Optional): Specifies the database driver to be used.
- `username`, `password`: Credentials for authentication.
- `host`, `port`: Database server's location.
- `database`: Name of the database to connect to.

## Example

The following example shows how to connect to a SQLite database and query it using SQLAlchemy:

```py
from sqlalchemy import create_engine

# Create an engine instance for SQLite
engine = create_engine('sqlite:///example.db')

# Connect to the database and then execute a query
with engine.connect() as connection:
result = connection.execute("SELECT * FROM users")
for row in result:
print(row)
```

This code will output the rows from the `users` table in the `example.db` SQLite database.
54 changes: 54 additions & 0 deletions content/sql/concepts/commands/terms/truncate/truncate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
Title: 'TRUNCATE'
Description: 'Deletes all rows from a table while retaining the table structure.'
Subjects:
- 'Data Science'
- 'Data Visualization'
Tags:
- 'Data'
- 'Database'
- 'Functions'
- 'SQL'
CatalogContent:
- 'learn-sql'
- 'paths/analyze-data-with-sql'
---

**`TRUNCATE`** is a SQL statement used to quickly delete all rows from a table while retaining the table structure. Unlike the **`DELETE`** statement, `TRUNCATE` does not log individual row deletions and does not generate triggers, making it faster for large datasets.

## Key Characteristics

- _Performance_: `TRUNCATE` is more efficient than `DELETE` because it uses minimal logging.
- _Data Integrity_: It resets identity columns (e.g., `AUTO_INCREMENT`) to their seed values.
- _Irreversibility_: `TRUNCATE` operations cannot be rolled back in most database systems, as it does not log row-level changes.
- _Constraints_: Cannot truncate a table that is referenced by a foreign key.

## Comparison: TRUNCATE vs. DELETE

| Feature | `TRUNCATE` | `DELETE` |
| --------------------- | ----------------------------- | ------------------------- |
| Row Logging | No | Yes |
| Triggers | Not activated | Activated |
| Rollback | Not supported in many systems | Supported |
| Identity Column Reset | Yes | No |
| Performance | Faster | Slower for large datasets |

## Syntax

```pseudo
TRUNCATE TABLE table_name;
```

- `table_name`: Specifies the name of the table to truncate.

> **Note:** Use `TRUNCATE` carefully in production environments, as it cannot be undone.
## Example

The `TRUNCATE` statement can be used to clear all records from a table while retaining its structure. For instance, to remove all data from a table named `employee_data`:

```sql
TRUNCATE TABLE employee_data;
```

This removes all rows from the `employee_data` table without affecting the table's schema or structure.
55 changes: 55 additions & 0 deletions content/sql/concepts/operators/terms/update/update.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
Title: 'UPDATE'
Description: 'Modifies existing records in a table according to specified conditions.'
Subjects:
- 'Data Science'
- 'Computer Science'
Tags:
- 'Database'
- 'Queries'
- 'PostgreSQL'
- 'MySQL'
CatalogContent:
- 'learn-sql'
- 'paths/analyze-data-with-sql'
---

The **`UPDATE`** statement in SQL is used to modify existing records in a table. This powerful **Data Manipulation Language (DML)** command allows developers to update one or multiple rows simultaneously.

## Syntax

```pseudo
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
```

- `table_name`: The name of the table containing the data to be updated.
- `SET`: Specifies the columns to update and their new values.
- `condition`: An optional clause that specifies which rows to update. If omitted, all rows in the table are updated.

## Example

Let's say there's a table `Employees`:

| ID | Name | Department | Salary |
| --- | ----- | ---------- | ------ |
| 1 | John | HR | 50000 |
| 2 | Alice | IT | 60000 |
| 3 | Bob | Sales | 45000 |

To update Bob's salary in the Sales department, the following query can be used:

```sql
UPDATE Employees
SET Salary = 47000
WHERE Name = 'Bob' AND Department = 'Sales';
```

Now, the updated table will be as follows:

| ID | Name | Department | Salary |
| --- | ----- | ---------- | ------ |
| 1 | John | HR | 50000 |
| 2 | Alice | IT | 60000 |
| 3 | Bob | Sales | 47000 |

0 comments on commit 828b916

Please sign in to comment.