Giter VIP home page Giter VIP logo

pyml's Introduction

PyML

Build Status Latest Version

Machine Learning algorithms in Python

Installation

$ pip install PyML

Contribution

Feel free to raise issues or add new features. A lot of work can be done on this project!

Usage

The first column of the training set x must be all 1s.

The following code is assumed in all the examples.

import numpy as np
import pyml

Cost

list_x = [[1, 1, 100], [1, 2, 101], [1, 3, 102]]
list_theta = [[0], [1], [0]]
list_y = [[1], [2], [3]]

x = np.matrix(list_x)
theta = np.matrix(list_theta)
y = np.matrix(list_y)

cost = pyml.linear_regression_cost(x, y, theta)
print('Cost: ', cost)
Cost:  0.0

Feature normalization

list_x = [[1, 1, 100], [1, 2, 101], [1, 3, 102]]
x = np.matrix(list_x)

(x, mu, sigma) = pyml.normalize_features(x)
print('Normalized x: ', x)
Normalized x:  [[ 1.         -1.22474487 -1.22474487]
 [ 1.          0.          0.        ]
 [ 1.          1.22474487  1.22474487]]

File operations

# load(file, sep=',')
# save(file, x, mode='w', sep=',')

x = pyml.load('data1')
pyml.save('data2', x)
y = pyml.load('data2')
print((x == y).all())
True

Get theta

list_x = [[1, 1], [1, 2], [1, 3]]
list_y = [[10], [20], [30]]

x = np.matrix(list_x)
y = np.matrix(list_y)

(x, mu, sigma) = pyml.normalize_features(x)

theta = pyml.get_theta(x, y)
print('Theta: ', theta)
Theta: [[ 20.        ]
 [  8.16496581]]

Gradient Descent

list_x = [[1, 1], [1, 2], [1, 3]]
list_theta = [[0], [0]]
list_y = [[10], [20], [30]]

x = np.matrix(list_x)
theta = np.matrix(list_theta)
y = np.matrix(list_y)

(x, mu, sigma) = pyml.normalize_features(x)

alpha = 0.03
num_iters = 2000

theta = pyml.gradient_descent(x, y, theta, alpha, num_iters)
print('Theta: ', theta)
Theta:  [[ 20.        ]
 [  8.16496581]]

Note: gradient_descent_history(x, y, theta, alpha, num_iters) returns a tuple in which the first element is theta and the second element is a list with cost history from all the iterations.

Prediction

list_x = [[1, 1], [1, 2], [1, 3]]
list_theta = [[0], [0]]
list_y = [[10], [20], [30]]

x = np.matrix(list_x)
theta = np.matrix(list_theta)
y = np.matrix(list_y)

(x, mu, sigma) = pyml.normalize_features(x)

alpha = 0.03
num_iters = 2000

theta = pyml.gradient_descent(x, y, theta, alpha, num_iters)

list_vals = [[1, 4], [1, 5]]
vals = np.matrix(list_vals)
predictions = pyml.predict(vals, theta, mu, sigma)
print('Predictions: ', predictions)
Predictions:  [[ 40.]
 [ 50.]]

Sigmoid

list_y = [[10], [20], [30]]
s = pyml.sigmoid(y)
print('Sigmoid: ', s)

print('Sigmoid: ', list(map(pyml.sigmoid, [float('-inf'), -1, 0, 1, float('inf')])))
Sigmoid:  [[ 0.9999546  1.         1.       ]]    # Returns np.matrix for iterables
Sigmoid:  [0.0, 0.2689414213699951, 0.5, 0.7310585786300049, 1.0]    # Returns float for int/float

pyml's People

Contributors

rohithpr avatar

Watchers

Meghana  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.