Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. import sys
  2. import numpy as np
  3. import pandas as pd
  4. from sklearn.preprocessing import minmax_scale
  5. import numpy as np
  6. import pandas as pd
  7. import seaborn as sns
  8. import matplotlib.pyplot as plt
  9.  
  10.  
  11. '''
  12. Take a list and create the formatted matrix
  13. '''
  14. def mkdf(ListOf480Numbers):
  15.     normalMatrix = np.array_split(ListOf480Numbers,8) #Take a list and create 8 array (Sections)
  16.     fixMatrix = []
  17.     for i in range(8):
  18.         lines = np.array_split(normalMatrix[i],6) #Split each section in lines (each line contains 10 cells from 0-9)
  19.         newMatrix = [0,0,0,0,0,0] #empty array to contain reordered lines
  20.         for j in (0,2,4):
  21.             newMatrix[j] = lines[j] #lines 1,3,5 remain equal
  22.         for j in (1,3,5):
  23.             newMatrix[j] = lines[j][::-1] #lines 2,4,6 are inverted
  24.         fixMatrix.append(newMatrix)
  25.     return fixMatrix
  26.  
  27. '''
  28. Print the matrix with the required format
  29. '''
  30. def print_df(fixMatrix, csv):
  31.     f = open(csv,"w")
  32.    
  33.     printMatrix = []
  34.     for i in range(6):
  35.         printMatrix.append(np.concatenate([fixMatrix[4][i],fixMatrix[7][i]])) #lines form section 4 and 7 are side by side
  36.     for i in range(6):
  37.         printMatrix.append(np.concatenate([fixMatrix[5][i],fixMatrix[6][i]])) #lines form section 5 and 6 are side by side
  38.     for i in range(6):
  39.         printMatrix.append(np.concatenate([fixMatrix[1][i],fixMatrix[2][i]])) #lines form section 1 and 2 are side by side
  40.     for i in range(6):
  41.         printMatrix.append(np.concatenate([fixMatrix[0][i],fixMatrix[3][i]])) #lines form section 0 and 3 are side by side
  42.     for i in printMatrix:
  43.         i = np.array(i,dtype='U20')
  44.         f.write(",".join(i) + "\n")
  45.     f.close()
  46.  
  47.  
  48.  
  49.  
  50. f = open("D:\me4.txt")
  51. me4 = f.readlines()
  52. f.close()
  53. speed = []
  54. acceleration = []
  55. temperature = []
  56. '''
  57. Remove "\n caracter"
  58. '''
  59. for i in range(len(me4)):
  60.     me4[i] = me4[i].rstrip("\n")
  61.  
  62. '''
  63. Split data in three diferent lists speed, acceleration and temperature
  64. '''
  65.  
  66. for i in range(0,len(me4)-3,4):
  67.     speed.append(me4[i+1])
  68.     acceleration.append(me4[i+2])
  69.     temperature.append(me4[i+3])
  70. '''
  71. Print each matrix
  72. '''
  73. print("Speed\n====Done")
  74. SpeedMatrix = mkdf(speed) #see definition of the function
  75. print_df(SpeedMatrix, "speed.csv") #see definition of the function
  76.  
  77. print("Acceleration\n====Done")
  78. AccMatrix = mkdf(acceleration) #see definition of the function
  79. print_df(AccMatrix, "acceleration.csv") #see definition of the function
  80.  
  81. print("Temperature\n====Done")
  82. newTemp = np.array(temperature, dtype='float64')
  83. newTemp_norm = minmax_scale(newTemp, feature_range=(-40, 150))
  84.  
  85. TempMatrix = mkdf(newTemp_norm) #see definition of the function
  86. print_df(TempMatrix, "temperature.csv") #see definition of the function
  87.  
  88. '''
  89. Missing data plot
  90. '''
  91. speed_df = pd.read_csv("speed.csv",header=None)
  92. speed_df = speed_df.replace(0,np.nan)
  93. sns.heatmap(speed_df.isnull(),cbar=False)
  94. c = 0
  95. for i in nan:
  96.     for j in i:
  97.         if j:
  98.             c += 1
  99. print (f'Null values equals {c}')
  100. plt.show()
  101.  
  102.  
  103. speed = np.array(speed, dtype='float64')
  104. acceleration = np.array(acceleration, dtype='float64')
  105. temperature = np.array(temperature, dtype='float64')
  106. df = pd.DataFrame({"Speed":speed,"Acceleration":acceleration,"Temperature":temperature})
  107. df = df.replace(0,np.nan)
  108. sns.heatmap(df.isnull(),cbar=False)
  109. nan = np.array(df.isnull())
  110. c = 0
  111. for i in nan:
  112.     for j in i:
  113.         if j:
  114.             c += 1
  115. print (f'Null values equals {c}')  
  116. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement