Skip to content

Instantly share code, notes, and snippets.

View rvndbalaji's full-sized avatar
👨‍💻
Changing the world, one commit at a time

Aravind Balaji rvndbalaji

👨‍💻
Changing the world, one commit at a time
View GitHub Profile
@rvndbalaji
rvndbalaji / langgraph_builder.py
Last active November 14, 2024 17:10
Utility class to build dynamic langgrah agents
import base64
import functools
import operator
from typing import *
from typing import Annotated, Any, Dict, List, Sequence
from dto.chat import *
from dto.graph import *
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.tools import BaseTool
# Given an incomplete JSON string, extract a subset JSON that is valid
# Obtain the maximum valid portion of this string
# by removing characters one by one from the end and checking the validity
# By default the incoming json is considered to be an object surrounded by {}
# Set is_list=True if incoming json is a list surrounded by []
# This method might be inefficient for now since it removes 1 character and validates
# TODO Future optimizaion -
# Matching blocks of {} for objects and removing block
#Using Keras
model = keras_model_sequential()
model %>%
layer_dense(units = 3, input_shape = c(5), activation = 'relu') %>%
layer_dense(units = 2, activation = 'relu') %>%
layer_dense(units = 1, activation = 'sigmoid')
model %>% compile(
loss = 'binary_crossentropy',
optimizer = optimizer_adam(),
#Install the pakages if you haven't already done so
install.packages('nnet')
nnet = neuralnet(Occupancy ~ Temperature + Humidity + Light + CO2 + HumidityRatio,data=train,linear.output = FALSE,hidden = c(3,2))
#Predicted values
pred = compute(nnet,test[,-6])
pred = pred$net.result
#From the graph, for all predictions greater than 0.7 1 else 0
sigmoid = function(z)
{
return(1/(1 + exp(-z)))
}
cost = function(T)
{
h = sigmoid(X%*%T)
m = nrow(X)
J = (1/m) * sum((-Y*log(h)) - (1-Y)*log(1-h))
model = glm(Occupancy~.,train,family= binomial(link = "logit")
pred = predict(model,test[,-6])
#All values with probability less than 0.7 are considered occupied.
pred[pred>=0.7] = 1
pred[pred<0.7] = 0
pred = factor(pred)
plot(pred)
#Calculate R-sqaured value
#Install the package, if you havn't already done so.
install.packages('psych')
#Randomply sample the data into two parts with replacement and probability
ind = sample(2,nrow(room_n),replace = TRUE,prob = c(0.8,0.2))
train = room_n[ind==1,]
test = room_n[ind==2,]
#Predictors
X = as.matrix(train[,-6])
#Feature Scaling & Mean Normalization
#This function is EXACTLY equivalent to the scale() function in R
//Remember to exclude the last column of the data, we should not normalize what we predict.
room_n = scale(room[1:5])
//Or all this function
normalize = function(x)
{
return ((x - mean(x))/sd(x))
library(psych)
roomtrain = read.table('datatraining.txt',header = TRUE,sep = ',',stringsAsFactors = FALSE)
roomtest1= read.table('datatest.txt',header = TRUE,sep = ',',stringsAsFactors = FALSE)
roomtest2= read.table('datatest2.txt',header = TRUE,sep = ',',stringsAsFactors = FALSE)
#Combine all three
room = rbind(roomtrain,roomtest1,roomtest2)
#Remove date and convert occupancy to factor
room = room[-1]
/* package codechef; // don't place package name! */
import java.util.Scanner;
import java.util.Stack;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{