Skip to content

Commit 03f41d7

Browse files
authored
Merge branch 'CodeHarborHub:main' into main
2 parents 0471ed0 + 03fac0c commit 03f41d7

File tree

18 files changed

+1813
-4
lines changed

18 files changed

+1813
-4
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Logistic Regression
2+
3+
4+
``` python
5+
from sklearn.datasets import make_classification
6+
7+
X, y = make_classification(n_samples=100, n_features=2, n_informative=2, n_redundant=0, n_clusters_per_class=1, random_state=42)
8+
```
9+
Above, is the custom dataset made using `make_classification` from
10+
`sklearn.datasets` .
11+
12+
``` python
13+
import matplotlib.pyplot as plt
14+
plt.scatter(X[:,0],X[:,1])
15+
plt.show()
16+
```
17+
![55558f59d1b98e9a3cc68d08daae54b9b065d057](https://github.com/AmrutaJayanti/codeharborhub/assets/142327526/84578011-0887-43da-b972-9e6f04ae505e)
18+
19+
20+
21+
Logistic Regression is a statistical method used for binary
22+
classification problems. It models the probability that a given input
23+
belongs to a particular category.
24+
25+
Logistic Function (Sigmoid Function): The core of logistic regression is
26+
the logistic function, which is an S-shaped curve that can take any
27+
real-valued number and map it into a value between 0 and 1. The function
28+
is defined as:
29+
30+
$$\sigma(x) = \frac{1}{1 + e^{-x}}$$
31+
32+
where $x$ is the input to the function
33+
34+
Logistic Regression is generally used for linearly separated data.
35+
36+
Logistic Regression cost function :
37+
38+
$J(\beta) = - \frac{1}{m} \sum_{i=1}^{m} \left[ y_i \log(h_\beta(x_i)) + (1 - y_i) \log(1 - h_\beta(x_i)) \right]$
39+
40+
### Applications
41+
42+
- **Medical Diagnosis**: Predicting whether a patient has a certain
43+
disease (e.g., diabetes, cancer) based on diagnostic features.
44+
- **Spam Detection**: Classifying emails as spam or not spam.
45+
- **Customer Churn**: Predicting whether a customer will leave a
46+
service.
47+
- **Credit Scoring**: Assessing whether a loan applicant is likely to
48+
default on a loan.
49+
50+
51+
``` python
52+
from sklearn.model_selection import train_test_split
53+
x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42)
54+
```
55+
56+
`X`,`y` are split into training and testing data using `train_test_split`
57+
58+
``` python
59+
from sklearn.linear_model import LogisticRegression
60+
61+
model = LogisticRegression()
62+
model.fit(x_train,y_train)
63+
y_pred = model.predict(x_test)
64+
65+
from sklearn.metrics import accuracy_score
66+
accuracy_score(y_test,y_pred)
67+
68+
```
69+
Output:
70+
71+
1.0
72+
73+
Our model predicts data accurately. Hence the accuracy score is 1 .
74+
75+
``` python
76+
import numpy as np
77+
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
78+
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
79+
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.01),
80+
np.arange(y_min, y_max, 0.01))
81+
82+
# Predict the class for each grid point
83+
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
84+
Z = Z.reshape(xx.shape)
85+
86+
# Plot decision boundary and data points
87+
plt.figure(figsize=(8, 6))
88+
plt.contourf(xx, yy, Z, alpha=0.8, cmap='viridis')
89+
plt.scatter(X[:, 0], X[:, 1], c=y, cmap='viridis', marker='o', edgecolors='k')
90+
plt.xlabel('Feature 1')
91+
plt.ylabel('Feature 2')
92+
plt.title('Logistic Regression Decision Boundary')
93+
plt.show()
94+
```
95+
96+
![3709358d7ef950353a7f26d9dfbb2f5f16fc962e](https://github.com/AmrutaJayanti/codeharborhub/assets/142327526/bd7361ac-b710-4975-8fb2-1ad4bf0ebe99)
97+
98+
99+
100+

docs/dsa/Queue/_category_.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"label": "Queue",
3+
"position": 11,
4+
"link": {
5+
"type": "generated-index",
6+
"description": "A queue is a data structure that follows the First-In-First-Out (FIFO) principle. It is similar to a real-life queue, where the first person to join the queue is the first one to be served. In programming, a queue allows you to add elements to the end and remove elements from the front. This makes it useful for implementing algorithms that require a FIFO ordering, such as breadth-first search. Queues are commonly used in operating systems, networking, and other areas of computer science."
7+
}
8+
}

docs/dsa/Queue/image.png

27 KB
Loading

docs/dsa/Queue/queue.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
---
2+
id: queue-in-dsa
3+
title: Queue Data Structure
4+
sidebar_label: Queue
5+
sidebar_position: 1
6+
description: "A queue is a linear data structure that follows the First In First Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed. Queues are used in various applications such as process scheduling, breadth-first search, and more."
7+
tags: [dsa, data-structures, Queue]
8+
---
9+
10+
### Introduction to Queue
11+
12+
A queue is a linear data structure that follows the First In First Out (FIFO) principle. This means that the first element added to the queue will be the first one to be removed. Queues are used in various applications such as process scheduling, breadth-first search, and more.
13+
14+
![alt text](image.png)
15+
16+
### Queue Operations
17+
18+
1. **Enqueue**: Add an element to the end of the queue.
19+
2. **Dequeue**: Remove the element from the front of the queue.
20+
3. **Peek**: Retrieve the element from the front of the queue without removing it.
21+
4. **isEmpty**: Check if the queue is empty.
22+
5. **Size**: Get the number of elements in the queue.
23+
24+
### Pseudocode
25+
26+
#### Basic Operations
27+
28+
1. **Enqueue**:
29+
30+
```text
31+
function enqueue(queue, element):
32+
queue.append(element)
33+
```
34+
35+
2. **Dequeue**:
36+
37+
```text
38+
function dequeue(queue):
39+
if isEmpty(queue):
40+
return "Queue Underflow"
41+
return queue.pop(0)
42+
```
43+
44+
3. **Peek**:
45+
46+
```text
47+
function peek(queue):
48+
if isEmpty(queue):
49+
return "Queue is empty"
50+
return queue[0]
51+
```
52+
53+
4. **isEmpty**:
54+
55+
```text
56+
function isEmpty(queue):
57+
return len(queue) == 0
58+
```
59+
60+
5. **Size**:
61+
```text
62+
function size(queue):
63+
return len(queue)
64+
```
65+
66+
### Implementation in Python, C++, and Java
67+
68+
#### Python Implementation
69+
70+
```python
71+
class Queue:
72+
def __init__(self):
73+
self.elements = []
74+
75+
def enqueue(self, element):
76+
self.elements.append(element)
77+
78+
def dequeue(self):
79+
if self.is_empty():
80+
return "Queue Underflow"
81+
return self.elements.pop(0)
82+
83+
def peek(self):
84+
if self.is_empty():
85+
return "Queue is empty"
86+
return self.elements[0]
87+
88+
def is_empty(self):
89+
return len(self.elements) == 0
90+
91+
def size(self):
92+
return len(self.elements)
93+
94+
# Example usage
95+
queue = Queue()
96+
queue.enqueue(10)
97+
queue.enqueue(20)
98+
print(queue.dequeue()) # Output: 10
99+
print(queue.peek()) # Output: 20
100+
print(queue.is_empty()) # Output: False
101+
print(queue.size()) # Output: 1
102+
```
103+
104+
#### C++ Implementation
105+
106+
```cpp
107+
#include <iostream>
108+
#include <vector>
109+
110+
class Queue {
111+
private:
112+
std::vector<int> elements;
113+
114+
public:
115+
void enqueue(int element) {
116+
elements.push_back(element);
117+
}
118+
119+
int dequeue() {
120+
if (is_empty()) {
121+
std::cerr << "Queue Underflow" << std::endl;
122+
return -1;
123+
}
124+
int front = elements.front();
125+
elements.erase(elements.begin());
126+
return front;
127+
}
128+
129+
int peek() {
130+
if (is_empty()) {
131+
std::cerr << "Queue is empty" << std::endl;
132+
return -1;
133+
}
134+
return elements.front();
135+
}
136+
137+
bool is_empty() {
138+
return elements.empty();
139+
}
140+
141+
int size() {
142+
return elements.size();
143+
}
144+
};
145+
146+
// Example usage
147+
int main() {
148+
Queue queue;
149+
queue.enqueue(10);
150+
queue.enqueue(20);
151+
std::cout << queue.dequeue() << std::endl; // Output: 10
152+
std::cout << queue.peek() << std::endl; // Output: 20
153+
std::cout << std::boolalpha << queue.is_empty() << std::endl; // Output: false
154+
std::cout << queue.size() << std::endl; // Output: 1
155+
return 0;
156+
}
157+
```
158+
159+
#### Java Implementation
160+
161+
```java
162+
import java.util.ArrayList;
163+
164+
public class Queue {
165+
private ArrayList<Integer> elements;
166+
167+
public Queue() {
168+
elements = new ArrayList<>();
169+
}
170+
171+
public void enqueue(int element) {
172+
elements.add(element);
173+
}
174+
175+
public int dequeue() {
176+
if (is_empty()) {
177+
System.out.println("Queue Underflow");
178+
return -1;
179+
}
180+
return elements.remove(0);
181+
}
182+
183+
public int peek() {
184+
if (is_empty()) {
185+
System.out.println("Queue is empty");
186+
return -1;
187+
}
188+
return elements.get(0);
189+
}
190+
191+
public boolean is_empty() {
192+
return elements.isEmpty();
193+
}
194+
195+
public int size() {
196+
return elements.size();
197+
}
198+
199+
// Example usage
200+
public static void main(String[] args) {
201+
Queue queue = new Queue();
202+
queue.enqueue(10);
203+
queue.enqueue(20);
204+
System.out.println(queue.dequeue()); // Output: 10
205+
System.out.println(queue.peek()); // Output: 20
206+
System.out.println(queue.is_empty()); // Output: false
207+
System.out.println(queue.size()); // Output: 1
208+
}
209+
}
210+
```
211+
212+
### Complexity
213+
214+
- **Time Complexity**:
215+
216+
- Enqueue: $O(1)$
217+
- Dequeue: $O(1)$
218+
- Peek: $O(1)$
219+
- isEmpty: $O(1)$
220+
- Size: $O(1)$
221+
222+
- **Space Complexity**: $O(n)$, where $n$ is the number of elements in the queue.
223+
224+
### Example
225+
226+
Consider a queue with the following operations:
227+
228+
1. Enqueue 10
229+
2. Enqueue 20
230+
3. Dequeue
231+
4. Peek
232+
5. Check if empty
233+
6. Get size
234+
235+
**Operations**:
236+
237+
- Enqueue 10: Queue becomes [10]
238+
- Enqueue 20: Queue becomes [10, 20]
239+
- Dequeue: Removes 10, Queue becomes [20]
240+
- Peek: Returns 20, Queue remains [20]
241+
- isEmpty: Returns false
242+
- Size: Returns 1
243+
244+
### Conclusion
245+
246+
A queue is a fundamental data structure used in computer science for various applications where the FIFO (First In First Out) principle is required. It is simple to implement and provides efficient operations for adding and removing elements. Understanding and using queues effectively can help solve many algorithmic problems.
247+

docs/dsa/stack/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"label": "Stack",
3-
"position": 9,
3+
"position": 12,
44
"link": {
55
"type": "generated-index",
66
"description": "Stack is a linear data structure that follows LIFO principle"

dsa-problems/leetcode-problems/0500-0599.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export const problems =[
218218
"problemName": "537. Complex Number Multiplication",
219219
"difficulty": "Medium",
220220
"leetCodeLink": "https://leetcode.com/problems/complex-number-multiplication",
221-
"solutionLink": "#"
221+
"solutionLink": "/dsa-solutions/lc-solutions/0500-0599/complex-number-multiplication"
222222
},
223223
{
224224
"problemName": "538. Convert BST to Greater Tree",
@@ -242,7 +242,7 @@ export const problems =[
242242
"problemName": "541. Reverse String II",
243243
"difficulty": "Easy",
244244
"leetCodeLink": "https://leetcode.com/problems/reverse-string-ii",
245-
"solutionLink": "#"
245+
"solutionLink": "/dsa-solutions/lc-solutions/0500-0599/reverse-string-ii"
246246
},
247247
{
248248
"problemName": "542. 01 Matrix",
@@ -314,7 +314,7 @@ export const problems =[
314314
"problemName": "553. Optimal Division",
315315
"difficulty": "Medium",
316316
"leetCodeLink": "https://leetcode.com/problems/optimal-division",
317-
"solutionLink": "#"
317+
"solutionLink": "/dsa-solutions/lc-solutions/0500-0599/optimal-division"
318318
},
319319
{
320320
"problemName": "554. Brick Wall",

0 commit comments

Comments
 (0)