Machine Learning Basics
by Dr. Jane Smith
Machine Learning Basics
Unsupervised Learning
Introduction
Unsupervised learning is a type of machine learning where the algorithm works with unlabeled data to discover hidden patterns, structures, and relationships. Unlike supervised learning, there are no predefined correct answers or output labels.
Types of Unsupervised Learning
- Clustering: Grouping similar data points together
- Dimensionality Reduction: Reducing the number of features while preserving important information
- Association Rule Mining: Discovering relationships between variables
- Anomaly Detection: Identifying unusual data points
Clustering
Clustering algorithms partition data into groups (clusters) where items in the same group are similar to each other and different from items in other groups.
Common Clustering Algorithms:
- K-Means Clustering
- Hierarchical Clustering
- DBSCAN (Density-Based Spatial Clustering)
- Mean Shift Clustering
- Gaussian Mixture Models
Example: Customer Segmentation
A retail company might use clustering to segment customers based on their purchasing behavior:
Features might include:
- Purchase frequency
- Average transaction value
- Product categories purchased
- Time since last purchase
Go Web Server
Go Web Server Example
This snippet shows a simple REST API server in Go with middleware.
1
2 package main
3
4 import (
5 "context"
6 "encoding/json"
7 "fmt"
8 "log"
9 "net/http"
10 "strconv"
11 "time"
12
13 "github.com/gorilla/mux"
14 )
15
16 type User struct {
17 ID int `json:"id"`
18 Name string `json:"name"`
19 Email string `json:"email"`
20 }
21
22 type Server struct {
23 router *mux.Router
24 users []User
25 }
26
27 func NewServer() *Server {
28 s := &Server{
29 router: mux.NewRouter(),
30 users: []User{
31 {ID: 1, Name: "John Doe", Email: "john@example.com"},
32 {ID: 2, Name: "Jane Smith", Email: "jane@example.com"},
33 },
34 }
35 s.setupRoutes()
36 return s
37 }
38
39 func (s *Server) setupRoutes() {
40 // Middleware
41 s.router.Use(s.loggingMiddleware)
42 s.router.Use(s.corsMiddleware)
43
44 // Routes
45 s.router.HandleFunc("/api/users", s.getUsers).Methods("GET")
46 s.router.HandleFunc("/api/users/{id}", s.getUser).Methods("GET")
47 s.router.HandleFunc("/api/users", s.createUser).Methods("POST")
48 s.router.HandleFunc("/api/users/{id}", s.updateUser).Methods("PUT")
49 s.router.HandleFunc("/api/users/{id}", s.deleteUser).Methods("DELETE")
50 }
51
52 func (s *Server) loggingMiddleware(next http.Handler) http.Handler {
53 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54 start := time.Now()
55 next.ServeHTTP(w, r)
56 log.Printf(
57 "%s %s %s %v",
58 r.Method,
59 r.RequestURI,
60 r.RemoteAddr,
61 time.Since(start),
62 )
63 })
64 }
65
66 func (s *Server) corsMiddleware(next http.Handler) http.Handler {
67 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
68 w.Header().Set("Access-Control-Allow-Origin", "*")
69 w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
70 w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
71
72 if r.Method == "OPTIONS" {
73 w.WriteHeader(http.StatusOK)
74 return
75 }
76
77 next.ServeHTTP(w, r)
78 })
79 }
80
81 func (s *Server) getUsers(w http.ResponseWriter, r *http.Request) {
82 w.Header().Set("Content-Type", "application/json")
83 json.NewEncoder(w).Encode(s.users)
84 }
85
86 func (s *Server) getUser(w http.ResponseWriter, r *http.Request) {
87 vars := mux.Vars(r)
88 id, _ := strconv.Atoi(vars["id"])
89
90 for _, user := range s.users {
91 if user.ID == id {
92 w.Header().Set("Content-Type", "application/json")
93 json.NewEncoder(w).Encode(user)
94 return
95 }
96 }
97
98 http.NotFound(w, r)
99 }
100
101 func (s *Server) createUser(w http.ResponseWriter, r *http.Request) {
102 var user User
103 if err := json.NewDecoder(r.Body).Decode(&user); err != nil {
104 http.Error(w, err.Error(), http.StatusBadRequest)
105 return
106 }
107
108 user.ID = len(s.users) + 1
109 s.users = append(s.users, user)
110
111 w.Header().Set("Content-Type", "application/json")
112 w.WriteHeader(http.StatusCreated)
113 json.NewEncoder(w).Encode(user)
114 }
115
116 func (s *Server) updateUser(w http.ResponseWriter, r *http.Request) {
117 vars := mux.Vars(r)
118 id, _ := strconv.Atoi(vars["id"])
119
120 var updatedUser User
121 if err := json.NewDecoder(r.Body).Decode(&updatedUser); err != nil {
122 http.Error(w, err.Error(), http.StatusBadRequest)
123 return
124 }
125
126 for i, user := range s.users {
127 if user.ID == id {
128 updatedUser.ID = id
129 s.users[i] = updatedUser
130 w.Header().Set("Content-Type", "application/json")
131 json.NewEncoder(w).Encode(updatedUser)
132 return
133 }
134 }
135
136 http.NotFound(w, r)
137 }
138
139 func (s *Server) deleteUser(w http.ResponseWriter, r *http.Request) {
140 vars := mux.Vars(r)
141 id, _ := strconv.Atoi(vars["id"])
142
143 for i, user := range s.users {
144 if user.ID == id {
145 s.users = append(s.users[:i], s.users[i+1:]...)
146 w.WriteHeader(http.StatusNoContent)
147 return
148 }
149 }
150
151 http.NotFound(w, r)
152 }
153
154 func main() {
155 server := NewServer()
156
157 fmt.Println("Server starting on port 8080...")
158 log.Fatal(http.ListenAndServe(":8080", server.router))
159 }
1
2 package main
3
4 import (
5 "fmt"
6 "sync"
7 )
8
9 type Customer struct {
10 ID string
11 Features []float64
12 }
13
14 func updateClusters(customers <-chan Customer) {
15 for customer := range customers {
16 // Update clustering model with new customer data
17 fmt.Printf("Processing customer: %s\n", customer.ID)
18 }
19 }
20
21 func main() {
22 customerStream := make(chan Customer, 100)
23
24 var wg sync.WaitGroup
25 wg.Add(1)
26 go func() {
27 defer wg.Done()
28 updateClusters(customerStream)
29 }()
30
31 // Simulate incoming customer data
32 for i := 0; i < 10; i++ {
33 customerStream <- Customer{
34 ID: fmt.Sprintf("cust-%d", i),
35 Features: []float64{float64(i), float64(i * 2)},
36 }
37 }
38
39 close(customerStream)
40 wg.Wait()
41 }
Dimensionality Reduction
Dimensionality reduction techniques reduce the number of features while preserving as much information as possible. Common Techniques:- Principal Component Analysis (PCA)
- t-SNE (t-Distributed Stochastic Neighbor Embedding)
- Autoencoders (Neural Networks)
- Factor Analysis
Rust Concurrent Programming
Rust Concurrent Programming Example
This snippet demonstrates concurrent programming in Rust using threads and channels.
1
2 use std::sync::{Arc, Mutex};
3 use std::thread;
4 use std::time::Duration;
5 use std::sync::mpsc;
6
7 fn main() {
8 println!("=== Rust Concurrent Programming Example ===\n");
9
10 // Example 1: Basic thread spawning
11 basic_thread_example();
12
13 // Example 2: Shared state with Arc<Mutex<>>
14 shared_state_example();
15
16 // Example 3: Channel communication
17 channel_example();
18 }
19
20 fn basic_thread_example() {
21 println!("1. Basic Thread Example:");
22
23 let handles: Vec<_> = (0..5)
24 .map(|i| {
25 thread::spawn(move || {
26 println!("Thread {} started", i);
27 thread::sleep(Duration::from_millis(100 * i));
28 println!("Thread {} finished", i);
29 i * 2
30 })
31 })
32 .collect();
33
34 for handle in handles {
35 let result = handle.join().unwrap();
36 println!("Thread result: {}", result);
37 }
38 println!();
39 }
40
41 fn shared_state_example() {
42 println!("2. Shared State Example:");
43
44 let counter = Arc::new(Mutex::new(0));
45 let mut handles = vec![];
46
47 for _ in 0..10 {
48 let counter_clone = Arc::clone(&counter);
49 let handle = thread::spawn(move || {
50 let mut num = counter_clone.lock().unwrap();
51 *num += 1;
52 });
53 handles.push(handle);
54 }
55
56 for handle in handles {
57 handle.join().unwrap();
58 }
59
60 println!("Result: {}", *counter.lock().unwrap());
61 println!();
62 }
63
64 fn channel_example() {
65 println!("3. Channel Communication Example:");
66
67 let (tx, rx) = mpsc::channel();
68
69 thread::spawn(move || {
70 let vals = vec![
71 String::from("hi"),
72 String::from("from"),
73 String::from("the"),
74 String::from("thread"),
75 ];
76
77 for val in vals {
78 tx.send(val).unwrap();
79 thread::sleep(Duration::from_millis(100));
80 }
81 });
82
83 for received in rx {
84 println!("Got: {}", received);
85 }
86 println!();
87 }
1
2 use rayon::prelude::*;
3 use ndarray::prelude::*;
4
5 fn parallel_pca(data: &Array2<f64>, n_components: usize) -> Array2<f64> {
6 let (n_samples, n_features) = data.dim();
7
8 // Center the data
9 let mean = data.mean_axis(Axis(0)).unwrap();
10 let centered = data - &mean;
11
12 // Compute covariance matrix in parallel
13 let covariance = centered.t().dot(¢ered) / (n_samples as f64 - 1.0);
14
15 // Parallel eigenvalue decomposition
16 let (eigenvectors, _) = parallel_eigendecomposition(&covariance);
17
18 // Return top components
19 eigenvectors.slice_axis(Axis(1), Slice::from(0..n_components)).to_owned()
20 }
Association Rule Mining
Association rule mining discovers interesting relationships between variables in large datasets.Famous Example: Market Basket Analysis
The classic example is discovering that customers who buy diapers also tend to buy beer. Algorithm: Apriori- Find frequent itemsets
- Generate association rules
- Evaluate rules using support, confidence, and lift
TypeScript React Component
TypeScript React Component Example
This snippet shows a TypeScript React component with proper typing.
1
2 import React, { useState, useEffect } from 'react';
3
4 interface User {
5 id: number;
6 name: string;
7 email: string;
8 role: 'admin' | 'user' | 'moderator';
9 }
10
11 interface UserListProps {
12 initialUsers: User[];
13 onUserSelect?: (user: User) => void;
14 }
15
16 const UserList: React.FC<UserListProps> = ({
17 initialUsers,
18 onUserSelect
19 }) => {
20 const [users, setUsers] = useState<User[]>(initialUsers);
21 const [filter, setFilter] = useState<string>('');
22 const [loading, setLoading] = useState<boolean>(false);
23
24 useEffect(() => {
25 // Fetch users from API
26 const fetchUsers = async () => {
27 setLoading(true);
28 try {
29 const response = await fetch('/api/users');
30 const data = await response.json();
31 setUsers(data);
32 } catch (error) {
33 console.error('Failed to fetch users:', error);
34 } finally {
35 setLoading(false);
36 }
37 };
38
39 fetchUsers();
40 }, []);
41
42 const filteredUsers = users.filter(user =>
43 user.name.toLowerCase().includes(filter.toLowerCase()) ||
44 user.email.toLowerCase().includes(filter.toLowerCase())
45 );
46
47 const handleUserClick = (user: User) => {
48 onUserSelect?.(user);
49 };
50
51 if (loading) {
52 return <div>Loading users...</div>;
53 }
54
55 return (
56 <div className="user-list">
57 <input
58 type="text"
59 placeholder="Filter users..."
60 value={filter}
61 onChange={(e) => setFilter(e.target.value)}
62 className="filter-input"
63 />
64 <ul className="user-items">
65 {filteredUsers.map(user => (
66 <li
67 key={user.id}
68 onClick={() => handleUserClick(user)}
69 className={`user-item ${user.role}`}
70 >
71 <span className="user-name">{user.name}</span>
72 <span className="user-email">{user.email}</span>
73 <span className="user-role">{user.role}</span>
74 </li>
75 ))}
76 </ul>
77 </div>
78 );
79 };
80
81 export default UserList;
1
2 import React, { useState, useMemo } from 'react';
3
4 interface AssociationRule {
5 antecedent: string[];
6 consequent: string[];
7 support: number;
8 confidence: number;
9 lift: number;
10 }
11
12 const AssociationRulesVisualization: React.FC<{ rules: AssociationRule[] }> = ({ rules }) => {
13 const [minSupport, setMinSupport] = useState(0.1);
14 const [minConfidence, setMinConfidence] = useState(0.5);
15
16 const filteredRules = useMemo(() => {
17 return rules.filter(rule =>
18 rule.support >= minSupport && rule.confidence >= minConfidence
19 );
20 }, [rules, minSupport, minConfidence]);
21
22 return (
23 <div>
24 <div>
25 <label>Min Support: {minSupport}</label>
26 <input
27 type="range"
28 min="0"
29 max="1"
30 step="0.01"
31 value={minSupport}
32 onChange={(e) => setMinSupport(parseFloat(e.target.value))}
33 />
34 </div>
35 <div>
36 <label>Min Confidence: {minConfidence}</label>
37 <input
38 type="range"
39 min="0"
40 max="1"
41 step="0.01"
42 value={minConfidence}
43 onChange={(e) => setMinConfidence(parseFloat(e.target.value))}
44 />
45 </div>
46 <ul>
47 {filteredRules.map((rule, index) => (
48 <li key={index}>
49 {rule.antecedent.join(', ')} → {rule.consequent.join(', ')}
50 <br />
51 <small>Support: {rule.support.toFixed(3)}, Confidence: {rule.confidence.toFixed(3)}</small>
52 </li>
53 ))}
54 </ul>
55 </div>
56 );
57 };
Anomaly Detection
Anomaly detection identifies data points that deviate significantly from the majority of the data. Applications:- Fraud detection
- Network intrusion detection
- Manufacturing defect detection
- Healthcare monitoring