#import packages import numpy as np import matplotlib.pyplot as plt import pandas as pd #create dataset using DataFrame df=pd.DataFrame({'X':[0.1,0.15,0.08,0.16,0.2,0.25,0.24,0.3], 'y':[0.6,0.71,0.9,0.85,0.3,0.5,0.1,0.2]}) f1 = df['X'].values f2 = df['y'].values X = np.array(list(zip(f1, f2))) print(X) #centroid points C_x=np.array([0.1,0.3]) C_y=np.array([0.6,0.2]) centroids=C_x,C_y #plot the given points colmap = {1: 'r', 2: 'b'} plt.scatter(f1, f2, color='k') plt.show() #for i in centroids(): plt.scatter(C_x[0],C_y[0], color=colmap[1]) plt.scatter(C_x[1],C_y[1], color=colmap[2]) plt.show() C = np.array(list((C_x, C_y)), dtype=np.float32) print (C) #plot given elements with centroid elements plt.scatter(f1, f2, c='#050505') plt.scatter(C_x[0], C_y[0], marker='*', s=200, c='r') plt.scatter(C_x[1], C_y[1], marker='*', s=200, c='b') plt.show() #import KMeans class and create object of it from sklearn.cluster import KMeans model=KMeans(n_clusters=2,random_state=0) model.fit(X) labels=model.labels_ print(labels) #using labels find population around centroid count=0 for i in range(len(labels)): if (labels[i]==1): count=count+1 print('No of population around cluster 2:',count-1) #Find new centroids new_centroids = model.cluster_centers_ print('Previous value of m1 and m2 is:') print('M1==',centroids[0]) print('M1==',centroids[1]) print('updated value of m1 and m2 is:') print('M1==',new_centroids[0]) print('M1==',new_centroids[1]) updated value of m1 and m2 is: M1== [0.2475 0.275 ] M1== [0.1225 0.765 ]
import operator class Employee: empId = 0 name = "Divya" age = 22 department = "Finance" def __init__(self, empId, name, age, department): self.empId = empId self.name = name self.age = age self.department = department employee1 = Employee(10, "Arjun", 32, "Finance") employee2 = Employee(5, "Swetha", 29, "HR") employee3 = Employee(3, "Rithika", 29, "HR") employee4 = Employee(7, "Sakthi", 30, "Sales") employee5 = Employee(4, "Rakshitha", 40, "Finance") employee6 = Employee(1, "Raj", 43, "Production") employee7 = Employee(2, "Ravi", 45, "Sales") employee8 = Employee(6, "Radha", 42, "Finance") employee9 = Employee(8, "Priya", 32, "HR") employee10 = Employee(9, "Ramya", 32, "HR") store = {} def display(list_of_employees): for emp in list_of_employees: store[emp.empId] = (emp.name, emp.age, emp.department) display([employee1, employee2, employee3, employee4, employee5, employee6, employee7, employee8, employee9, employee10]) empDict = sorted(store.items(),reverse=True) values=[] for key,val in empDict: values=list(val) print "EmpId= ", str(key), ",Name= ",values[0], ",Age= ",values[1], ",Department= ",values[2]
# Hello World program in Python from math import * def LagrangePol (datos): def L(k, x): out= 1.0 for i,p in enumerate(datos): if i != k: out *= (x-p[0])/(datos[k][0]-p[0]) return out def P(x): lag = 0.0 for k, p in enumerate(datos) : lag += p[1]*L(k, x) return lag return P datosf =[(2.0,1.0/2.0), (11.0/4.0, 4.0/11.0), (4.0, 1.0/4.0)] Pf = LagrangePol(datosf) j =3.0 while j>0.0: if j>0: print "\n"+r"-- Polinomio de Lagrange en x=:"+str(j)+"\n" print Pf(j) j=j-0.1
# Hello World program in Python from collections import OrderedDict matrix = [[11, 12, 5, 2], [15, 6, 10, 8], [10, 8, 12, 5], [12,15,8,6]] bank = {} for x in range(len(matrix)): for y in range(len(matrix[x])): harrisValue = matrix[x][y] print "x: %d, y: %d is %d" % (x, y, harrisValue) if harrisValue in bank: bank[harrisValue].append([x, y]) else: bank[harrisValue] = [[x, y]] top3HarrisValues = bank.keys()[-3:] # print(top3HarrisValues) topCoord = {} for key in reversed(top3HarrisValues): topCoord[key] = bank[key] for key in topCoord: print key print bank[key]
#!/usr/bin/python str = 'Hello World!' print str print str[0] print str[2:5] print str[2:] print str * 2 print str + "TEST"
#!/usr/bin/python tuple = ( 'abcd', 786, 2.23, 'jonh', 70.2 ) tinytuple = (123, 'jonh') print tuple # Prints complete list print tuple[0] # Prints first element of the list print tuple[1:3] # Prints elements starting from 2nd till 3rd print tuple[2:] # Prints elements starting from 3rd element print tuple * 2 # Prints list two times print tuple + tinytuple # Prints concatenated lists
tuple = ('abcd', 786, 2.23,'john',70.2) tinytuple = (123, 'john') print tuple print tuple [0] print tuple [1:3] print tuple [2:] print tinytuple *2 print tuple + tinytuple
#!/usr/bin/python tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) tinytuple = (123, 'john') print tuple # Prints complete list print tuple[0] # Prints first element of the list print tuple[1:3] # Prints elements starting from 2nd till 3rd print tuple[2:] # Prints elements starting from 3rd element print tuple * 2 # Prints list two times print tuple + tinytuple # Prints concatenated lists
example_input="John is connected to Bryant, Debra, Walter.\ John likes to play The Movie: The Game, The Legend of Corgi, Dinosaur Diner.\ Bryant is connected to Olive, Ollie, Freda, Mercedes.\ Bryant likes to play City Comptroller: The Fiscal Dilemma, Super Mushroom Man.\ Mercedes is connected to Walter, Robin, Bryant.\ Mercedes likes to play The Legend of Corgi, Pirates in Java Island, Seahorse Adventures.\ Olive is connected to John, Ollie.\ Olive likes to play The Legend of Corgi, Starfleet Commander.\ Debra is connected to Walter, Levi, Jennie, Robin.\ Debra likes to play Seven Schemers, Pirates in Java Island, Dwarves and Swords.\ Walter is connected to John, Levi, Bryant.\ Walter likes to play Seahorse Adventures, Ninja Hamsters, Super Mushroom Man.\ Levi is connected to Ollie, John, Walter.\ Levi likes to play The Legend of Corgi, Seven Schemers, City Comptroller: The Fiscal Dilemma.\ Ollie is connected to Mercedes, Freda, Bryant.\ Ollie likes to play Call of Arms, Dwarves and Swords, The Movie: The Game.\ Jennie is connected to Levi, John, Freda, Robin.\ Jennie likes to play Super Mushroom Man, Dinosaur Diner, Call of Arms.\ Robin is connected to Ollie.\ Robin likes to play Call of Arms, Dwarves and Swords.\ Freda is connected to Olive, John, Debra, Thor.\ Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures." ##########==========Making the social network structure==========########## def create_data_structure(string_input): if string_input == '': return {} if string_input == ' ': return {} network = [] buffer = [] social_network = {} span = 2 breakdown = string_input.split('.') breakdown = ['.'.join(breakdown[i:i + span]) for i in range(0, len(breakdown), span)] for e in breakdown: e = e.split('.', 1) network.append(e) network.remove(network[-1]) for f in network: g = f[0] g = g.split() social_network[g[0]] = f network = [] return social_network ##########==========Getting a list of who is connect to whom==========########## def get_connections(network, user): connections = [] existance = user.split() for word in existance: if network.has_key(word): connections = network[word][0].split(', ') first = connections.pop(0) first = first.split() connections.append(first[-1]) if connections == ['to']: connections = [] return connections #return connections ##########==========Getting a list games a user likes==========########## def get_games_liked(network,user): games_liked = [] existance = user.split() for word in existance: if network.has_key(word): games_liked = network[word][-1].split(', ') games_liked[0] = games_liked[0].split() games_liked[0].remove(games_liked[0][0]) if games_liked == [['likes', 'to', 'play']]: return [] if games_liked[0][0] == 'likes': games_liked[0].remove(games_liked[0][0]) if games_liked[0][0] == 'to': games_liked[0].remove(games_liked[0][0]) if games_liked[0][0] == 'play': games_liked[0].remove(games_liked[0][0]) games_liked[0] = ' '.join(games_liked[0]) return games_liked ##########==========Adds a connection from one person to another==========########## def add_connection(network, user_A, user_B): apeople = get_connections(network, user_A) bperson = get_connections(network, user_B) bpeople = user_B.split() if apeople == None: return False if bperson == None: return False else: for word in bpeople: if network.has_key(word): if word in apeople: return 'network unchanged' else: apeople.append(word) toappend = user_A.split() for word in toappend: if network.has_key(word): tstfor1 = network[word][0] tstfor1 = tstfor1.split(' ', 1) if tstfor1[-1] == 'is connected to': network[word][0] = network[word][0] + ' ' + apeople[-1] else: network[word][0] = network[word][0] + ', ' + apeople[-1] return network ##########==========Adds a new user to the social network==========########## def add_new_user(network, user, games): usrchk = user.split() person = usrchk[0] if network.has_key(person): return 'Network Unchanged' games = ', '.join(games) network[person] = [('%s is connected to' % (person)), ('%s likes to play ' % (person) + games)] return network ##########==========Compiles a list of the users secondary connections==========########## def get_secondary_connections(network, user): person = user.split() seconds = [] primaries = get_connections(network, user) if primaries == None: return None for e in primaries: e = get_connections(network, e) for f in e: if f not in seconds and f not in person and f not in primaries: seconds.append(f) return seconds ##########==========Tallies the number of connections that each user has in common==========########## def count_common_connections(network, user_A, user_B): mancount = 0 agames = get_connections(network,user_A) bgames = get_connections(network,user_B) if agames == None: return False if bgames == None: return False for game in agames: if game in bgames: mancount = mancount + 1 return mancount ##########==========makes a path of people from one to a target==========########## def find_path_to_friend(network, user_A, user_B): path = [] user_A = user_A.split() for word in user_A: if word in network: user_A = word break else: return None user_B = user_B.split() for word in user_B: if word in network: user_B = word break else: return None path.append(user_A) aguys = get_connections(network, user_A) if user_B in aguys: path.append(user_B) else: for dude in aguys: if dude not in path: nxt = find_path_to_friend(network, dude, user_B) return path + nxt return path ##########==========potential friends==========########## ''' this procedure allows a user to input any game and see other people in the social network who like the same game. ''' def game_reccomendations(game, network): #starts with a blank list players = [] #iterates through each user in the network for dude in network: #gets games liked for each person gamelist = get_games_liked(network, dude) #appends player of the game indicated for title in gamelist: if title == game: players.append(dude) if players == []: return None return players ###==========Test code==========### ###==========Don't get rid of this, this is the network==========### social_network = create_data_structure(example_input) ###==========Don't get rid of this, this is the network==========### ''' #print social_network get_connections(social_network, 'Freda is happy Freda') get_games_liked(social_network, 'is John happy') add_connection(social_network, 'John is sad', 'Hello Levi') add_new_user(social_network, 'Thor is connected to Olive, John, Debra. Freda likes to play Starfleet Commander, Ninja Hamsters, Seahorse Adventures.', ['Ninja Hamsters', 'Super Mushroom Man', 'Dinosaur Diner']) get_secondary_connections(social_network, 'Here is Freda') count_common_connections(social_network, 'Olive likes this', 'Bryant man') find_path_to_friend(social_network, 'My name is John', 'Ollie oil') game_reccomendations('Mario', social_network) print [] ''' #udacity tests network = create_data_structure('') network = add_new_user(network, 'Alice', []) network = add_new_user(network, "Bob", []) network = add_connection(network, 'Alice', 'Bob') network = add_connection(network, 'Alice', 'Bob') print network #print get_connections(network, 'Alice')
prvi_dan =233 drugi_dan=316 treci_dan=534 najvise_krompira=max(prvi_dan, drugi_dan, treci_dan) print('Prvog dana je prodao krompira:', prvi_dan, 'kg') print('Drugog dana je prodao krompira:', drugi_dan, 'kg') print('Treceg dana je prodao krompira:', treci_dan, 'kg') print('Najvise krompira dnevno je: ', najvise_krompira, 'kg')
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more