Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Mar 21 09:23:51 2018
  4.  
  5. @author: kazin
  6. """
  7.  
  8. import pyodbc as cn
  9. import numpy as np
  10. import pandas as pd
  11. from sklearn import preprocessing
  12.  
  13. #Connecting to database
  14. server = 'facil.database.windows.net'
  15. database = 'main'
  16. username = 'facildatabase'
  17. password = 'DifficultPassword69.'
  18. driver = '{ODBC Driver 11 for SQL Server}'
  19. cnxn = cn.connect('DRIVER=' + driver + ';SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password)
  20. print(cnxn)
  21.  
  22. ##Importing Data to dataframes form daatabase
  23. nutsComplete = pd.read_sql('SELECT * from dbo.NutsComplete', con = cnxn)
  24. boltsComplete = pd.read_sql('SELECT * from dbo.BoltsComplete', con = cnxn)
  25. contracts = pd.read_sql('SELECT * from dbo.contracts', con = cnxn)
  26. coatings = pd.read_sql('SELECT * from dbo.coatings', con = cnxn)
  27. geometryCoating = pd.read_sql('SELECT * from dbo.geometryCoating', con = cnxn)
  28. countryCodes = pd.read_sql('SELECT * from dbo.countryCodes', con = cnxn)
  29. rfqPortal = pd.read_sql('SELECT * from dbo.rfqPortal', con = cnxn)
  30.  
  31. ###Exporting dataframes to .csv files
  32. #nutsComplete.to_csv('nutsComplete.csv', sep='\t', encoding='utf-8')
  33. #boltsComplete.to_csv('boltsComplete.csv', sep= '\t', encoding='utf-8')
  34. #contracts.to_csv('contracts.csv', sep= '\t', encoding='utf-8')
  35. #coatings.to_csv('coatings.csv', sep= '\t', encoding='utf-8')
  36. #geometryCoating.to_csv('geometryCoating.csv', sep= '\t', encoding='utf-8')
  37. #countryCodes.to_csv('countryCodes.csv', sep= '\t', encoding='utf-8')
  38.  
  39.  
  40. #KPI Calculation for boltsComplete
  41. headers = list(boltsComplete)
  42. KpiBolts =[]
  43. boltsComplete = boltsComplete.astype('float64')
  44.  
  45. for x in range(len(headers)):
  46. KpiBolts.append((boltsComplete['Pprice'].corr(boltsComplete[headers[x]].astype(float))))
  47.  
  48. labels = ['ColumnName','Value']
  49. KPIBolts = pd.DataFrame(np.column_stack([headers, KpiBolts]),
  50. columns=['ColumnName','Value'])
  51. ##Exporting the KPI calculation boltsComplete values into .csv
  52. KPIBolts.to_csv('KpiBolts.csv',sep=',',encoding='utf-8')
  53. #Replacing all the nan values with 0
  54. boltsComplete.fillna(0, inplace = True)
  55.  
  56. ##Normalizing the data
  57. scaler = preprocessing.MinMaxScaler(feature_range=(-1,1))
  58. scaled = scaler.fit_transform(boltsComplete)
  59. BoltsNormalizedDataFrame = pd.DataFrame(scaled,columns=headers)
  60.  
  61. #KPI Calculation for nutsComplete
  62. headers = list(nutsComplete)
  63. KpiNuts =[]
  64. nutsComplete['Has_Washer'] = nutsComplete['Has_Washer'].astype(int)
  65. nutsComplete = nutsComplete.astype('float64')
  66. for x in range(len(headers)):
  67. KpiNuts.append((nutsComplete['Pprice'].corr(nutsComplete[headers[x]].astype(float))))
  68.  
  69. labels = ['ColumnName','Value']
  70. KPIN = pd.DataFrame(np.column_stack([headers, KpiNuts]),
  71. columns=['ColumnName','Value'])
  72. ##Exporting the KPI calculation nutsComplete values into .csv
  73. KPIN.to_csv('KpiNuts.csv',sep=',',encoding='utf-8')
  74.  
  75. #Replacing all the nan values with 0
  76. nutsComplete.fillna(0, inplace = True)
  77. scaler = preprocessing.MinMaxScaler(feature_range=(-1,1))
  78. scaled = scaler.fit_transform(nutsComplete)
  79. NutsNormalizedDataFrame = pd.DataFrame(scaled, columns = headers)
  80.  
  81. #Creating matrix for most valuables KPI in Bolts DataFrame
  82. HeadDiameter_matrix = BoltsNormalizedDataFrame['HeadDiameter'].as_matrix()
  83. Length_matrix = BoltsNormalizedDataFrame['Length'].as_matrix()
  84. TotalLength_matrix = BoltsNormalizedDataFrame['TotalLength'].as_matrix()
  85. ShoulderDiameter_matrix = BoltsNormalizedDataFrame['ShoulderDiameter'].as_matrix()
  86. Weight_matrix = BoltsNormalizedDataFrame['Weight'].as_matrix()
  87. TEGWNE_matrix = BoltsNormalizedDataFrame['TEGWNE'].as_matrix()
  88.  
  89. #Creating matrix for most valuables KPI in Nuts DataFrame
  90. Diameter_matrix = NutsNormalizedDataFrame['Diameter'].as_matrix()
  91. Height_matrix = NutsNormalizedDataFrame['Height'].as_matrix()
  92. TEGWNE_Nuts_matrix = NutsNormalizedDataFrame['TEGWNE'].as_matrix()
  93.  
  94. ####NeuralNetwork implementation
  95. X = np.array((Diameter_matrix, Height_matrix, TEGWNE_Nuts_matrix))
  96. #Input Values are the matrixes so we begin use them in Sigmoid
  97.  
  98. class Neural_Network(object):
  99. def __init__(self):
  100. #paramaters
  101. self.input = 3
  102. self.outputSize = 1
  103. self.hiddenSize = 3
  104.  
  105. #Weights
  106. self.W1 = np.random.randn(self.inputSize, self.hiddenSize)
  107. self.W2 = np.random.randn(self.hiddenSize, self.outputSize)
  108.  
  109. def forward(self, X):
  110. #Forward propagation
  111. self.z = np.dot(X, self.W1)
  112. self.z2 = self.sigmoid(self.z)
  113. self.z3 = np.dot(self.z2, self.W2)
  114. o = self.sigmoid(self.z3)
  115. return o
  116.  
  117. def sigmoid(self, s):
  118. return 1/(1+np.exp(-s))
  119.  
  120. def sigmoidPrime(self, s):
  121. return s* (1 - s)
  122.  
  123. def backward(self, X, y, o):
  124. # backward propgate through the network
  125. self.o_error = y - o # error in output
  126. self.o_delta = self.o_error*self.sigmoidPrime(o) # applying derivative of sigmoid to error
  127.  
  128. self.z2_error = self.o_delta.dot(self.W2.T) # z2 error: how much our hidden layer weights contributed to output error
  129. self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2) # applying derivative of sigmoid to z2 error
  130.  
  131. self.W1 += X.T.dot(self.z2_delta) # adjusting first set (input --> hidden) weights
  132. self.W2 += self.z2.T.dot(self.o_delta) # adjusting second set (hidden --> output) weights
  133.  
  134. def train (self, X, y):
  135. o = self.forward(X)
  136. self.backward(X, y, o)
  137.  
  138. NN = Neural_Network()
  139. for i in range(1000): # trains the NN 1,000 times
  140. print ("Input: " + str(X))
  141. print ("Actual Output: " + str(Y))
  142. print ("Predicted Output: " + str(NN.forward(X)))
  143. print ("Loss: " + str(np.mean(np.square(Y - NN.forward(X))))) # mean sum squared loss
  144. print (" ")
  145. NN.train(X, y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement