text_plain_output_1.png
import numpy as np

import pandas as pd

def get_xyz_data(filename):
    pos_data = []
    lat_data = []
    with open(filename) as f:
        for line in f.readlines():
            x = line.split()
            if x[0] == 'atom':
                pos_data.append([np.array(x[1:4], dtype=np.float), x[4]])
            elif x[0] == 'lattice_vector':
                lat_data.append(np.array(x[1:4], dtype=np.float))
    A = np.transpose(lat_data)
    B = np.linalg.inv(A)
    R = pos_data[0][0]
    return np.matmul(B, R)

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

for c in train.columns:
    if c.find('angle') != -1:
        print(c)
        train[c] = np.radians(train[c])
        test[c] = np.radians(test[c])
traindata = np.zeros((train.shape[0], 3))
for i, idx in enumerate(train.id.values):
    fn = '../input/train/{}/geometry.xyz'.format(idx)
    data = get_xyz_data(fn)
    traindata[i, :] = data
testdata = np.zeros((test.shape[0], 3))
for i, idx in enumerate(test.id.values):
    fn = '../input/test/{}/geometry.xyz'.format(idx)
    data = get_xyz_data(fn)
    testdata[i, :] = data
train['a0'] = 0
train['a1'] = 0
train['a2'] = 0
train[['a0', 'a1', 'a2']] = traindata
test['a0'] = 0
test['a1'] = 0
test['a2'] = 0
test[['a0', 'a1', 'a2']] = testdata
train.number_of_total_atoms = np.log(train.number_of_total_atoms)
test.number_of_total_atoms = np.log(test.number_of_total_atoms)
alldata = pd.concat([train, test])
alldata = pd.concat([alldata.drop(['spacegroup'], axis=1), pd.get_dummies(alldata['spacegroup'], prefix='SG')], axis=1)
train = alldata[:train.shape[0]].copy()
test = alldata[train.shape[0]:].copy()
target_fe = np.log1p(train.formation_energy_ev_natom)
target_be = np.log1p(train.bandgap_energy_ev)
del train['formation_energy_ev_natom'], train['bandgap_energy_ev'], train['id'], test['id']