Skip to content

Commit ab32390

Browse files
Merge branch 'main' into feature/add-polyfill-doc
2 parents 9856fb0 + f6e3b48 commit ab32390

File tree

4 files changed

+284
-0
lines changed

4 files changed

+284
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
Title: 'Gradient Descent'
3+
Description: 'Gradient Descent is an optimization algorithm that minimizes a cost function by iteratively adjusting parameters in the direction of its gradient.'
4+
Subjects:
5+
- 'Machine Learning'
6+
- 'Data Science'
7+
- 'Computer Science'
8+
Tags:
9+
- 'AI'
10+
- 'Machine Learning'
11+
- 'Neural Networks'
12+
- 'Functions'
13+
CatalogContent:
14+
- 'paths/data-science'
15+
- 'paths/machine-learning'
16+
---
17+
18+
**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.
19+
20+
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.
21+
22+
## Types of Gradient Descent
23+
24+
There are three main types of gradient descent:
25+
26+
| Type | Description |
27+
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28+
| **Batch Gradient Descent** | Uses the entire dataset to compute the gradient and update the weights. Typically slower but more accurate for large datasets. |
29+
| **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. |
30+
| **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. |
31+
32+
## Gradient Descent Update Rule
33+
34+
The basic update rule for gradient descent is:
35+
36+
```pseudo
37+
theta = theta - learning_rate * gradient_of_cost_function
38+
```
39+
40+
- `theta`: The parameter (weight) of the model that is being optimized.
41+
- `learning_rate`: A hyperparameter that controls the step size.
42+
- `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.
43+
44+
## Syntax
45+
46+
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:
47+
48+
```pseudo
49+
# Initialize parameters (weights) and learning rate
50+
theta = initial_value # Model Parameters (weights)
51+
learning_rate = value # Learning rate (step size)
52+
iterations = number_of_iterations # Number of iterations
53+
54+
# Repeat until convergence
55+
for i in range(iterations):
56+
# Calculate the gradient of the cost function
57+
gradient = compute_gradient(X, y, theta) # Gradient calculation
58+
59+
# Update the parameters (weights)
60+
theta = theta - learning_rate * gradient # Update rule
61+
62+
# Optionally, compute and store the cost (for monitoring convergence)
63+
cost = compute_cost(X, y, theta)
64+
store(cost)
65+
```
66+
67+
## Example
68+
69+
In the following example, we implement simple gradient descent to minimize the cost function of a linear regression problem:
70+
71+
```py
72+
import numpy as np
73+
74+
# Sample data (X: inputs, y: actual outputs)
75+
X = np.array([1, 2, 3, 4, 5])
76+
y = np.array([1, 2, 1.3, 3.75, 2.25])
77+
78+
# Parameters initialization
79+
theta = 0.0 # Initial weight
80+
learning_rate = 0.01 # Step size
81+
iterations = 1000 # Number of iterations
82+
83+
# Cost function (Mean Squared Error)
84+
def compute_cost(X, y, theta):
85+
m = len(y)
86+
cost = (1/(2*m)) * np.sum((X*theta - y)**2) # The cost function for linear regression
87+
return cost
88+
89+
# Gradient Descent function
90+
def gradient_descent(X, y, theta, learning_rate, iterations):
91+
m = len(y)
92+
cost_history = []
93+
94+
for i in range(iterations):
95+
gradient = (1/m) * np.sum(X * (X*theta - y)) # Derivative of cost function
96+
theta = theta - learning_rate * gradient # Update theta
97+
cost_history.append(compute_cost(X, y, theta)) # Track cost
98+
return theta, cost_history
99+
100+
# Run Gradient Descent
101+
theta_optimal, cost_history = gradient_descent(X, y, theta, learning_rate, iterations)
102+
103+
print(f"Optimal Theta: {theta_optimal}")
104+
```
105+
106+
The output for the above code will be something like this:
107+
108+
```shell
109+
Optimal Theta: 0.6390909090909086
110+
```
111+
112+
> **Note**: The optimal `theta` value will be an approximation, as the gradient descent approach iteratively updates the weight to reduce the cost function.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
Title: '.Histogram2dContour()'
3+
Description: 'Creates 2D histograms with contours for visualizing density distributions in data.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Data Visualization'
7+
Tags:
8+
- 'Data'
9+
- 'Data Structures'
10+
- 'Plotly'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/data-science'
14+
---
15+
16+
The **`.Histogram2dContour()`** method in Plotly's `graph_objects` module creates a 2D histogram with contour lines to visualize the joint distribution of two variables. It uses a grid where color intensity represents the count or aggregated values within each cell, while the contour lines indicate regions of equal density. This method helps visualize relationships and density in bivariate data, helping to uncover patterns and trends.
17+
18+
## Syntax
19+
20+
```pseudo
21+
plotly.graph_objects.Histogram2dContour(x=None, y=None, nbinsx=None, nbinsy=None, colorscale=None, contours=None, ...)
22+
```
23+
24+
- `x`: Input data for the x-axis.
25+
- `y`: Input data for the y-axis.
26+
- `nbinsx` (Optional): The number of bins (intervals) used to divide the x-axis range. If not specified (`None`), Plotly automatically calculates an appropriate number of bins based on the data.
27+
- `nbinsy` (Optional): The number of bins (intervals) used to divide the y-axis range on the data.
28+
- `colorscale` (Optional): Defines the color scale for heatmap.
29+
- `contours` (Optional): Configuration for contour lines (e.g., `levels`, `start`, `end`, `size`).
30+
31+
> **Note**: To personalize the scatter plot on polar axes, there are more possible options than those mentioned above, as indicated by the ellipsis in the syntax (...).
32+
33+
## Example
34+
35+
The following example showcases the use of the `.Histogram2dContour()`:
36+
37+
```py
38+
import plotly.graph_objects as go
39+
40+
# Sample data
41+
x = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
42+
y = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
43+
44+
# Create the histogram with contours
45+
fig = go.Figure(
46+
go.Histogram2dContour(
47+
x=x,
48+
y=y,
49+
nbinsx=5,
50+
nbinsy=5,
51+
colorscale='Viridis',
52+
contours=dict(start=0, end=4, size=1)
53+
)
54+
)
55+
56+
# Show the figure
57+
fig.show()
58+
```
59+
60+
The example demonstrates how to use `.Histogram2dContour()` to create a two-dimensional histogram that includes contour lines which are used to visualize the joint distribution between two variables.
61+
62+
The above code generates the following output:
63+
64+
![Histogram2dContour in Plotly](https://raw.githubusercontent.com/Codecademy/docs/main/media/histogram2dcontour-example.png)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
Title: 'pyodbc'
3+
Description: 'pyodbc is a library in Python that provides a bridge between Python applications and ODBC-compliant databases, allowing efficient database operations.'
4+
Subjects:
5+
- 'Data Science'
6+
- 'Web Development'
7+
- 'Developer Tools'
8+
Tags:
9+
- 'Database'
10+
- 'SQL'
11+
- 'Python'
12+
CatalogContent:
13+
- 'learn-python-3'
14+
- 'paths/data-science'
15+
---
16+
17+
**`pyodbc`** is a Python library that enables Python programs to interact with databases through **ODBC (Open Database Connectivity)**, a standard API for accessing database management systems (DBMS). It provides a powerful and efficient way to execute SQL queries, retrieve results, and perform other database operations.
18+
19+
## Installation
20+
21+
To install `pyodbc`, `pip` can be used:
22+
23+
```bash
24+
pip install pyodbc
25+
```
26+
27+
## Syntax
28+
29+
A basic connection to an ODBC database and query execution with `pyodbc` follows this structure:
30+
31+
```pseudo
32+
import pyodbc
33+
34+
# Connect to the database
35+
connection = pyodbc.connect("Driver={Driver_Name};"
36+
"Server=server_name;"
37+
"Database=database_name;"
38+
"UID=user_id;"
39+
"PWD=password;")
40+
41+
# Create a cursor object
42+
cursor = connection.cursor()
43+
44+
# Execute a query
45+
cursor.execute("SQL QUERY")
46+
47+
# Fetch results
48+
rows = cursor.fetchall()
49+
50+
# Process results
51+
for row in rows:
52+
print(row)
53+
54+
# Close the connection
55+
connection.close()
56+
```
57+
58+
## Key Parameters
59+
60+
- `Driver`: Specifies the ODBC driver to use for the connection.
61+
- `Server`: The database server's address or name.
62+
- `Database`: The name of the database to connect to.
63+
- `UID` and `PWD`: The username and password for authentication. They are case-sensitive in most databases.
64+
65+
> **Note**: Connection string formats depend on the database type. Refer to [connectionstrings.com](https://www.connectionstrings.com/) for specific examples.
66+
67+
## Example
68+
69+
The following example demonstrates connecting to a Microsoft SQL Server, querying a table, and printing the results:
70+
71+
```py
72+
import pyodbc
73+
74+
# Define connection string
75+
connection_string = ("Driver={ODBC Driver 17 for SQL Server};"
76+
"Server=localhost;"
77+
"Database=TestDB;"
78+
"UID=sa;"
79+
"PWD=your_password;")
80+
81+
try:
82+
# Establish connection
83+
conn = pyodbc.connect(connection_string)
84+
cursor = conn.cursor()
85+
86+
# Execute a SQL query
87+
cursor.execute("SELECT * FROM Employees")
88+
89+
# Fetch and print results
90+
for row in cursor:
91+
print(row)
92+
93+
except pyodbc.Error as ex:
94+
print("An error occurred:", ex)
95+
96+
finally:
97+
# Close the connection
98+
if 'conn' in locals():
99+
conn.close()
100+
```
101+
102+
## Use Cases
103+
104+
Here are some use cases for `pyodbc`:
105+
106+
- Connecting to a variety of databases (e.g., SQL Server, MySQL, PostgreSQL) via ODBC
107+
- Executing dynamic SQL queries
108+
- Efficiently handling large datasets
29.5 KB
Loading

0 commit comments

Comments
 (0)