Code Library
Reusable code snippets and complete implementations. Copy, paste, and adapt for your projects.
Machine LearningPython
Linear Regression Implementation
Complete implementation of linear regression from scratch using NumPy
import numpy as np
class LinearRegression:
def __init__(self, learning_rate=0.01, iterations=1000):
self.lr = learning_rate
self.iterations = iterations
self.weights = None
self.bias = None
def fit(self, X, y):
n_samples, n_features = X.shape
self.weights = np.zeros(n_features)
self.bias = 0
for _ in range(self.iterations):
y_pred = np.dot(X, self.weights) + self.bias
dw = (1/n_samples) * np.dot(X.T, (y_pred - y))
db = (1/n_samples) * np.sum(y_pred - y)
self.weights -= self.lr * dw
self.bias -= self.lr * db
def predict(self, X):
return np.dot(X, self.weights) + self.biasData AnalysisPython
Data Preprocessing Pipeline
Comprehensive data cleaning and preprocessing functions
import pandas as pd
from sklearn.preprocessing import StandardScaler
def preprocess_data(df):
# Handle missing values
df = df.fillna(df.mean())
# Remove duplicates
df = df.drop_duplicates()
# Encode categorical variables
categorical_cols = df.select_dtypes(include=['object']).columns
df = pd.get_dummies(df, columns=categorical_cols)
# Scale numerical features
scaler = StandardScaler()
numerical_cols = df.select_dtypes(include=['float64', 'int64']).columns
df[numerical_cols] = scaler.fit_transform(df[numerical_cols])
return dfDeep LearningPython
Neural Network Layer
Custom neural network layer implementation with backpropagation
import numpy as np
class DenseLayer:
def __init__(self, input_size, output_size):
self.weights = np.random.randn(input_size, output_size) * 0.01
self.bias = np.zeros((1, output_size))
def forward(self, inputs):
self.inputs = inputs
self.output = np.dot(inputs, self.weights) + self.bias
return self.output
def backward(self, dvalues, learning_rate):
dweights = np.dot(self.inputs.T, dvalues)
dbias = np.sum(dvalues, axis=0, keepdims=True)
dinputs = np.dot(dvalues, self.weights.T)
self.weights -= learning_rate * dweights
self.bias -= learning_rate * dbias
return dinputsSQLPython
SQL Query Builder
Python class for building SQL queries programmatically
class QueryBuilder:
def __init__(self, table):
self.table = table
self.query_parts = []
def select(self, *columns):
cols = ', '.join(columns) if columns else '*'
self.query_parts.append(f"SELECT {cols} FROM {self.table}")
return self
def where(self, condition):
self.query_parts.append(f"WHERE {condition}")
return self
def order_by(self, column, direction='ASC'):
self.query_parts.append(f"ORDER BY {column} {direction}")
return self
def limit(self, count):
self.query_parts.append(f"LIMIT {count}")
return self
def build(self):
return ' '.join(self.query_parts)Want the Complete Repository?
Access all our code examples, projects, and resources on GitHub.
View on GitHub