Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 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. Pprice_matrix = NutsNormalizedDataFrame['Pprice'].as_matrix()
  94.  
  95. ####NeuralNetwork implementation###
  96. X = np.vstack([np.hstack([Diameter_matrix]), np.hstack([Height_matrix]), np.hstack([TEGWNE_Nuts_matrix])])
  97. #X = np.vstack([np.mean([Diameter_matrix]), np.mean([Height_matrix]), np.mean([TEGWNE_Nuts_matrix])])
  98. X= X.reshape(-1,1)
  99. ### Creating some useful variables
  100. num_examples = len(X)
  101. nn_input_dim = 1
  102. nn_output_din = 1
  103.  
  104. ##Gradient descent parameters picked by the hand
  105. epsilon = 0.01
  106. reg_lambda = 0.01
  107.  
  108. #Helper function to evaluate the total loss on the dataset
  109. def calculate_loss(model):
  110. W1, b1, W2, b2, W3, b3 = model['W1'], model['b1'], model['W2'], model['b2'], model['W3'], model['b3']
  111.  
  112. ##forward propagation to calculate our predictions
  113. z1 = X.dot(W1) + b1
  114. a1 = np.tanh(z1)
  115. z2 = a1.dot(W2) + b2
  116. a2 = np.tanh(z2)
  117. z3 = a2.dot(W3) + b3
  118. exp_scores = np.exp(z3)
  119. probs = exp_scores / np.sum(exp_scores, axis = 1, keepdims = True)
  120.  
  121. ##Calculating the loss
  122.  
  123. correct_logprobs = -np.log(probs[range(num_examples),Pprice_matrix])
  124. data_loss = np.sum(correct_logprobs)
  125.  
  126. ##Adding the regulatization term to loss
  127.  
  128. data_loss += reg_lambda/2 - (np.sum(np.square(W1)) + (np.sum(np.square(W2))) + (np.sum(np.square(W3))))
  129. return 1./num_examples * data_loss
  130.  
  131. def predict(model, x):
  132. W1, b1, W2, b2, W3, b3 = model['W1'], model['b1'], model['W2'], model['b2'], model['W3'], model['b3']
  133. ##forward propagation to calculate our predictions
  134. z1 = X.dot(W1) + b1
  135. a1 = np.tanh(z1)
  136. z2 = a1.dot(W2) + b2
  137. a2 = np.tanh(z2)
  138. z3 = a2.dot(W3) + b3
  139. exp_scores = np.exp(z3)
  140. probs = exp_scores / np.sum(exp_scores, axis = 1, keepdims = True)
  141. return np.argmax(probs, axis = 1)
  142.  
  143.  
  144. def build_model(nn_hdim, num_passes=20000, print_loss=False):
  145. nn_input_dim = 1
  146. nn_output_dim = 1
  147. # Initialize the parameters to random values. We need to learn these.
  148. np.random.seed(0)
  149. W1 = np.random.randn(nn_input_dim, nn_hdim) / np.sqrt(nn_input_dim)
  150. b1 = np.zeros((1, nn_hdim))
  151. W2 = np.random.randn(nn_hdim, nn_output_dim) / np.sqrt(nn_hdim)
  152. W3 = np.random.randn(nn_hdim, nn_output_dim) / np.sqrt(nn_hdim)
  153. b3 = np.zeros((1, nn_output_dim))
  154. b2 = np.zeros((nn_input_dim, nn_output_dim))
  155.  
  156.  
  157. # This is what we return at the end
  158. model = {}
  159.  
  160. # Gradient descent. For each batch...
  161. for i in range(0, num_passes):
  162.  
  163. # Forward propagation
  164. z1 = X.dot(W1) + b1
  165. a1 = np.tanh(z1)
  166. z2 = a1.dot(W2) + b2
  167. a2 = np.tanh(z2)
  168. z3 = a2.dot(W3) + b3
  169. exp_scores = np.exp(z3)
  170. probs = exp_scores / np.sum(exp_scores, axis = 1, keepdims = True)
  171. probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
  172.  
  173. # Backpropagation
  174. delta3 = probs
  175. delta3[range(num_examples)] -= 1
  176. dW2 = (a1.T).dot(delta3)
  177. db2 = np.sum(delta3, axis=0, keepdims=True)
  178. delta2 = delta3.dot(W2.T) * (1 - np.power(a1, 2))
  179. dW1 = np.dot(X.T, delta2)
  180. db1 = np.sum(delta2, axis=0)
  181.  
  182. # Add regularization terms (b1 and b2 don't have regularization terms)
  183. dW2 += reg_lambda * W2
  184. dW1 += reg_lambda * W1
  185.  
  186. # Gradient descent parameter update
  187. W1 += -epsilon * dW1
  188. b1 += -epsilon * db1
  189. W2 += -epsilon * dW2
  190. b2 += -epsilon * db2
  191.  
  192. # Assign new parameters to the model
  193. model = { 'W1': W1, 'b1': b1, 'W2': W2, 'b2': b2 }
  194.  
  195. # Optionally print the loss.
  196. # This is expensive because it uses the whole dataset, so we don't want to do it too often.
  197. if print_loss and i % 1000 == 0:
  198. print (i, calculate_loss(model))
  199.  
  200. return model
  201. import seaborn as sns
  202. model = build_model(3,print_loss = True)
  203.  
  204. sns.pointplot(lambda x: predict (model, x))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement