I am using a GridDB container and the database has 3 columns, with the number of rows as a multiple of 3: say 369 rows. All the columns have numerical features of values from -1 to 1. I need to get all the rows converted to matrices of 3 rows each.
The function should return a generator of all the matrices as NumPy arrays. The code below is for my database container and where I select rows using SQL queries:
import griddb_python as griddb
import pandas as pd
import numpy as np
from pprint import pprint
factory = griddb.StoreFactory.get_instance()
# Initialize container
try:
gridstore = factory.get_store(host="127.0.0.1", port="8080",
cluster_name="ClsWeb", username="root",
password="")
conInfo = griddb.ContainerInfo("Websites",
[["shortened", griddb.Type.INTEGER],
["rank",griddb.Type.STRING],
["rcDisabled", griddb.Type.STRING]
],
griddb.ContainerType.COLLECTION, True)
cont = gridstore.put_container(conInfo)
data = pd.read_csv("websites.csv")
#Add data
for i in range(len(data)):
ret = cont.put(data.iloc[i, :])
print("Data has been added successfully")
except griddb.GSException as e:
print(e)
sql_statement = ('SELECT * FROM Websites')
sql_query = pd.read_sql_query(sql_statement, cont)
For instance, here are 6 rows from the database:
Code:
shortened rank rcDisabled
0 1 0 1
1 1 1 1
2 0 -1 0
3 -1 1 1
4 -1 -1 1
5 1 0 1
I need a function that returns a NumPy array containing all the matrices, like this:
Code:
#code
def doSomething(database):
…
return array
arrayOfMatrices = doSomething(db)
pprint(f‛Matrices: {arrayOfMatrices}‛)
The function above should return something like this, without the headers or column names, whichshould be 123 matrices in total:
Code:
Matrices: [[[1, 0, 1],
[1, 1, 1],
[0, -1, 0]],
[[-1, 1, 1],
[-1, -1, 1],
[1, 0, 1]]]
Then, I should be able to iterate through the array and get the matrices as a generator:
Code:
for matrix in arrayOfMatrices:
print(matrix)