<jupyter_start><jupyter_text>Import<jupyter_code>import os
import numpy as np

from astropy.io import fits
from matplotlib import pyplot as plt
import numpy as np
import lightkurve as lk
from astroquery.simbad import Simbad
import pandas as pd 
from numpy import genfromtxt

# #tf
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, models


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error<jupyter_output><empty_output><jupyter_text>Check GPU<jupyter_code>devices = tf.config.list_physical_devices()
print(devices)<jupyter_output>[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU'), PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]<jupyter_text>Import Data<jupyter_code>all_data = np.empty((1,482))
for i in range(25):
  all_data = np.concatenate((all_data,pd.read_csv('/Users/michaelsong/Documents/GitHub/starzam/Data_v2/'+str(i)+'.csv',delimiter=',').to_numpy()), axis=0)
all_data = all_data[1:].T[1:].T
print(all_data.shape)
print(all_data)
# pd.read_csv('/content/drive/MyDrive/Research - Yihong Song and <NAME>/Yihong/Data_v2/0.csv',delimiter=',').to_numpy
all_x = all_data.T[10:].T
all_y = all_data.T[2:8].T
print(all_x.shape,all_y.shape)
teff_y = all_y.T[1].T
print(teff_y.shape)<jupyter_output>(36575, 471) (36575, 6)
(36575,)<jupyter_text>Machine Learning<jupyter_code>train_x,test_x,train_y,test_y=train_test_split(all_x, teff_y, test_size=0.20, random_state=42)
print(train_x.shape,test_x.shape,train_y.shape,test_y.shape)
train_x = np.asarray(train_x).astype('float32')
test_x = np.asarray(test_x).astype('float32')
train_y = np.asarray(train_y).astype('float32')
test_y = np.asarray(test_y).astype('float32')
print(train_x.shape,test_x.shape,train_y.shape,test_y.shape)
tf.test.gpu_device_name() # No GPU Sad :v
keras.backend.clear_session()
# model = models.Sequential()

# model.add(layers.Conv1D(1024, 1, activation='relu',batch_input_shape=train_x.shape))
# model.add(layers.Dense(128))
# model.add(layers.Conv1D(32, 1, activation='relu'))
# model.add(layers.Dense(1,activation="sigmoid"))

# model.summary()
model = models.Sequential()
#model.add(layers.Conv1D(filters=16, kernel_size=1, activation='relu', batch_input_shape=(train_x.shape)))

#model.add(layers.Embedding(input_dim=64, output_dim=16))
#model.add(layers.LSTM(128,dropout=0.2,recurrent_dropout=0.2,input_dim=(train_x.shape[2])))
model.add(layers.Dense(4096, activation='relu', input_shape=((471,))))
#model.add(layers.Conv1D(1024,1,activation='relu',input_shape=(train_x.shape)))
#model.add(layers.Conv1D(256,1,activation='relu'))
model.add(layers.Dense(2048, activation="relu"))
model.add(layers.Dense(2048, activation="relu"))
model.add(layers.Dense(2048, activation="relu"))
model.add(layers.Dense(1024, activation="relu"))
model.add(layers.Dense(1024, activation="relu"))
model.add(layers.Dense(1024, activation="relu"))
model.add(layers.Dense(512, activation="relu"))
model.add(layers.Dense(512, activation="relu"))
model.add(layers.Dense(512, activation="relu"))
model.add(layers.Dense(256, activation="relu"))
model.add(layers.Dense(32, activation="relu"))
model.add(layers.Dense(4, activation="relu"))
# model.add(layers.Dense(2, activation="relu"))
model.add(layers.Dense(1))
model.summary()
model.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
# reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.2,
#                               patience=5, min_lr=0.001)
monitor = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=1e-4, 
        patience=30, verbose=1, mode='auto',
        restore_best_weights=True)
history = model.fit(train_x, train_y, epochs=1000,validation_data=(test_x, test_y),callbacks=[monitor], verbose=2)
model.save('/Users/michaelsong/Documents/GitHub/starzam/Models/m2.1')

pred_test_y = model.predict(test_x).reshape(-1)
pred_train_y = model.predict(train_x).reshape(-1)
print(pred_test_y.shape,pred_train_y.shape)

plt.scatter(train_y,pred_train_y, c='red', alpha=0.5,s=1)

plt.scatter(test_y,pred_test_y, c='blue', alpha=0.5,s=1)

linex = np.linspace(2000,10000)
liney = linex
# Create the plot
plt.plot(linex, liney,c="green")

plt.xlim(2000, 10000)
plt.ylim(2000, 10000)
plt.gca().set_aspect('equal', adjustable='box')
plt.show()
def normalize1D(data):
    return np.divide(np.nan_to_num(data,np.nanmean(data)),np.nanmax(data))
mse = np.sqrt(mean_squared_error(normalize1D(test_y),normalize1D(pred_test_y)))
mae = mean_absolute_error(normalize1D(test_y),normalize1D(pred_test_y))
print("Mean Sq. Error, Mean Abs. Error")
print(mse,mae)<jupyter_output>Mean Sq. Error, Mean Abs. Error
0.20206538 0.1949369
{'fraction_non_alphanumeric': 0.16152263374485595, 'fraction_numerical': 0.04465020576131687, 'mean_word_length': 8.241444866920151, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '440993', 'license': 'MIT', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1874, 'n_words': 252, 'path': 'ML/model1.ipynb', 'repo_name': 'michaelsong4399/starzam', 'size': '4868'}