Skip to content

Commit

Permalink
Merge branch 'main' into optional_chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
PragatiVerma18 authored Dec 24, 2024
2 parents d3cad8d + 94f1219 commit 1035991
Show file tree
Hide file tree
Showing 44 changed files with 3,209 additions and 47 deletions.
2 changes: 1 addition & 1 deletion bin/concept-of-the-week.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
content/ruby/concepts/gems/gems.md
content/c/concepts/user-input/user-input.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
Title: 'Convolutional Neural Networks'
Description: 'Convolutional Neural Networks are a type of neural network that are primarily used for computer vision tasks, such as image classification, object detection, and semantic segmentation.'
Description: 'Convolutional Neural Networks (CNNs) are neural networks primarily used for computer vision tasks like image classification, object detection, and segmentation.'
Subjects:
- 'Machine Learning'
- 'Computer Science'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
---
Title: 'Gradient Descent'
Description: 'Gradient Descent is an optimization algorithm that minimizes a cost function by iteratively adjusting parameters in the direction of its gradient.'
Subjects:
- 'Machine Learning'
- 'Data Science'
- 'Computer Science'
Tags:
- 'AI'
- 'Machine Learning'
- 'Neural Networks'
- 'Functions'
CatalogContent:
- 'paths/data-science'
- 'paths/machine-learning'
---

**Gradient Descent** is an optimization algorithm commonly used in machine learning and neural networks to minimize a cost function. Its goal is to iteratively find the optimal parameters (weights) that minimize the error or loss.

In neural networks, gradient descent computes the gradient (derivative) of the cost function with respect to each parameter. It then updates the parameters in the direction of the negative gradient, effectively reducing the cost with each step.

## Types of Gradient Descent

There are three main types of gradient descent:

| Type | Description |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Batch Gradient Descent** | Uses the entire dataset to compute the gradient and update the weights. Typically slower but more accurate for large datasets. |
| **Stochastic Gradient Descent (SGD)** | Uses a single sample to compute the gradient and update the weights. It is faster, but the updates are noisy and can cause fluctuations in the convergence path. |
| **Mini-batch Gradient Descent** | A compromise between batch and stochastic gradient descent, using a small batch of samples to compute the gradient. It balances the speed and accuracy of the learning process. |

## Gradient Descent Update Rule

The basic update rule for gradient descent is:

```pseudo
theta = theta - learning_rate * gradient_of_cost_function
```

- `theta`: The parameter (weight) of the model that is being optimized.
- `learning_rate`: A hyperparameter that controls the step size.
- `gradient_of_cost_function`: The gradient (derivative) of the cost function with respect to the parameters, indicating the direction and magnitude of the change needed.

## Syntax

Here's a basic syntax for Gradient Descent in the context of machine learning, specifically for updating the model parameters (weights) in order to minimize the cost function:

```pseudo
# Initialize parameters (weights) and learning rate
theta = initial_value # Model Parameters (weights)
learning_rate = value # Learning rate (step size)
iterations = number_of_iterations # Number of iterations
# Repeat until convergence
for i in range(iterations):
# Calculate the gradient of the cost function
gradient = compute_gradient(X, y, theta) # Gradient calculation
# Update the parameters (weights)
theta = theta - learning_rate * gradient # Update rule
# Optionally, compute and store the cost (for monitoring convergence)
cost = compute_cost(X, y, theta)
store(cost)
```

## Example

In the following example, we implement simple gradient descent to minimize the cost function of a linear regression problem:

```py
import numpy as np

# Sample data (X: inputs, y: actual outputs)
X = np.array([1, 2, 3, 4, 5])
y = np.array([1, 2, 1.3, 3.75, 2.25])

# Parameters initialization
theta = 0.0 # Initial weight
learning_rate = 0.01 # Step size
iterations = 1000 # Number of iterations

# Cost function (Mean Squared Error)
def compute_cost(X, y, theta):
m = len(y)
cost = (1/(2*m)) * np.sum((X*theta - y)**2) # The cost function for linear regression
return cost

# Gradient Descent function
def gradient_descent(X, y, theta, learning_rate, iterations):
m = len(y)
cost_history = []

for i in range(iterations):
gradient = (1/m) * np.sum(X * (X*theta - y)) # Derivative of cost function
theta = theta - learning_rate * gradient # Update theta
cost_history.append(compute_cost(X, y, theta)) # Track cost
return theta, cost_history

# Run Gradient Descent
theta_optimal, cost_history = gradient_descent(X, y, theta, learning_rate, iterations)

print(f"Optimal Theta: {theta_optimal}")
```

The output for the above code will be something like this:

```shell
Optimal Theta: 0.6390909090909086
```

> **Note**: The optimal `theta` value will be an approximation, as the gradient descent approach iteratively updates the weight to reduce the cost function.
29 changes: 29 additions & 0 deletions content/blockchain/blockchain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
Title: 'Blockchain'
Description: 'Blockchain is a decentralized ledger that securely records transactions, ensuring transparency, trust, and immutability without a central authority.'
Codecademy Hub Page: 'https://www.codecademy.com/catalog/subject/blockchain'
CatalogContent:
- 'rust-for-programmers'
- 'paths/computer-science'
---

**Blockchain** is a decentralized and distributed digital ledger that securely records transactions across multiple nodes in a network. It ensures data integrity through cryptographic techniques and transparency by allowing participants to access an immutable, shared history of transactions. By eliminating the need for a central authority, blockchain enables trust and collaboration in various applications, from cryptocurrencies to supply chain management.

Blockchain’s origins trace back to 1991 when cryptographers Stuart Haber and W. Scott Stornetta introduced a system for timestamping digital documents. The technology gained prominence in 2008 with Satoshi Nakamoto’s creation of Bitcoin, the first decentralized cryptocurrency using blockchain as its backbone. Over time, its applications have expanded beyond cryptocurrencies to include smart contracts, supply chain management, and enterprise solutions.

Key principles of Blockchain include:

- **Decentralization**: No central authority; data is shared across nodes.
- **Cryptographic Security**: Data integrity ensured by encryption.
- **Consensus Mechanisms**: Agreement protocols like Proof of Work or Proof of Stake.

## Types of Blockchains

1. **Public Blockchains**:
Open and decentralized networks where anyone can participate, read, or write data. These blockchains prioritize transparency and security, making them ideal for cryptocurrencies (e.g., Bitcoin, Ethereum). However, they may face scalability challenges and require significant energy for consensus.

2. **Private Blockchains**:
Permissioned networks with restricted access, used by organizations to enhance efficiency and control. Only authorized participants can interact with the network, making it suitable for use cases like supply chain management or internal data sharing (e.g., Hyperledger, Corda).

3. **Consortium Blockchains**:
Blockchains managed collaboratively by a group of organizations. These hybrid systems strike a balance between decentralization and controlled access, often used in industries where shared authority is required, such as banking or healthcare (e.g., R3 Corda, Quorum).
2 changes: 1 addition & 1 deletion content/lua/concepts/strings/terms/lower/lower.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Title: 'lower()'
Title: '.lower()'
Description: 'Returns a copy of the string given, with all uppercase characters transformed to lowercase.'
Subjects:
- 'Code Foundations'
Expand Down
79 changes: 79 additions & 0 deletions content/numpy/concepts/built-in-functions/terms/sort/sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
Title: '.sort()'
Description: 'Sorts an array in ascending order along the specified axis and returns a sorted copy of the input array.'
Subjects:
- 'Computer Science'
- 'Data Science'
Tags:
- 'Arrays'
- 'Functions'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/data-science'
---

In NumPy, the **`.sort()`** function sorts the elements of an array or matrix along a specified axis. It returns a new array with elements sorted in ascending order, leaving the original array unchanged. Sorting can be performed along different axes (such as rows or columns in a 2D array), with the default being along the last axis (`axis=-1`).

## Syntax

```pseudo
numpy.sort(a, axis=-1, kind=None, order=None)
```

- `a`: The array of elements to be sorted.
- `axis`: The axis along which to sort. If set to `None`, the array is flattened before sorting. The default is `-1`, which sorts along the last axis.
- `kind`: The sorting algorithm to use. The options are:
- [`'quicksort'`](https://www.codecademy.com/resources/docs/general/algorithm/quick-sort): Default algorithm, a fast, comparison-based algorithm.
- [`'mergesort'`](https://www.codecademy.com/resources/docs/general/algorithm/merge-sort): Stable sort using a divide-and-conquer algorithm.
- [`'heapsort'`](https://www.codecademy.com/resources/docs/general/algorithm/heap-sort): A comparison-based sort using a heap.
- `'stable'`: A stable sorting algorithm, typically mergesort.
- `order`: If `a` is a structured array, this specifies the field(s) to sort by. If not provided, sorting will be done based on the order of the fields in `a`.

## Example

The following example demonstrates how to use the `.sort()` function with various parameters:

```py
import numpy as np

arr = np.array([[3, 1, 2], [6, 4, 5]])

print(np.sort(arr))
print(np.sort(arr, axis=0))
print(np.sort(arr, axis=None))
```

This example results in the following output:

```shell
[[1 2 3]
[4 5 6]]
[[3 1 2]
[6 4 5]]
[1 2 3 4 5 6]
```

## Codebyte Example

Run the following codebyte example to better understand the `.sort()` function:

```codebyte/python
import numpy as np
arr = np.array([[23, 54, 19], [45, 34, 12]])
print("Original array:")
print(arr)
# Sort along axis 0 (sort by columns)
print("\nSorted array along axis 0 (columns):")
print(np.sort(arr, axis=0))
# Sort along axis 1 (sort by rows)
print("\nSorted array along axis 1 (rows):")
print(np.sort(arr, axis=1))
print("\nSorted array (flattened):")
print(np.sort(arr, axis=None))
```
65 changes: 65 additions & 0 deletions content/numpy/concepts/math-methods/terms/deg2rad/deg2rad.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
Title: '.deg2rad()'
Description: 'Converts angles from degrees to radians.'
Subjects:
- 'Computer Science'
- 'Data Science'
- 'Web Development'
Tags:
- 'Math'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

In NumPy, the **`.deg2rad()`** function converts an angle from degrees to radians.

> **Note:** In NumPy, the default unit for angles is radians. Therefore, the `.deg2rad()` function is used to convert angle values from degrees to radians.
## Syntax

```pseudo
numpy.deg2rad(x, out=None)
```

- `x`: The input array (or scalar) containing angles in degrees that need to be converted to radians.
- `out` (Optional): A location where the result is stored. If not specified, a new array is returned.

## Example

In this example, the code converts an angle measured in degrees to radians using the `numpy.deg2rad()` function:

```py
import numpy as np

# Angle of the board on a table measured in degrees
angle_degrees = 45

# Convert the angle to radians
angle_radians = np.deg2rad(angle_degrees)

# Output the result
print(f"Angle in degrees: {angle_degrees}")
print(f"Angle in radians: {angle_radians}")
```

The code above produces the following output:

```shell
Angle in degrees: 45
Angle in radians: 0.7853981633974483
```

## Codebyte Example

Run the codebyte example below to understand how the `.deg2rad()` function works:

```codebyte/python
import numpy as np
degrees = 170
radians = np.deg2rad(degrees)
print(f"{degrees} degrees is {radians} radians.")
```
Loading

0 comments on commit 1035991

Please sign in to comment.