1 - All Python Codes + Neo4j Samples
1 - All Python Codes + Neo4j Samples
T=[
['<=30', 'high', 'n', 'fair','n'],
['<=30', 'high', 'n', 'exe', 'n'],
['3140', 'high', 'n', 'fair', 'y'],
['40', 'medium', 'n', 'fair', 'y'],
['40', 'w', 'y', 'fair', 'y'],
['40', 'w', 'y', 'exe', 'n'],
['3140', 'w', 'y', 'exe', 'y'],
['<=30', 'medium', 'n', 'fair', 'n'],
['<=30', 'w', 'y', 'fair', 'y'],
['40', 'medium', 'y', 'fair', 'y'],
['<=30', 'medium', 'y', 'exe', 'y'],
['3140', 'medium', 'n', 'exe', 'y'],
['3140', 'high', 'y', 'fair', 'y'],
['40', 'medium', 'n', 'exe', 'n']
]
y=0
n=0
for record in T:
y += record[4].count('y')
for record in T:
n += record[4].count('n')
x1 = 0
for p1 in T:
x1 += p1[0].count('<=30') and p1[4].count('y')
y1 = 0
for p1 in T:
y1 += p1[0].count('<=30') and p1[4].count('n')
x2 = 0
for p1 in T:
x2 += p1[0].count('3140') and p1[4].count('y')
y2 = 0
for p1 in T:
y2 += p1[0].count('3140') and p1[4].count('n')
x3 = 0
for p1 in T:
x3 += p1[0].count('40') and p1[4].count('y')
y3 = 0
for p1 in T:
y3 += p1[0].count('40') and p1[4].count('n')
x4 = 0
for p1 in T:
x4 += p1[1].count('high') and p1[4].count('y')
y4 = 0
for p1 in T:
y4 += p1[1].count('high') and p1[4].count('n')
x5 = 0
for p1 in T:
x5 += p1[1].count('medium') and p1[4].count('y')
y5 = 0
for p1 in T:
y5 += p1[1].count('medium') and p1[4].count('n')
x6 = 0
for p1 in T:
x6 += p1[1].count('w') and p1[4].count('y')
y6 = 0
for p1 in T:
y6 += p1[1].count('w') and p1[4].count('n')
x7 = 0
for p1 in T:
x7 += p1[2].count('n') and p1[4].count('y')
y7 = 0
for p1 in T:
y7 += p1[2].count('n') and p1[4].count('n')
x8 = 0
for p1 in T:
x8 += p1[2].count('y') and p1[4].count('y')
y8 = 0
for p1 in T:
y8 += p1[2].count('y') and p1[4].count('n')
x9 = 0
for p1 in T:
x9 += p1[3].count('fair') and p1[4].count('y')
y9 = 0
for p1 in T:
y9 += p1[3].count('fair') and p1[4].count('n')
x10 = 0
for p1 in T:
x10 += p1[3].count('exe') and p1[4].count('y')
y10 = 0
for p1 in T:
y10 += p1[3].count('exe') and p1[4].count('n')
print('\nfor yes')
print (yes)
print('\nfor no')
print (nn)
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 4].values
y_pred = classifier.predict(X_test)
PERCEPTRONS CODE-1
import numpy as np
class Perceptron(object):
def __init__(self, no_of_inputs, epoch=20, learning_rate=0.01):
self.epoch = epoch
self.learning_rate = learning_rate
self.weights = np.zeros(no_of_inputs + 1)
training_inputs = []
training_inputs.append(np.array([1, 1]))
training_inputs.append(np.array([1, 0]))
training_inputs.append(np.array([0, 1]))
training_inputs.append(np.array([0, 0]))
PERCEPTRONS CODE-2
import numpy as np
class Perceptron(object):
self.threshold = threshold
self.learning_rate = learning_rate
self.weights = np.zeros(no_of_inputs + 1)
if summation > 0:
activation = 1
else:
activation = 0
return activation
def train(self, training_inputs, labels):
for _ in range(self.threshold):
prediction = self.predict(inputs)
class Knapsack(object):
self.C = 0
self.weights = []
self.profits = []
self.opt = []
self.parents = []
self.newparents = []
self.bests = []
self.best_p = []
self.iterated = 1
self.population = 0
for i in range(self.population):
parent = []
for k in range(0, 5):
k = random.randint(0, 1)
parent.append(k)
self.parents.append(parent)
self.weights = weights
self.profits = profits
self.opt = opt
self.C = C
self.population = population
self.initialize()
sum_w = 0
sum_p = 0
# run generations of GA
def evaluation(self):
for i in range(len(ch)):
k = random.uniform(0, 1)
if k > 0.5:
#if random float number greater that 0.5 flip 0 with
1 and vice versa
if ch[i] == 1:
ch[i] = 0
else:
ch[i] = 1
return ch
if self.opt in newparents:
print ("optimal found in {} generations"
.format(self.iterated))
else:
self.iterated += 1
print("recreate generations for {} time"
.format(self.iterated))
self.parents = newparents
self.bests = []
self.best_p = []
self.run()
k = Knapsack()
k.properties(weights, profits, opt, C, population)
k.run()
def fitness(chromosome):
horizontal_collisions = sum([chromosome.count(queen)-1 for queen in
chromosome])/2
diagonal_collisions = 0
n = len(chromosome)
left_diagonal = [0] * 2*n
right_diagonal = [0] * 2*n
for i in range(n):
left_diagonal[i + chromosome[i] - 1] += 1
right_diagonal[len(chromosome) - i + chromosome[i] - 2] += 1
diagonal_collisions = 0
for i in range(2*n-1):
counter = 0
if left_diagonal[i] > 1:
counter += left_diagonal[i]-1
if right_diagonal[i] > 1:
counter += right_diagonal[i]-1
diagonal_collisions += counter / (n-abs(i-n+1))
def print_chromosome(chrom):
print("Chromosome = {}, Fitness = {}"
.format(str(chrom), fitness(chrom)))
if __name__ == "__main__":
nq = int(input("Enter Number of Queens: ")) #say N = 8
maxFitness = (nq*(nq-1))/2 # 8*7/2 = 28
population = [random_chromosome(nq) for _ in range(100)]
generation = 1
board = []
for x in range(nq):
board.append(["x"] * nq)
for i in range(nq):
board[nq-chrom_out[i]][i]="Q"
def print_board(board):
for row in board:
print (" ".join(row))
print()
print_board(board)
Neo4j Sample Codeser
MATCH (A:Person)-[*3]->(b) WHERE A.name=”Alice” RETURN b
RETURN ee,js,ir,rvb,ally
(js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb),
(ir)-[:BOSS]->(js),(ir)-[:FRIEND]->(ally),
(rvb)-[:BROTHER]->(ally)
RETURN ee,js,ir,rvb,ally
MATCH (js:Person)-[:KNOWS]-()-[:KNOWS]-(surfer)
MATCH (js:Person)-[:KNOWS]-()-[:FRIEND]-(surfer)