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 SCTW_DS.Utils 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.6.tar.gz (11.8 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.6-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sctw_ds-0.2.6.tar.gz
  • Upload date:
  • Size: 11.8 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.6.tar.gz
Algorithm Hash digest
SHA256 4eaa87ebe30ec8d5d65578ccb678904b4c72b0c494da66455cb62160209984e4
MD5 5b373ff72f9d6d04030665a18db5c35f
BLAKE2b-256 ca0d9d1cbdd156c4bbb569ac787b32e8d955181e01333e75cc55a2dbf20bdd26

See more details on using hashes here.

File details

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

File metadata

  • Download URL: SCTW_DS-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 11.4 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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 49ce6ab6099e92dcb6f50113f9e3e6b1c3fc8718a2104e9ab47eab83921d4029
MD5 03f9c57d57e3cd32d2846cc1621bd9d8
BLAKE2b-256 5ffbec0adb28dbc9f9774f7b67f4ee6cd2378c6ff57edee3159a1c75a4f3cff4

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