This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func heapify(heap *[]int, i int) { | |
smallest := i | |
lChild := 2*i + 1 | |
rChild := 2*i + 2 | |
if lChild < len(*heap) && (*heap)[lChild] < (*heap)[smallest] { | |
smallest = lChild | |
} | |
if rChild < len(*heap) && (*heap)[rChild] < (*heap)[smallest] { | |
smallest = rChild |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"log" | |
"./pb" | |
"google.golang.org/grpc" | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == '__main__': | |
logging.basicConfig( | |
level=logging.INFO, | |
format='%(asctime)s - %(levelname)s - %(message)s', | |
) | |
server = grpc.server(ThreadPoolExecutor()) | |
add_WineClassifierServiceServicer_to_server(WineClassifierServer(), server) | |
port = 8080 | |
server.add_insecure_port(f'[::]:{port}') | |
server.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def GetWineVariety(self, request, context): | |
logging.info(request.review) | |
# transform review to document-term matrix | |
x_train_counts = self.count_vect.transform([request.review]) | |
tfidf_transformer = TfidfTransformer() | |
x_tfidf = tfidf_transformer.fit_transform(x_train_counts) | |
prediction = self.lsvc.predict(x_tfidf) | |
for variety, idx in self.top_10_varieties.items(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WineClassifierServer(WineClassifierServiceServicer): | |
def __init__(self): | |
wine_df = pd.read_csv('data/winemag-data-130k-v2.csv') | |
# map top 10 wine varieties to ints in wine_df | |
counter = Counter(wine_df['variety'].tolist()) | |
self.top_10_varieties = {i[0]: idx for idx, i in enumerate(counter.most_common(10))} | |
logging.info(self.top_10_varieties) | |
wine_df = wine_df[wine_df['variety'].map(lambda x: x in self.top_10_varieties)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# server tools & grpc | |
from concurrent.futures import ThreadPoolExecutor | |
from wine_varieties_pb2 import WineReviewResponse | |
from wine_varieties_pb2_grpc import WineClassifierServiceServicer, add_WineClassifierServiceServicer_to_server | |
import logging | |
import grpc | |
# model construction and selection | |
from sklearn.svm import LinearSVC | |
from sklearn.model_selection import train_test_split |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
syntax = "proto3"; | |
package pb; | |
option go_package = "./pb"; | |
message WineReviewRequest { | |
string review = 1; | |
} | |
message WineReviewResponse { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/sjwhitworth/golearn/base" | |
"github.com/sjwhitworth/golearn/evaluation" | |
"github.com/sjwhitworth/golearn/knn" | |
) | |
func main() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TrainModel(train dataframe.DataFrame, stream chan gomlBase.TextDatapoint) { | |
sentimentMap := map[string]int{ | |
"0": 0, | |
"1": 1, | |
} | |
for i := 0; i < train.Nrow(); i++ { | |
record := train.Subset(i).Records() | |
stream <- gomlBase.TextDatapoint{ | |
X: record[1][0], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
gomlBase "github.com/cdipaolo/goml/base" | |
"github.com/cdipaolo/goml/text" | |
"github.com/go-gota/gota/dataframe" | |
"log" | |
"math/rand" | |
"os" |