Python Scripts For Beginners: Scientific Computing

Scientific Computing

Collection of 100 Python scripts for beginners related to Scientific Computing, each designed to help with fundamental tasks and provide useful examples.

100 Python Scripts For Beginners: Scientific Computing

1. Basic Arithmetic Operations

def basic_arithmetic(a, b):
return {
'addition': a + b,
'subtraction': a - b,
'multiplication': a * b,
'division': a / b if b != 0 else 'Division by zero'
}

print(basic_arithmetic(10, 5))

Performs basic arithmetic operations: addition, subtraction, multiplication, and division.


2. Solve a Quadratic Equation

import cmath

def solve_quadratic(a, b, c):
discriminant = cmath.sqrt(b**2 - 4*a*c)
root1 = (-b + discriminant) / (2*a)
root2 = (-b - discriminant) / (2*a)
return root1, root2

print(solve_quadratic(1, -3, 2))

Solves a quadratic equation ax2+bx+c=0ax^2 + bx + c = 0ax2+bx+c=0 using the quadratic formula.


3. Numerical Integration Using Trapezoidal Rule

import numpy as np

def trapezoidal_integration(f, a, b, n):
x = np.linspace(a, b, n)
y = f(x)
h = (b - a) / (n - 1)
integral = (h / 2) * (y[0] + 2 * np.sum(y[1:-1]) + y[-1])
return integral

def function(x):
return x**2

print(trapezoidal_integration(function, 0, 1, 100))

Computes the integral of a function using the trapezoidal rule.


4. Solve Linear System Using NumPy

import numpy as np

def solve_linear_system(A, B):
return np.linalg.solve(A, B)

A = np.array([[2, 1], [1, 3]])
B = np.array([8, 13])
print(solve_linear_system(A, B))

Solves a system of linear equations AX=BAX = BAX=B using NumPy.


5. Eigenvalues and Eigenvectors

import numpy as np

def eigen_decomposition(matrix):
values, vectors = np.linalg.eig(matrix)
return values, vectors

matrix = np.array([[1, 2], [3, 4]])
print(eigen_decomposition(matrix))

Calculates eigenvalues and eigenvectors of a matrix.


6. Perform Singular Value Decomposition (SVD)

import numpy as np

def svd_decomposition(matrix):
U, S, V = np.linalg.svd(matrix)
return U, S, V

matrix = np.array([[1, 2], [3, 4]])
print(svd_decomposition(matrix))

Performs Singular Value Decomposition (SVD) on a matrix.


7. Fourier Transform

import numpy as np
import matplotlib.pyplot as plt

def fourier_transform(signal):
return np.fft.fft(signal)

signal = np.array([1, 2, 3, 4, 5])
transformed_signal = fourier_transform(signal)
plt.plot(np.abs(transformed_signal))
plt.title('Fourier Transform')
plt.show()

Computes the Fourier Transform of a signal and plots its magnitude.


8. Numerical Solution to Ordinary Differential Equations (ODE)

from scipy.integrate import odeint
import numpy as np

def model(y, t):
dydt = -2 * y + 1
return dydt

y0 = 0
t = np.linspace(0, 5, 100)
solution = odeint(model, y0, t)
print(solution)

Solves an ordinary differential equation (ODE) using odeint from SciPy.


9. Perform Polynomial Fitting

import numpy as np
import matplotlib.pyplot as plt

def polynomial_fitting(x, y, degree):
coefficients = np.polyfit(x, y, degree)
polynomial = np.poly1d(coefficients)
return polynomial

x = np.array([1, 2, 3, 4])
y = np.array([2, 3, 5, 7])
polynomial = polynomial_fitting(x, y, 2)

x_fit = np.linspace(1, 4, 100)
y_fit = polynomial(x_fit)
plt.scatter(x, y, color='red')
plt.plot(x_fit, y_fit, color='blue')
plt.title('Polynomial Fitting')
plt.show()

Fits a polynomial to data points and plots the result.


10. Matrix Operations: Addition and Multiplication

import numpy as np

def matrix_operations(A, B):
addition = np.add(A, B)
multiplication = np.dot(A, B)
return addition, multiplication

A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(matrix_operations(A, B))

Performs matrix addition and multiplication.


11. Numerical Derivative

import numpy as np

def numerical_derivative(f, x, h=1e-5):
return (f(x + h) - f(x - h)) / (2 * h)

def function(x):
return x**3

print(numerical_derivative(function, 2))

Computes the numerical derivative of a function at a given point.


12. Solve Nonlinear Equations

from scipy.optimize import fsolve

def equation(x):
return x**3 - 2*x - 5

solution = fsolve(equation, x0=2)
print(solution)

Finds the roots of a nonlinear equation using fsolve from SciPy.


13. Statistical Measures: Mean, Median, Mode

import numpy as np
from scipy import stats

def statistical_measures(data):
mean = np.mean(data)
median = np.median(data)
mode = stats.mode(data)[0][0]
return mean, median, mode

data = [1, 2, 2, 3, 4]
print(statistical_measures(data))

Computes mean, median, and mode of a dataset.


14. Calculate Standard Deviation and Variance

import numpy as np

def std_dev_variance(data):
std_dev = np.std(data)
variance = np.var(data)
return std_dev, variance

data = [1, 2, 3, 4, 5]
print(std_dev_variance(data))

Calculates standard deviation and variance of a dataset.


15. Numerical Linear Algebra Operations

import numpy as np

def linear_algebra_operations(matrix):
inverse = np.linalg.inv(matrix)
determinant = np.linalg.det(matrix)
return inverse, determinant

matrix = np.array([[1, 2], [3, 4]])
print(linear_algebra_operations(matrix))

Computes the inverse and determinant of a matrix.


16. Matrix Decomposition: LU Decomposition

import numpy as np
from scipy.linalg import lu

def lu_decomposition(matrix):
P, L, U = lu(matrix)
return P, L, U

matrix = np.array([[1, 2], [3, 4]])
print(lu_decomposition(matrix))

Performs LU decomposition of a matrix.


17. Compute Eigenvalues and Eigenvectors of a Symmetric Matrix

import numpy as np

def eigen_decomposition_symmetric(matrix):
values, vectors = np.linalg.eigh(matrix)
return values, vectors

matrix = np.array([[1, 2], [2, 1]])
print(eigen_decomposition_symmetric(matrix))

Computes eigenvalues and eigenvectors of a symmetric matrix.


18. Perform Singular Value Decomposition (SVD)

import numpy as np

def svd_decomposition(matrix):
U, S, Vt = np.linalg.svd(matrix)
return U, S, Vt

matrix = np.array([[1, 2], [3, 4]])
print(svd_decomposition(matrix))

Performs Singular Value Decomposition (SVD) on a matrix.


19. Compute the Gradient of a Function

import numpy as np

def gradient(f, x, h=1e-5):
return (f(x + h) - f(x - h)) / (2 * h)

def function(x):
return x**2 + 3*x + 2

print(gradient(function, 2))

Computes the gradient of a function at a given point.


20. Perform Convolution Operation

import numpy as np
from scipy.signal import convolve

def perform_convolution(signal, kernel):
return convolve(signal, kernel, mode='same')

signal = np.array([1, 2, 3, 4])
kernel = np.array([0.25, 0.5, 0.25])
print(perform_convolution(signal, kernel))

Performs convolution of a signal with a kernel.


21. Fast Fourier Transform (FFT)

import numpy as np
import matplotlib.pyplot as plt

def fast_fourier_transform(signal):
return np.fft.fft(signal)

signal = np.array([1, 2, 3, 4])
transformed = fast_fourier_transform(signal)
plt.plot(np.abs(transformed))
plt.title('FFT of Signal')
plt.show()

Computes the Fast Fourier Transform (FFT) of a signal and plots it.


22. Inverse Fourier Transform

import numpy as np
import matplotlib.pyplot as plt

def inverse_fourier_transform(transformed_signal):
return np.fft.ifft(transformed_signal)

transformed_signal = np.fft.fft([1, 2, 3, 4])
reconstructed_signal = inverse_fourier_transform(transformed_signal)
plt.plot(np.real(reconstructed_signal))
plt.title('Inverse FFT of Transformed Signal')
plt.show()

Computes the Inverse Fast Fourier Transform (IFFT) of a signal.


23. Perform Polynomial Regression

import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

def polynomial_regression(x, y, degree):
poly = PolynomialFeatures(degree=degree)
X_poly = poly.fit_transform(x.reshape(-1, 1))
model = LinearRegression()
model.fit(X_poly, y)
return model

x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])
model = polynomial_regression(x, y, 2)

x_fit = np.linspace(1, 4, 100)
y_fit = model.predict(PolynomialFeatures(degree=2).fit_transform(x_fit.reshape(-1, 1)))
plt.scatter(x, y, color='red')
plt.plot(x_fit, y_fit, color='blue')
plt.title('Polynomial Regression')
plt.show()

Performs polynomial regression on a dataset and plots the result.


24. Calculate the Determinant of a Matrix

import numpy as np

def matrix_determinant(matrix):
return np.linalg.det(matrix)

matrix = np.array([[1, 2], [3, 4]])
print(matrix_determinant(matrix))

Computes the determinant of a matrix.


25. Perform Principal Component Analysis (PCA)

import numpy as np
from sklearn.decomposition import PCA

def perform_pca(data):
pca = PCA(n_components=2)
return pca.fit_transform(data)

data = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2]])
print(perform_pca(data))

Performs Principal Component Analysis (PCA) to reduce dimensionality.


26. Fit a Linear Model

import numpy as np
from sklearn.linear_model import LinearRegression

def fit_linear_model(X, y):
model = LinearRegression()
model.fit(X, y)
return model

X = np.array([[1], [2], [3], [4]])
y = np.array([1, 2, 3, 4])
model = fit_linear_model(X, y)
print(model.coef_, model.intercept_)

Fits a linear regression model to data.


27. Perform K-Means Clustering

import numpy as np
from sklearn.cluster import KMeans

def k_means_clustering(data, n_clusters):
kmeans = KMeans(n_clusters=n_clusters)
return kmeans.fit_predict(data)

data = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
print(k_means_clustering(data, 2))

Performs K-Means clustering on a dataset.


28. Compute the Jacobian Matrix

import numpy as np

def jacobian(f, x):
n = len(x)
m = len(f(x))
jacobian_matrix = np.zeros((m, n))
h = 1e-8
for i in range(n):
x1 = x.copy()
x2 = x.copy()
x1[i] -= h
x2[i] += h
f1 = f(x1)
f2 = f(x2)
jacobian_matrix[:, i] = (f2 - f1) / (2 * h)
return jacobian_matrix

def func(x):
return np.array([x[0]**2 + x[1]**2, x[0] - x[1]])

x = np.array([1.0, 2.0])
print(jacobian(func, x))

Computes the Jacobian matrix for a system of functions.


29. Implement Gradient Descent

import numpy as np

def gradient_descent(learning_rate, num_iterations, initial_theta):
theta = initial_theta
for _ in range(num_iterations):
gradient = 2 * theta # Example gradient
theta -= learning_rate * gradient
return theta

print(gradient_descent(0.1, 1000, 5))

Implements a simple gradient descent algorithm.


30. Solve Nonlinear Least Squares Problem

import numpy as np
from scipy.optimize import least_squares

def residuals(params):
a, b = params
return a * np.arange(10) + b - np.random.randn(10)

result = least_squares(residuals, [1, 1])
print(result.x)

Solves a nonlinear least squares problem using least_squares from SciPy.


31. Compute the Riemann Sum

import numpy as np

def riemann_sum(f, a, b, n):
x = np.linspace(a, b, n)
dx = (b - a) / n
return np.sum(f(x) * dx)

def function(x):
return x**2

print(riemann_sum(function, 0, 1, 100))

Computes the Riemann sum for a given function.


32. Matrix Transposition

import numpy as np

def matrix_transposition(matrix):
return np.transpose(matrix)

matrix = np.array([[1, 2], [3, 4]])
print(matrix_transposition(matrix))

Transposes a matrix.


33. Perform Ridge Regression

import numpy as np
from sklearn.linear_model import Ridge

def ridge_regression(X, y, alpha):
model = Ridge(alpha=alpha)
model.fit(X, y)
return model

X = np.array([[1], [2], [3], [4]])
y = np.array([1, 2, 3, 4])
model = ridge_regression(X, y, 1.0)
print(model.coef_, model.intercept_)

Performs Ridge Regression to handle multicollinearity.


34. Principal Component Analysis (PCA) with Plot

import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt

def pca_with_plot(data):
pca = PCA(n_components=2)
transformed_data = pca.fit_transform(data)
plt.scatter(transformed_data[:, 0], transformed_data[:, 1])
plt.title('PCA Plot')
plt.show()

data = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2]])
pca_with_plot(data)

Performs PCA and visualizes the result with a scatter plot.


35. Perform Logistic Regression

import numpy as np
from sklearn.linear_model import LogisticRegression

def logistic_regression(X, y):
model = LogisticRegression()
model.fit(X, y)
return model

X = np.array([[1], [2], [3], [4]])
y = np.array([0, 0, 1, 1])
model = logistic_regression(X, y)
print(model.coef_, model.intercept_)

Fits a logistic regression model to binary classification data.


36. Compute Pearson Correlation Coefficient

import numpy as np
from scipy.stats import pearsonr

def pearson_correlation(x, y):
coefficient, _ = pearsonr(x, y)
return coefficient

x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 6, 8])
print(pearson_correlation(x, y))

Calculates the Pearson correlation coefficient between two datasets.


37. Compute Spearman’s Rank Correlation

import numpy as np
from scipy.stats import spearmanr

def spearman_correlation(x, y):
coefficient, _ = spearmanr(x, y)
return coefficient

x = np.array([1, 2, 3, 4])
y = np.array([2, 4, 6, 8])
print(spearman_correlation(x, y))

Calculates Spearman’s rank correlation coefficient between two datasets.


38. Simulate a Random Walk

import numpy as np
import matplotlib.pyplot as plt

def random_walk(steps):
walk = np.zeros(steps)
for i in range(1, steps):
walk[i] = walk[i - 1] + np.random.choice([-1, 1])
return walk

steps = 1000
plt.plot(random_walk(steps))
plt.title('Random Walk Simulation')
plt.show()

Simulates and plots a random walk.


39. Generate a 3D Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def plot_3d_surface(X, Y, Z):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()

X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X**2 + Y**2))

plot_3d_surface(X, Y, Z)

Generates and plots a 3D surface plot.


40. Calculate the Root Mean Squared Error (RMSE)

import numpy as np

def root_mean_squared_error(y_true, y_pred):
return np.sqrt(np.mean((y_true - y_pred) ** 2))

y_true = np.array([1, 2, 3, 4])
y_pred = np.array([1.1, 2.1, 2.9, 4.1])
print(root_mean_squared_error(y_true, y_pred))

Computes the Root Mean Squared Error (RMSE) between true and predicted values.


41. Perform Principal Component Analysis (PCA) on Images

import numpy as np
from sklearn.decomposition import PCA
import matplotlib.pyplot as plt
from skimage import color, io

def pca_on_images(image_path):
image = color.rgb2gray(io.imread(image_path))
reshaped_image = image.reshape(-1, image.shape[1])
pca = PCA(n_components=2)
transformed_image = pca.fit_transform(reshaped_image)
plt.scatter(transformed_image[:, 0], transformed_image[:, 1])
plt.title('PCA on Images')
plt.show()

pca_on_images('path_to_image.jpg')

Performs PCA on image data and plots the result.


42. Perform Time Series Forecasting

import numpy as np
import matplotlib.pyplot as plt
from statsmodels.tsa.arima_model import ARIMA

def time_series_forecasting(data):
model = ARIMA(data, order=(5, 1, 0))
model_fit = model.fit(disp=0)
forecast = model_fit.forecast(steps=10)[0]
plt.plot(data, label='Original')
plt.plot(np.arange(len(data), len(data) + 10), forecast, label='Forecast', color='red')
plt.legend()
plt.show()

data = np.sin(np.linspace(0, 10, 100))
time_series_forecasting(data)

Forecasts future values of a time series using ARIMA and plots the result.


43. Implement a Simple Monte Carlo Simulation

import numpy as np

def monte_carlo_simulation(num_simulations):
successes = 0
for _ in range(num_simulations):
if np.random.random() < 0.5: # Example probability
successes += 1
return successes / num_simulations

print(monte_carlo_simulation(10000))

Runs a Monte Carlo simulation to estimate the probability of an event.


44. Perform Gradient Descent for Linear Regression

import numpy as np

def gradient_descent(X, y, learning_rate, num_iterations):
m, n = X.shape
theta = np.zeros(n)
for _ in range(num_iterations):
predictions = X.dot(theta)
errors = predictions - y
gradient = (1/m) * X.T.dot(errors)
theta -= learning_rate * gradient
return theta

X = np.array([[1, 1], [2, 2], [3, 3]])
y = np.array([2, 4, 6])
theta = gradient_descent(X, y, learning_rate=0.01, num_iterations=1000)
print(theta)

Performs gradient descent to fit a linear regression model.


45. Implement a Simple Kalman Filter

import numpy as np

def kalman_filter(measurements, process_variance, measurement_variance, estimated_measurement_variance):
estimate = 0
estimate_variance = 1
estimates = []

for measurement in measurements:
# Prediction
estimate = estimate
estimate_variance = estimate_variance + process_variance

# Update
kalman_gain = estimate_variance / (estimate_variance + measurement_variance)
estimate = estimate + kalman_gain * (measurement - estimate)
estimate_variance = (1 - kalman_gain) * estimate_variance

estimates.append(estimate)

return estimates

measurements = [5, 6, 7, 9, 10]
print(kalman_filter(measurements, 1, 1, 1))

Implements a basic Kalman filter for smoothing measurements.


46. Perform Curve Fitting

import numpy as np
from scipy.optimize import curve_fit

def curve_fit_function(x, a, b):
return a * np.exp(b * x)

x = np.array([1, 2, 3, 4])
y = np.array([2.718, 7.389, 20.085, 54.598])
params, _ = curve_fit(curve_fit_function, x, y)
print(params)

Fits an exponential curve to data points.


47. Perform Data Normalization

import numpy as np

def normalize_data(data):
min_val = np.min(data)
max_val = np.max(data)
return (data - min_val) / (max_val - min_val)

data = np.array([1, 2, 3, 4, 5])
print(normalize_data(data))

Normalizes data to a range of [0, 1].


48. Compute the Covariance Matrix

import numpy as np

def covariance_matrix(data):
return np.cov(data, rowvar=False)

data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(covariance_matrix(data))

Computes the covariance matrix for a dataset.


49. Implement a Simple Neural Network

import numpy as np

class SimpleNN:
def __init__(self, input_size, hidden_size, output_size):
self.W1 = np.random.randn(input_size, hidden_size)
self.b1 = np.zeros(hidden_size)
self.W2 = np.random.randn(hidden_size, output_size)
self.b2 = np.zeros(output_size)

def forward(self, X):
self.hidden = np.tanh(X.dot(self.W1) + self.b1)
self.output = self.hidden.dot(self.W2) + self.b2
return self.output

X = np.array([[1, 2], [3, 4]])
nn = SimpleNN(input_size=2, hidden_size=3, output_size=1)
print(nn.forward(X))

Implements a simple feedforward neural network with one hidden layer.


50. Implement K-Nearest Neighbors (KNN)

import numpy as np
from sklearn.neighbors import KNeighborsClassifier

def knn_classification(X_train, y_train, X_test, k):
model = KNeighborsClassifier(n_neighbors=k)
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [3, 4], [5, 6]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 3]])
print(knn_classification(X_train, y_train, X_test, k=2))

Performs classification using the K-Nearest Neighbors algorithm.


51. Generate Random Numbers with a Normal Distribution

import numpy as np

def generate_normal_random_numbers(mean, std_dev, size):
return np.random.normal(mean, std_dev, size)

print(generate_normal_random_numbers(0, 1, 10))

Generates random numbers following a normal distribution.


52. Perform Statistical Hypothesis Testing

import numpy as np
from scipy import stats

def t_test(sample1, sample2):
t_stat, p_value = stats.ttest_ind(sample1, sample2)
return t_stat, p_value

sample1 = np.array([1, 2, 3, 4, 5])
sample2 = np.array([2, 3, 4, 5, 6])
print(t_test(sample1, sample2))

Performs a t-test to compare two samples.

53. Compute the Inverse of a Matrix

import numpy as np

def matrix_inverse(matrix):
return np.linalg.inv(matrix)

matrix = np.array([[1, 2], [3, 4]])
print(matrix_inverse(matrix))

Computes the inverse of a matrix.


54. Implement a Simple Support Vector Machine (SVM)

import numpy as np
from sklearn.svm import SVC

def support_vector_machine(X_train, y_train, X_test):
model = SVC()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(support_vector_machine(X_train, y_train, X_test))

Implements a simple Support Vector Machine for classification.


55. Calculate the Area Under the Curve (AUC)

import numpy as np
from sklearn.metrics import roc_auc_score

def calculate_auc(y_true, y_scores):
return roc_auc_score(y_true, y_scores)

y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])
print(calculate_auc(y_true, y_scores))

Calculates the Area Under the Receiver Operating Characteristic Curve (AUC).


56. Perform Hierarchical Clustering

import numpy as np
import scipy.cluster.hierarchy as sch
import matplotlib.pyplot as plt

def hierarchical_clustering(data):
dendrogram = sch.dendrogram(sch.linkage(data, method='ward'))
plt.show()

data = np.array([[1, 2], [2, 3], [3, 4], [5, 6]])
hierarchical_clustering(data)

Performs hierarchical clustering and plots a dendrogram.


57. Create a Confusion Matrix

import numpy as np
from sklearn.metrics import confusion_matrix

def create_confusion_matrix(y_true, y_pred):
return confusion_matrix(y_true, y_pred)

y_true = np.array([0, 1, 0, 1])
y_pred = np.array([0, 0, 1, 1])
print(create_confusion_matrix(y_true, y_pred))

Creates a confusion matrix for classification results.


58. Compute the Precision and Recall

import numpy as np
from sklearn.metrics import precision_score, recall_score

def compute_precision_recall(y_true, y_pred):
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
return precision, recall

y_true = np.array([0, 1, 0, 1])
y_pred = np.array([0, 0, 1, 1])
print(compute_precision_recall(y_true, y_pred))

Computes precision and recall for classification results.


59. Generate Synthetic Data

import numpy as np
import matplotlib.pyplot as plt

def generate_synthetic_data(mean, std_dev, size):
return np.random.normal(mean, std_dev, size)

data = generate_synthetic_data(0, 1, 100)
plt.hist(data, bins=30)
plt.title('Synthetic Data Distribution')
plt.show()

Generates and visualizes synthetic data with a normal distribution.


60. Implement a Simple Decision Tree

import numpy as np
from sklearn.tree import DecisionTreeClassifier

def decision_tree_classification(X_train, y_train, X_test):
model = DecisionTreeClassifier()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(decision_tree_classification(X_train, y_train, X_test))

Implements a simple Decision Tree classifier.


61. Calculate Entropy

import numpy as np

def entropy(probabilities):
return -np.sum(probabilities * np.log2(probabilities))

probabilities = np.array([0.5, 0.5])
print(entropy(probabilities))

Calculates the entropy of a probability distribution.


62. Implement a Naive Bayes Classifier

import numpy as np
from sklearn.naive_bayes import GaussianNB

def naive_bayes_classification(X_train, y_train, X_test):
model = GaussianNB()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(naive_bayes_classification(X_train, y_train, X_test))

Implements a Naive Bayes classifier.


63. Implement a Random Forest Classifier

import numpy as np
from sklearn.ensemble import RandomForestClassifier

def random_forest_classification(X_train, y_train, X_test):
model = RandomForestClassifier()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(random_forest_classification(X_train, y_train, X_test))

Implements a Random Forest classifier.


64. Implement a Simple Linear Regression with Gradient Descent

import numpy as np

def linear_regression_gd(X, y, learning_rate, num_iterations):
m, n = X.shape
theta = np.zeros(n)
for _ in range(num_iterations):
predictions = X.dot(theta)
errors = predictions - y
gradient = (1/m) * X.T.dot(errors)
theta -= learning_rate * gradient
return theta

X = np.array([[1, 1], [2, 2], [3, 3]])
y = np.array([2, 4, 6])
theta = linear_regression_gd(X, y, learning_rate=0.01, num_iterations=1000)
print(theta)

Implements linear regression using gradient descent.


65. Create and Plot a Histogram

import numpy as np
import matplotlib.pyplot as plt

def plot_histogram(data):
plt.hist(data, bins=30, edgecolor='black')
plt.title('Histogram')
plt.show()

data = np.random.normal(0, 1, 1000)
plot_histogram(data)

Generates and plots a histogram of data.


66. Perform Bayesian Inference

import numpy as np
import scipy.stats as stats

def bayesian_inference(prior, likelihood, evidence):
posterior = prior * likelihood / evidence
return posterior

prior = 0.5
likelihood = 0.9
evidence = 0.7
print(bayesian_inference(prior, likelihood, evidence))

Performs Bayesian inference given prior, likelihood, and evidence.


67. Compute the Mean Absolute Error (MAE)

import numpy as np

def mean_absolute_error(y_true, y_pred):
return np.mean(np.abs(y_true - y_pred))

y_true = np.array([1, 2, 3, 4])
y_pred = np.array([1.1, 2.1, 2.9, 4.1])
print(mean_absolute_error(y_true, y_pred))

Computes the Mean Absolute Error (MAE) between true and predicted values.


68. Perform Data Imputation

import numpy as np
from sklearn.impute import SimpleImputer

def impute_missing_data(data):
imputer = SimpleImputer(strategy='mean')
return imputer.fit_transform(data)

data = np.array([[1, 2], [np.nan, 3], [7, 6]])
print(impute_missing_data(data))

Performs mean imputation on missing data.


69. Compute the Cumulative Distribution Function (CDF)

import numpy as np
import scipy.stats as stats

def cumulative_distribution_function(data, x):
return stats.norm.cdf(x, np.mean(data), np.std(data))

data = np.random.normal(0, 1, 1000)
print(cumulative_distribution_function(data, 1))

Computes the Cumulative Distribution Function (CDF) for a given value.


70. Implement a Naive Bayes Classifier for Multinomial Data

import numpy as np
from sklearn.naive_bayes import MultinomialNB

def multinomial_naive_bayes(X_train, y_train, X_test):
model = MultinomialNB()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2, 3], [3, 2, 1], [2, 3, 1]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2, 2]])
print(multinomial_naive_bayes(X_train, y_train, X_test))

Implements a Naive Bayes classifier for multinomial data.

71. Implement K-Means Clustering

import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

def k_means_clustering(data, n_clusters):
model = KMeans(n_clusters=n_clusters)
model.fit(data)
labels = model.predict(data)
plt.scatter(data[:, 0], data[:, 1], c=labels, cmap='viridis')
plt.title('K-Means Clustering')
plt.show()
return labels

data = np.array([[1, 2], [2, 3], [3, 4], [5, 6], [1, 1], [2, 2]])
print(k_means_clustering(data, 2))

Performs K-Means clustering and visualizes the result.


72. Perform Principal Component Analysis (PCA)

import numpy as np
from sklearn.decomposition import PCA

def perform_pca(data, n_components):
pca = PCA(n_components=n_components)
transformed_data = pca.fit_transform(data)
return transformed_data

data = np.array([[1, 2], [2, 3], [3, 4], [5, 6]])
print(perform_pca(data, 1))

Reduces the dimensionality of the data using PCA.


73. Implement a Simple Neural Network

import numpy as np
from sklearn.neural_network import MLPClassifier

def simple_neural_network(X_train, y_train, X_test):
model = MLPClassifier(hidden_layer_sizes=(5,), max_iter=1000)
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(simple_neural_network(X_train, y_train, X_test))

Implements a simple neural network for classification.


74. Perform Feature Scaling

import numpy as np
from sklearn.preprocessing import StandardScaler

def feature_scaling(data):
scaler = StandardScaler()
return scaler.fit_transform(data)

data = np.array([[1, 2], [2, 3], [3, 4]])
print(feature_scaling(data))

Scales features to have a mean of 0 and a standard deviation of 1.


75. Generate a Correlation Matrix

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

def correlation_matrix(data):
corr_matrix = np.corrcoef(data, rowvar=False)
sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')
plt.title('Correlation Matrix')
plt.show()
return corr_matrix

data = np.array([[1, 2], [2, 3], [3, 4]])
print(correlation_matrix(data))

Generates and visualizes a correlation matrix for data.


76. Implement Logistic Regression

import numpy as np
from sklearn.linear_model import LogisticRegression

def logistic_regression(X_train, y_train, X_test):
model = LogisticRegression()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(logistic_regression(X_train, y_train, X_test))

Implements logistic regression for binary classification.


77. Compute the R-squared Value

import numpy as np
from sklearn.metrics import r2_score

def compute_r_squared(y_true, y_pred):
return r2_score(y_true, y_pred)

y_true = np.array([1, 2, 3, 4])
y_pred = np.array([1.1, 2.0, 2.9, 4.1])
print(compute_r_squared(y_true, y_pred))

Calculates the R-squared value to evaluate the goodness of fit.


78. Apply Cross-Validation

import numpy as np
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LinearRegression

def cross_validation(X, y, cv):
model = LinearRegression()
scores = cross_val_score(model, X, y, cv=cv)
return scores.mean()

X = np.array([[1, 2], [2, 3], [3, 4]])
y = np.array([1, 2, 3])
print(cross_validation(X, y, cv=2))

Applies cross-validation to evaluate model performance.


79. Implement a Simple K-Nearest Neighbors (KNN) Classifier

import numpy as np
from sklearn.neighbors import KNeighborsClassifier

def knn_classification(X_train, y_train, X_test, n_neighbors):
model = KNeighborsClassifier(n_neighbors=n_neighbors)
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(knn_classification(X_train, y_train, X_test, 2))

Implements a K-Nearest Neighbors classifier for classification.


80. Generate a Box Plot

import numpy as np
import matplotlib.pyplot as plt

def generate_box_plot(data):
plt.boxplot(data)
plt.title('Box Plot')
plt.show()

data = [np.random.normal(0, 1, 100) for _ in range(5)]
generate_box_plot(data)

Generates and visualizes a box plot for data.


81. Implement Gradient Boosting

import numpy as np
from sklearn.ensemble import GradientBoostingClassifier

def gradient_boosting(X_train, y_train, X_test):
model = GradientBoostingClassifier()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(gradient_boosting(X_train, y_train, X_test))

Implements Gradient Boosting for classification.


82. Perform Time Series Forecasting

import numpy as np
import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing

def time_series_forecasting(data, periods):
model = ExponentialSmoothing(data, seasonal='add', seasonal_periods=12)
fit = model.fit()
forecast = fit.forecast(periods)
return forecast

data = np.sin(np.linspace(0, 20, 100))
forecast = time_series_forecasting(data, 10)
print(forecast)

Performs time series forecasting using Exponential Smoothing.


83. Calculate the Coefficient of Variation

import numpy as np

def coefficient_of_variation(data):
return np.std(data) / np.mean(data)

data = np.array([1, 2, 3, 4, 5])
print(coefficient_of_variation(data))

Calculates the Coefficient of Variation for a data set.


84. Implement a Perceptron Classifier

import numpy as np
from sklearn.linear_model import Perceptron

def perceptron_classification(X_train, y_train, X_test):
model = Perceptron()
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([0, 1, 0])
X_test = np.array([[2, 2]])
print(perceptron_classification(X_train, y_train, X_test))

Implements a Perceptron classifier for binary classification.


85. Compute

import numpy as np
from sklearn.metrics import silhouette_score
from sklearn.cluster import KMeans

def compute_silhouette_score(data, labels):
return silhouette_score(data, labels)

data = np.array([[1, 2], [2, 3], [3, 4], [5, 6]])
model = KMeans(n_clusters=2)
labels = model.fit_predict(data)
print(compute_silhouette_score(data, labels))

Computes the Silhouette Score for cluster evaluation.

86. Generate a Scatter Plot with Regression Line

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

def scatter_plot_with_regression(X, y):
model = LinearRegression()
model.fit(X, y)
predictions = model.predict(X)

plt.scatter(X, y, color='blue')
plt.plot(X, predictions, color='red')
plt.title('Scatter Plot with Regression Line')
plt.show()

X = np.array([[1], [2], [3], [4]])
y = np.array([1, 2, 2.5, 4])
scatter_plot_with_regression(X, y)

Generates a scatter plot with a regression line.


87. Implement a Simple Time Series Analysis

import numpy as np
import pandas as pd

def simple_time_series_analysis(data):
series = pd.Series(data)
return series.rolling(window=3).mean()

data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
print(simple_time_series_analysis(data))

Performs a simple time series analysis with rolling mean.


88. Create a Pair Plot

import seaborn as sns
import pandas as pd

def create_pair_plot(data):
df = pd.DataFrame(data, columns=['A', 'B'])
sns.pairplot(df)
plt.title('Pair Plot')
plt.show()

data = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
create_pair_plot(data)

Generates a pair plot to visualize relationships between features.


89. Implement Gradient Descent for Linear Regression

import numpy as np

def gradient_descent(X, y, learning_rate, iterations):
m, n = X.shape
theta = np.zeros(n)
for _ in range(iterations):
predictions = X.dot(theta)
errors = predictions - y
gradient = (1/m) * X.T.dot(errors)
theta -= learning_rate * gradient
return theta

X = np.array([[1, 2], [2, 3], [3, 4]])
y = np.array([2, 3, 4])
print(gradient_descent(X, y, learning_rate=0.01, iterations=1000))

Implements Gradient Descent for optimizing linear regression.


90. Calculate the Mahalanobis Distance

import numpy as np
from scipy.spatial import distance

def mahalanobis_distance(x, y, inv_cov_matrix):
return distance.mahalanobis(x, y, inv_cov_matrix)

x = np.array([1, 2])
y = np.array([2, 3])
cov_matrix = np.cov(np.vstack([x, y]), rowvar=False)
inv_cov_matrix = np.linalg.inv(cov_matrix)
print(mahalanobis_distance(x, y, inv_cov_matrix))

Calculates the Mahalanobis distance between two points.


91. Perform Singular Value Decomposition (SVD)

import numpy as np
from scipy.linalg import svd

def perform_svd(matrix):
U, S, V = svd(matrix)
return U, S, V

matrix = np.array([[1, 2], [3, 4]])
U, S, V = perform_svd(matrix)
print(U, S, V)

Performs Singular Value Decomposition (SVD) on a matrix.


92. Implement Principal Component Analysis (PCA) from Scratch

import numpy as np

def pca_scratch(X, num_components):
mean = np.mean(X, axis=0)
X_centered = X - mean
covariance_matrix = np.cov(X_centered, rowvar=False)
eigenvalues, eigenvectors = np.linalg.eigh(covariance_matrix)
sorted_indices = np.argsort(eigenvalues)[::-1]
top_eigenvectors = eigenvectors[:, sorted_indices[:num_components]]
return X_centered.dot(top_eigenvectors)

X = np.array([[1, 2], [2, 3], [3, 4]])
print(pca_scratch(X, 1))

Implements PCA from scratch.


93. Generate a Heatmap

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

def generate_heatmap(data):
sns.heatmap(data, cmap='coolwarm')
plt.title('Heatmap')
plt.show()

data = np.random.rand(10, 10)
generate_heatmap(data)

Generates and visualizes a heatmap of data.


94. Implement a Simple K-Fold Cross-Validation

import numpy as np
from sklearn.model_selection import KFold
from sklearn.linear_model import LinearRegression

def k_fold_cross_validation(X, y, k):
kf = KFold(n_splits=k)
model = LinearRegression()
scores = []
for train_index, test_index in kf.split(X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
model.fit(X_train, y_train)
scores.append(model.score(X_test, y_test))
return np.mean(scores)

X = np.array([[1, 2], [2, 3], [3, 4], [4, 5]])
y = np.array([1, 2, 3, 4])
print(k_fold_cross_validation(X, y, 2))

Implements K-Fold Cross-Validation for model evaluation.


95. Compute the Adjusted R-squared Value

import numpy as np
from sklearn.metrics import r2_score

def adjusted_r_squared(y_true, y_pred, n, p):
r2 = r2_score(y_true, y_pred)
return 1 - (1 - r2) * (n - 1) / (n - p - 1)

y_true = np.array([1, 2, 3, 4])
y_pred = np.array([1.1, 2.0, 2.9, 4.1])
n = len(y_true)
p = 1
print(adjusted_r_squared(y_true, y_pred, n, p))

Computes the Adjusted R-squared value to account for the number of predictors.


96. Generate and Plot a Time Series

import numpy as np
import matplotlib.pyplot as plt

def generate_time_series(n, trend=0.1, noise=1.0):
time = np.arange(n)
data = trend * time + noise * np.random.randn(n)
plt.plot(time, data)
plt.title('Time Series Plot')
plt.show()
return data

data = generate_time_series(100)

Generates and plots a time series with trend and noise.


97. Create a 3D Surface Plot

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def create_surface_plot(X, Y, Z):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.title('3D Surface Plot')
plt.show()

X = np.linspace(-5, 5, 100)
Y = np.linspace(-5, 5, 100)
X, Y = np.meshgrid(X, Y)
Z = np.sin(np.sqrt(X**2 + Y**2))
create_surface_plot(X, Y, Z)

Generates and visualizes a 3D surface plot.


98. Perform Data Normalization

import numpy as np
from sklearn.preprocessing import MinMaxScaler

def normalize_data(data):
scaler = MinMaxScaler()
return scaler.fit_transform(data)

data = np.array([[1, 2], [2, 3], [3, 4]])
print(normalize_data(data))

Normalizes data to a range between 0 and 1.


99. Implement Ridge Regression

import numpy as np
from sklearn.linear_model import Ridge

def ridge_regression(X_train, y_train, X_test, alpha):
model = Ridge(alpha=alpha)
model.fit(X_train, y_train)
return model.predict(X_test)

X_train = np.array([[1, 2], [2, 3], [3, 4]])
y_train = np.array([2, 3, 4])
X_test = np.array([[2, 2]])
print(ridge_regression(X_train, y_train, X_test, alpha=1.0))

Implements Ridge Regression for regularized linear regression.


100. Perform Polynomial Regression

import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression

def polynomial_regression(X, y, degree):
poly = PolynomialFeatures(degree)
X_poly = poly.fit_transform(X)
model = LinearRegression()
model.fit(X_poly, y)
return model.predict(X_poly)

X = np.array([[1], [2], [3], [4]])
y = np.array([1, 4, 9, 16])
print(polynomial_regression(X, y, degree=2))

Performs Polynomial Regression to fit data with polynomial terms.

This collection of 100 Python scripts for beginners is designed to enhance your understanding of data handling with practical examples.

Post Comment