-
Notifications
You must be signed in to change notification settings - Fork 1
/
MaterialtoOneHotVector.py
85 lines (61 loc) · 2.46 KB
/
MaterialtoOneHotVector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 18 14:53:57 2019
@author: Murat Cihan Sorkun
"""
import pandas as pd
import matplotlib.pyplot as pyplot
import re
def main():
data = pd.read_csv('2d-materials-list.csv', header=0)
formulas= data['Formula'].values
allElementsList = CreateElementList(formulas)
elementsCount = CountElements(formulas,allElementsList)
pyplot.figure(figsize=(14,5))
pyplot.bar(allElementsList,elementsCount, width = 1/2)
oneHotVector = CreateOneHotVector(formulas,allElementsList)
oneHotVector.to_csv("OneHotVector.csv", sep=',', encoding='utf-8')
print("Resulst are written in file:OneHotVector.csv")
#Create list for all elements dataset includes
def CreateElementList(formulas):
allElementsList=[]
for formula in formulas:
elementList=re.findall('[A-Z][^A-Z]*', formula)
for elem in elementList:
element, number = splitNumber(elem)
if not element in allElementsList:
allElementsList.append(element)
return allElementsList
#parses elements and number of element occurences from element-number couple
def splitNumber(text):
element = text.rstrip('0123456789')
number = text[len(element):]
if number=="":
number=1
return element, int(number)
#counts number of element occurences from the formula list
def CountElements(formulas,allElementsList):
elementCounts=[0]*len(allElementsList)
for formula in formulas:
elementList=re.findall('[A-Z][^A-Z]*', formula)
for elem in elementList:
element, number = splitNumber(elem)
elementIndex=allElementsList.index(element)
elementCounts[elementIndex]=elementCounts[elementIndex]+1
return elementCounts
#parses element symbols and number of occurences from the formula list and create one hot vector
def CreateOneHotVector(formulas,allElementsList):
vectors = []
for formula in formulas:
elementVector=[0]*len(allElementsList)
elementList=re.findall('[A-Z][^A-Z]*', formula)
for elem in elementList:
element, number = splitNumber(elem)
elementIndex=allElementsList.index(element)
elementVector[elementIndex]=number
vectors.append(elementVector)
oneHotVectorDF=pd.DataFrame(index = formulas, data=vectors, columns=allElementsList)
return oneHotVectorDF
if __name__== "__main__":
main()