Skip to main content

A python library for implementation of some Data Science topics.

Project description

Super Cool And Totally Working Data Science Library (SCTW)

This repository contains a collection of personal machine learning tools and algorithms developed for educational purposes. This is not a professional project and is not intended to be used as such. The code here is intended to provide basic implementations of some common machine learning algorithms and utilities.

Overview

This project includes the following components:

  • Linear Regression
  • Multivariate Linear Regression
  • Polynomial Regression
  • Train-Test Split
  • Data Generation Function
  • Data Cleaner Class
  • Neural Network and Layer Classes

Linear Regression

A simple linear regression implementation that fits a line to the given data points.

Usage

from SCTW_DS.Regression import LinearRegression

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

model = LinearRegression(x, y)
predictions = model.predict([6, 7, 8])
print(predictions)  # Output: [12.0, 14.0, 16.0]

Multivariate Linear Regression

An implementation of multivariate linear regression that fits a hyperplane to the given data points.

Usage

from SCTW_DS.Regression import MultivariateLinearRegression

X = [[1, 2], [2, 3], [3, 4], [4, 5]]
Y = [[2], [3], [4], [5]]

model = MultivariateLinearRegression(X, Y)
predictions = model.predict([[5, 6], [6, 7]])
print(predictions)  # Output: [[5.999999999999999], [6.999999999999999]]

Polynomial Regression

An implementation of polynomial regression that fits a polynomial curve to the given data points.

Usage

from SCTW_DS.Regression import PolynomialRegression

x = [1, 2, 3, 4, 5]
y = [1, 8, 27, 64, 125]

model = PolynomialRegression(x, y, degree=3)
predictions = model.predict([6, 7, 8])
print(predictions)  # Output: [216.0, 343.0, 512.0]

Train-Test Split

A utility function that splits a dataset into training and test sets.

Usage

import pandas as pd
from SCTW_DS.Utils import trainTestSplit

df = pd.read_csv('your_dataset.csv')
train_data, test_data = trainTestSplit(df, ratio=0.8)
print(len(train_data), len(test_data))  # Output: 80 20 (if there are 100 samples)

Data Generation Function

A function to generate synthetic datasets for testing machine learning algorithms.

Usage

from your_module import generateDataset

def func1(low, high, size):
    return np.random.uniform(low=low, high=high, size=size)

def pois_func(arr):
    return ((arr[1] ** 2 + arr[0] ** 2) > 50**2).astype(float)

generateDataset(
    name="data.csv",
    num_input_variables=2,
    x_funcs=[func1, func1],
    x_func_params=[(0, 100, 1000), (0, 100, 1000)],
    y_func=pois_func,
    balance_dataset=True,
)

dataCleaner

dataCleaner is a Python class designed to help clean and preprocess datasets for better model training and evaluation. It provides functionalities for handling duplicate observations, missing values, and outliers in your training and testing datasets.

Usage

from SCTW_DS.Utils import dataCleaner  

# Sample data
train_df = pd.DataFrame({
    'A': [1, 2, 2, 4, np.nan],
    'B': [5, np.nan, np.nan, 8, 10],
    'C': [11, 12, 13, 14, 15]
})

test_df = pd.DataFrame({
    'A': [np.nan, 2, 3, 4, 5],
    'B': [5, 6, np.nan, 8, 10],
    'C': [15, 14, 13, 12, 11]
})

# Initialize dataCleaner
cleaner = dataCleaner(train_df, test_df)

# Show duplicate observations
duplicates = cleaner.show_duplicate_observations()
print(duplicates)

# Remove duplicate observations
cleaner.remove_duplicate_observations()

# Show missing values
missing_values = cleaner.show_missing_values()
print(missing_values)

# Fix missing values in column 'A' using the 'Mean' strategy
cleaner.fix_missing_values(feature='A', strategy='Mean')

# Detect outliers in column 'A' using the inter-quartile range strategy
outliers = cleaner.outlier_detection(feature='A')
print(outliers)

NeuralNetwork and Layer

Together form a simple sequential neural network.

Usage

import numpy as np
import pandas as pd

from SCTW_DS.NeuralNetwork import NeuralNetwork
from SCTW_DS.NeuralNetwork import Layer
from SCTW_DS.Utils import trainTestSplit

# Load the dataset
df = pd.read_csv("data.csv")

# Split the dataset into training and test data points
train, test = trainTestSplit(df, 0.8)

# Define the layers of the neural network
layers = [
    Layer(2, 3, "reLU"),
    Layer(3, 5, "reLU"),
    Layer(5, 3, "reLU"),
    Layer(3, 2, "sigmoid", "mse"),
]

# Initialize the neural network
nn = NeuralNetwork(layers)

# Train the neural network
costs = nn.train(
    iterations=10000,
    data_points=training_data,
    learning_rate=0.01,
    batch_size=50,
    momentum=0.9,
)

model_version = 1
# Save the model
nn.save_model(f"model_{model_version}.pkl")

# Plot the cost over iterations
plt.plot(costs)
plt.xlabel("Iteration")
plt.ylabel("Cost")
plt.title("Cost over Iterations")
plt.show()

# Load the model
nn_loaded = NeuralNetwork.load_model(f"model_{model_version}.pkl")

# Verify loaded model
y_pred = nn_loaded.classify(test)

# Calculate accuracy
y_true = [np.argmax(point.expected_outputs) for point in test]
accuracy = sum(y1 == y2 for y1, y2 in zip(y_true, y_pred)) / len(y_true)

print(f"Accuracy: {accuracy * 100:.2f}%")

Note

This project is a personal endeavor and is not intended to be a professional or production-ready implementation. The code is for educational purposes only and may not follow best practices in software development or machine learning.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Installation

You can install the required dependencies using pip:

pip install SCTW-DS

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sctw_ds-0.2.3.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

SCTW_DS-0.2.3-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file sctw_ds-0.2.3.tar.gz.

File metadata

  • Download URL: sctw_ds-0.2.3.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for sctw_ds-0.2.3.tar.gz
Algorithm Hash digest
SHA256 bd962a87b086ad5a0cc662dd056e845c051865e9d0a89eee9818fe98645815ef
MD5 b5cadc653ac8f440cc3efc4311bc5913
BLAKE2b-256 ba46dc3ba27c923ec148bbbd09a77137dfb69bf4c403c0bbaafbc679c415531f

See more details on using hashes here.

File details

Details for the file SCTW_DS-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: SCTW_DS-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for SCTW_DS-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ec0d0702b7fb5d8a98415ed45a90000a2ca91c3c370c3062ac3e004c01b1b4b7
MD5 ef8c7e338dc2dcae77a71ef7961b69cd
BLAKE2b-256 de3503c3d0cdd4f9f467fcc15d7efa04d6fc8902bedcce0ba396efd4bf2d6af4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page