项目作者: sercandogan

项目描述 :
Python client for redis-ml
高级语言: Python
项目地址: git://github.com/sercandogan/redis-ml-py.git
创建时间: 2018-09-06T17:55:13Z
项目社区:https://github.com/sercandogan/redis-ml-py

开源协议:MIT License

下载


redis-ml-py

It’s python client of Redis-ML.
It basically provide a basis for real-time machine learning apps.

Primary Algorithms of Client:

  • Linear Regression
  • Logistic Regression
  • Matrix Operations

Dependencies

  • redis and redis-ml module
  • numpy

How to use

Linear Regression

Coefficients’ keys and values are stored.

  1. from redisml import LinearRegression
  2. r = redis.StrictRedis(host='localhost', port=6379)
  3. model = LinearRegression('cars', r)
  4. # Model Coefficients
  5. coefficients = {
  6. "intercept": -22.657,
  7. "speed": 4.316
  8. }
  9. # Inputs to predict
  10. inputs = {
  11. "speed": 10
  12. }
  13. model.set(**coefficients)
  14. dist = model.predict(**inputs)
  15. print(dist)
  16. # 20.502999999999997

Logistic Regression

Default cut-off point is 0.5.
Coefficients’ keys and values are stored.

  1. from redisml import LogisticRegression
  2. r = redis.StrictRedis(host='localhost', port=6379)
  3. model = LogisticRegression('titanic', r)
  4. # Model Coefficients
  5. coefficients = {
  6. "intercept": 5.137627,
  7. "Pclass": -1.087156,
  8. "Sexmale": -2.756819,
  9. "Age": -0.037267,
  10. "SibSp": -0.292920
  11. }
  12. # Inputs to predict
  13. inputs = {
  14. "Pclass": 1,
  15. "Sexmale": 0,
  16. "Age": 24,
  17. "SibSp": 3
  18. }
  19. model.set(**coefficients)
  20. survived_or_not = model.predict(**inputs)
  21. print(survived_or_not)
  22. #0

Matrix Operations

First, let’s create a matrix

  1. from redisml import Matrix
  2. import numpy
  3. r = redis.StrictRedis(host='localhost', port=6379)
  4. matrix_1 = numpy.array(((1.23, 212.123, 3,), (4.10, 5, 6), (7, 8, 9)))
  5. a = Matrix('a', r)
  6. a.set(matrix_1)
  7. print(a.get())
  8. # [[ 1.23 212.123 3. ]
  9. # [ 4.1 5. 6. ]
  10. # [ 7. 8. 9. ]]

then let’s create another matrix and perform the operations

  1. matrix_2 = numpy.array(((9, 8, 7), (6, 5, 4), (3, 2, 1)))
  2. b = Matrix('b', r)
  3. b.set(matrix_2)

Adds two matrices

  1. c = Matrix('c', r)
  2. c.add(a, b) # adds two matrices
  3. print(c.get())
  4. # [[ 10.23 220.123 10. ]
  5. # [ 10.1 10. 10. ]
  6. # [ 10. 10. 10. ]]

Multilplies two matrices

  1. d = Matrix('d', r)
  2. d.multiply(b, c) # Multiplies the matrices
  3. print(d.get())
  4. # [[ 242.87 2131.107 240. ]
  5. # [ 151.88 1410.738 150. ]
  6. # [ 60.89 690.369 60. ]]

Scale matrix with scalar

  1. scalar = 3.14
  2. d.scale(scalar)
  3. print(d.get())
  4. # [[ 762.6118 6691.67598 753.6 ]
  5. # [ 476.9032 4429.71732 471. ]
  6. # [ 191.1946 2167.75866 188.4 ]]

TODO

  • K-Means Implementation
  • RandomForest Implementation