-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,531 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
content/uiux/concepts/design-software/design-software.md | ||
content/ruby/concepts/gems/gems.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
content/ai/concepts/neural-networks/terms/gradient-descent/gradient-descent.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
118 changes: 118 additions & 0 deletions
118
content/c-sharp/concepts/strings/terms/padright/padright.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
--- | ||
Title: '.PadRight()' | ||
Description: 'Adds padding to the right side of a string to ensure it reaches a specified total length.' | ||
Subjects: | ||
- 'Code Foundations' | ||
- 'Computer Science' | ||
Tags: | ||
- 'Strings' | ||
- 'Characters' | ||
- 'Methods' | ||
- 'Functions' | ||
CatalogContent: | ||
- 'learn-c-sharp' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`.PadRight()`** method in C# is used to add padding characters to the right of a string to achieve a specified total length. By default, it uses spaces as the padding character, but a custom character can also be specified. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
string.PadRight(int totalWidth, char paddingChar) | ||
``` | ||
|
||
- `totalWidth`: The desired total length of the string, including padding. If the specified width is less than the string's length, no padding is added. | ||
- `paddingChar`(Optional): The character to use for padding. Defaults to a space character (`' '`). | ||
|
||
> **Note**: The `.PadRight()` method does not modify the original string. It generates and returns a new string with padding applied to achieve the specified total width. | ||
## Example | ||
|
||
### Default Padding with Spaces | ||
|
||
```cs | ||
using System; | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string name = "John"; | ||
string paddedName = name.PadRight(10); | ||
Console.WriteLine($"'{paddedName}'"); | ||
} | ||
} | ||
``` | ||
|
||
The above code generates the output as follows: | ||
|
||
```shell | ||
'John ' | ||
``` | ||
|
||
### Custom Padding Character | ||
|
||
```cs | ||
using System; | ||
|
||
class NameFormatter | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
string name = "John"; | ||
string paddedName = name.PadRight(10, '-'); | ||
Console.WriteLine($"'{paddedName}'"); | ||
} | ||
} | ||
``` | ||
|
||
The output of the above code will be: | ||
|
||
```shell | ||
'John------' | ||
``` | ||
|
||
### Handling Shorter Total Width | ||
|
||
If the specified `totalWidth` is less than the length of the string, the original string is returned: | ||
|
||
```cs | ||
using System; | ||
|
||
class NamePaddingExample | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
string name = "John"; | ||
string result = name.PadRight(3); | ||
Console.WriteLine($"'{result}'"); | ||
} | ||
} | ||
``` | ||
|
||
Here's what the output of this code will be: | ||
|
||
```shell | ||
'John' | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
Run the following example to understand how the `.PadRight()` method works: | ||
|
||
```codebyte/csharp | ||
using System; | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string word = "Align"; | ||
// Pad with spaces to a total width of 15 | ||
Console.WriteLine("'" + word.PadRight(15) + "'"); | ||
// Pad with asterisks to a total width of 15 | ||
Console.WriteLine("'" + word.PadRight(15, '*') + "'"); | ||
} | ||
} | ||
``` |
129 changes: 129 additions & 0 deletions
129
content/c-sharp/concepts/strings/terms/tochararray/tochararray.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
--- | ||
Title: '.ToCharArray()' | ||
Description: 'Converts a string into an array of characters.' | ||
Subjects: | ||
- 'Computer Science' | ||
- 'Code Foundations' | ||
Tags: | ||
- 'Strings' | ||
- 'Methods' | ||
CatalogContent: | ||
- 'learn-c-sharp' | ||
- 'paths/computer-science' | ||
--- | ||
|
||
The **`.ToCharArray()`** method in C# converts a string into an array of characters, where each character in the string becomes an element in the resulting array. This allows for the manipulation or iteration of individual characters. | ||
|
||
## Syntax | ||
|
||
```pseudo | ||
char[] arrayName = stringName.ToCharArray(); | ||
``` | ||
|
||
- `stringName`: The string to be converted into a character array. | ||
- `arrayName`: The resulting array containing individual characters from the string. | ||
|
||
### Optional Parameters | ||
|
||
The `.ToCharArray()` method can also take optional parameters to convert a specific substring into a character array: | ||
|
||
```pseudo | ||
char[] substringArray = stringName.ToCharArray(startIndex, length); | ||
``` | ||
|
||
- `startIndex`: The zero-based index where the substring starts. | ||
- `length`: The number of characters to include in the resulting array, starting from `startIndex`. | ||
|
||
## Examples | ||
|
||
### Split into Individual Characters | ||
|
||
Here's an example of using `.ToCharArray()` to split a string into individual characters: | ||
|
||
```cs | ||
using System; | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string greeting = "Hello, World!"; | ||
char[] charArray = greeting.ToCharArray(); | ||
|
||
foreach (char c in charArray) | ||
{ | ||
Console.WriteLine(c); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The output of the above code will be as follows: | ||
|
||
```shell | ||
H | ||
e | ||
l | ||
l | ||
o | ||
, | ||
|
||
W | ||
o | ||
r | ||
l | ||
d | ||
! | ||
``` | ||
|
||
### Converting a Substring to a Character Array | ||
|
||
```cs | ||
using System; | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string phrase = "Convert this string!"; | ||
char[] partialArray = phrase.ToCharArray(8, 4); | ||
|
||
foreach (char c in partialArray) | ||
{ | ||
Console.WriteLine(c); | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The output of the above code will be as follows: | ||
|
||
```shell | ||
t | ||
h | ||
i | ||
s | ||
``` | ||
|
||
## Codebyte Example | ||
|
||
Run the following codebyte example to see how `.ToCharArray()` works in C#: | ||
|
||
```codebyte/csharp | ||
using System; | ||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
string example = "Learn C#"; | ||
char[] charArray = example.ToCharArray(); | ||
Console.WriteLine("Character Array:"); | ||
foreach (char c in charArray) | ||
{ | ||
Console.Write(c + " "); | ||
} | ||
} | ||
} | ||
``` |
File renamed without changes.
Oops, something went wrong.