Skip to content

Commit 47064b0

Browse files
authored
Merge branch 'main' into add-cpp-end-term
2 parents 2ccb031 + 23d0583 commit 47064b0

File tree

3 files changed

+275
-0
lines changed

3 files changed

+275
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
Title: 'size()'
3+
Description: 'Returns a Series containing the size (row count) of each group.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
Tags:
8+
- 'Data Structures'
9+
- 'Pandas'
10+
CatalogContent:
11+
- 'learn-python-3'
12+
- 'paths/data-science'
13+
---
14+
15+
The **`size()`** method in pandas returns the number of rows or elements in each group created by the `groupby()` [function](https://www.codecademy.com/resources/docs/pandas/built-in-functions). It provides a quick way to determine group sizes without applying an aggregation function.
16+
17+
## Syntax
18+
19+
```pseudo
20+
DataFrameGroupBy.size()
21+
```
22+
23+
**Parameters:**
24+
25+
The `size()` method doesn't take any parameters.
26+
27+
**Return value:**
28+
29+
The `size()` method returns a Series containing the size (row count) of each group created by `groupby()`.
30+
31+
## Example 1: Counting Rows by Group
32+
33+
In this example, a [DataFrame](https://www.codecademy.com/resources/docs/pandas/dataframe) of employees is grouped by their department, and `size()` counts how many employees belong to each department:
34+
35+
```py
36+
import pandas as pd
37+
38+
data = {
39+
'Department': ['HR', 'IT', 'HR', 'Finance', 'IT', 'Finance'],
40+
'Employee': ['John', 'Sara', 'Mike', 'Anna', 'Tom', 'Chris']
41+
}
42+
df = pd.DataFrame(data)
43+
44+
group_sizes = df.groupby('Department').size()
45+
print(group_sizes)
46+
```
47+
48+
The output of this code is:
49+
50+
```shell
51+
Department
52+
Finance 2
53+
HR 2
54+
IT 2
55+
dtype: int64
56+
```
57+
58+
## Example 2: Using Multiple Grouping Columns
59+
60+
In this example, `size()` counts the number of members in each combination of team and shift within a dataset:
61+
62+
```py
63+
import pandas as pd
64+
65+
data = {
66+
'Team': ['A', 'A', 'B', 'B', 'B', 'C'],
67+
'Shift': ['Day', 'Night', 'Day', 'Night', 'Day', 'Day'],
68+
'Name': ['John', 'Sara', 'Mike', 'Anna', 'Tom', 'Chris']
69+
}
70+
df = pd.DataFrame(data)
71+
72+
group_sizes = df.groupby(['Team', 'Shift']).size()
73+
print(group_sizes)
74+
```
75+
76+
The output of this code is:
77+
78+
```shell
79+
Team Shift
80+
A Day 1
81+
Night 1
82+
B Day 2
83+
Night 1
84+
C Day 1
85+
dtype: int64
86+
```
87+
88+
## Codebyte Example: Counting Transactions Per Product
89+
90+
In this example, `size()` is used to count how many sales transactions occurred for each product in a store dataset:
91+
92+
```codebyte/python
93+
import pandas as pd
94+
95+
sales = pd.DataFrame({
96+
'Product': ['Apple', 'Banana', 'Apple', 'Orange', 'Banana', 'Banana', 'Apple'],
97+
'Customer': ['A', 'B', 'C', 'A', 'D', 'E', 'F']
98+
})
99+
100+
counts = sales.groupby('Product').size()
101+
print(counts)
102+
```
103+
104+
## Frequently Asked Questions
105+
106+
### 1. What is the pandas `groupby().size()` method?
107+
108+
`groupby().size()` returns the number of rows in each group created by `groupby()`.
109+
110+
### 2. What is the purpose of `groupby()` in pandas?
111+
112+
`groupby()` splits data into groups based on selected column values to enable aggregation and summarization.
113+
114+
### 3. What does `NaN` stand for in pandas?
115+
116+
`NaN` stands for Not a Number and indicates missing or undefined data.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
Title: '.log2()'
3+
Description: 'Computes the base-2 logarithm of each element in the input tensor and returns a new tensor with the results.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Elements'
9+
- 'Methods'
10+
- 'PyTorch'
11+
- 'Tensors'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.log2()`** method in PyTorch returns a new [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors) by computing the logarithm base 2 of each element in the input tensor. This operation is useful in numerous data-science and machine-learning workflows where values are interpreted on a log scale (e.g., information theory, binary magnitude comparisons).
18+
19+
## Syntax
20+
21+
```pseudo
22+
torch.log2(input, *, out=None) → Tensor
23+
```
24+
25+
**Parameters:**
26+
27+
- `input` (Tensor): The tensor whose elements are to be transformed by base-2 logarithm.
28+
- `out` (Tensor, optional): A tensor to store the output; must have the same shape as input if provided.
29+
30+
**Return value:**
31+
32+
Returns a new tensor of the same shape as `input` where each element is `log₂(input[i])`.
33+
34+
## Example 1: Basic Usage of `.log2()`
35+
36+
In this example, the base-2 logarithm is computed for a tensor containing powers of 2:
37+
38+
```py
39+
import torch
40+
41+
# Define a tensor
42+
input_tensor = torch.tensor([2.0, 4.0, 8.0, 16.0, 32.0])
43+
44+
# Compute base-2 logarithm
45+
output_tensor = torch.log2(input_tensor)
46+
47+
print(output_tensor)
48+
```
49+
50+
The output of this code is:
51+
52+
```shell
53+
tensor([1., 2., 3., 4., 5.])
54+
```
55+
56+
## Example 2: Applying `.log2()` on Random Values
57+
58+
In this example, a tensor with random positive values is transformed using base-2 logarithm to analyze data on a log scale:
59+
60+
```py
61+
import torch
62+
63+
# Generate a tensor of random positive values
64+
data = torch.rand(5) * 10 + 1
65+
66+
# Apply log2 transformation
67+
log_tensor = torch.log2(data)
68+
69+
print(data)
70+
print(log_tensor)
71+
```
72+
73+
The output of this code is:
74+
75+
```shell
76+
tensor([10.5500, 9.2777, 10.9371, 1.3551, 5.2609])
77+
tensor([3.3992, 3.2138, 3.4512, 0.4384, 2.3953])
78+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
Title: '.logaddexp()'
3+
Description: 'Computes the element-wise logarithm of the sum of exponentials of two input tensors.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Elements'
9+
- 'Methods'
10+
- 'PyTorch'
11+
- 'Tensors'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/data-science'
15+
---
16+
17+
The **`.logaddexp()`** function in PyTorch computes the element-wise logarithm of the sum of exponentials of two input [tensors](https://www.codecademy.com/resources/docs/pytorch/tensors). If the tensors are `x` and `y`, the mathematical formula will be:
18+
19+
$$\log(\exp(x) + \exp(y))$$
20+
21+
This operation is particularly useful for combining log-space values (such as log-probabilities) in a numerically stable way.
22+
23+
## Syntax
24+
25+
```pseudo
26+
torch.logaddexp(input, other, *, out=None) → Tensor
27+
```
28+
29+
**Parameters:**
30+
31+
- `input` (Tensor): The first input tensor.
32+
- `other` (Tensor): The second input tensor, broadcastable to the shape of `input`.
33+
- `out` (Tensor, optional): A tensor to store the output; must have the same shape as the broadcasted result if provided.
34+
35+
**Return value:**
36+
37+
Returns a new tensor of the same shape as the broadcasted `input` and `other`, where each element is:
38+
39+
$$\log\left(\exp(\text{input}[i]) + \exp(\text{other}[i])\right)$$
40+
41+
## Example 1: Combining Log-Probabilities
42+
43+
In this example, two tensors representing log-probabilities are combined using `.logaddexp()`:
44+
45+
```py
46+
import torch
47+
48+
x = torch.tensor([ -0.5, -1.2, -3.0 ])
49+
y = torch.tensor([ -0.2, -0.8, -4.5 ])
50+
51+
result = torch.logaddexp(x, y)
52+
print(result)
53+
```
54+
55+
The output of this code is:
56+
57+
```shell
58+
tensor([ 0.3544, -0.2870, -2.7986])
59+
```
60+
61+
## Example 2: Broadcasting Two Tensors of Different Shapes
62+
63+
In this example, a tensor and a scalar are combined with broadcasting using `.logaddexp()`:
64+
65+
```py
66+
import torch
67+
68+
x = torch.tensor([[ 1.0, 2.0 ,3.0],
69+
[ 4.0, 5.0 ,6.0]])
70+
y = torch.tensor( 2.0 )
71+
72+
result = torch.logaddexp(x, y)
73+
print(result)
74+
```
75+
76+
The output of this code is:
77+
78+
```shell
79+
tensor([[2.3133, 2.6931, 3.3133],
80+
[4.1269, 5.0486, 6.0181]])
81+
```

0 commit comments

Comments
 (0)