<jupyter_start><jupyter_text>Importing Dependicies<jupyter_code>import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn import tree<jupyter_output><empty_output><jupyter_text>Importing Datasets<jupyter_code>url='https://raw.githubusercontent.com/IntellectualCoders/DockshipMLComp/main/new_train.csv'
df=pd.read_csv(url)
url_test='https://raw.githubusercontent.com/IntellectualCoders/DockshipMLComp/main/new_test.csv'
df_test=pd.read_csv(url_test)
df.head()
df_test.head()
print(df.shape)
df.dropna(inplace=True,how='any')
print(df.shape)
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
Int64Index: 299 entries, 0 to 298
Data columns (total 36 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   Date              299 non-null    object 
 1   Open-Stock-1      299 non-null    float64
 2   High-Stock-1      299 non-null    float64
 3   Low-Stock-1       299 non-null    float64
 4   Close-Stock-1     299 non-null    float64
 5   VWAP-Stock-1      299 non-null    float64
 6   Volume-Stock-1    299 non-null    int64  
 7   Turnover-Stock-1  299 non-null    int64  
 8   Open-Stock-2      299 non-null    float64
 9   High-Stock-2      299 non-null    float64
 10  Low-Stock-2       299 non-null    float64
 11  Close-Stock-2     299 non-null    float64
 12  VWAP-Stock-2      299 non-null    float64
 13  Volume-Stock-2    299 non-null    int64  
 14  Turnover-Stock-2  299 non-null    int64  
 15  Open-Stock-3      299 non-null    float64
 16  High-Stock-3      299 non-null    float64
 1[...]<jupyter_text>Plotting SNS HeatPlot<jupyter_code>import matplotlib.pyplot as plt
from matplotlib import rcParams
from matplotlib.cm import rainbow
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
import seaborn as sns
#get correlations of each features in dataset
corrmat = df.corr()
top_corr_features = corrmat.index
plt.figure(figsize=(20,20))
#plot heat map
g=sns.heatmap(df[top_corr_features].corr(),annot=True,cmap="RdYlGn")<jupyter_output><empty_output><jupyter_text>Data preprocessing<jupyter_code>df.describe()
from sklearn.preprocessing import MinMaxScaler
from sklearn import preprocessing
df.hist(bins=30, figsize=(15, 10))
skewValue = df.skew(0,1)
skewValue
from scipy.stats import boxcox
from scipy.stats import skew
df["Volume-Stock-1"] = np.log1p(df["Volume-Stock-1"])
df["Volume-Stock-2"] = np.log1p(df["Volume-Stock-2"])
df["Volume-Stock-3"] = np.log1p(df["Volume-Stock-3"])
df["Volume-Stock-4"] = np.log1p(df["Volume-Stock-4"])
df["Volume-Stock-5"] = np.log1p(df["Volume-Stock-5"])
df["Turnover-Stock-1"] = np.log1p(df["Turnover-Stock-1"])
df["Turnover-Stock-2"] = np.log1p(df["Turnover-Stock-2"])
df["Turnover-Stock-3"] = np.log1p(df["Turnover-Stock-3"])
df["Turnover-Stock-4"] = np.log1p(df["Turnover-Stock-4"])
df["Turnover-Stock-5"] = np.log1p(df["Turnover-Stock-5"])
df["Open-Stock-1"] , lam1= boxcox(df["Open-Stock-1"])
df["High-Stock-1"] , lam2 = boxcox(df["High-Stock-1"])
df["Low-Stock-1"] , lam3 = boxcox(df["Low-Stock-1"])
df["VWAP-Stock-1"] , lam4 = boxcox(df["VWAP-Stock-1"])
df["Open-Stock-2"] , lam1= boxcox(df["Open-Stock-2"])
df["High-Stock-2"] , lam2 = boxcox(df["High-Stock-2"])
df["Low-Stock-2"] , lam3 = boxcox(df["Low-Stock-2"])
df["VWAP-Stock-2"] , lam4 = boxcox(df["VWAP-Stock-2"])
df.skew(0,1)


df_test["Volume-Stock-1"] = np.log1p(df_test["Volume-Stock-1"])
df_test["Volume-Stock-2"] = np.log1p(df_test["Volume-Stock-2"])
df_test["Volume-Stock-3"] = np.log1p(df_test["Volume-Stock-3"])
df_test["Volume-Stock-4"] = np.log1p(df_test["Volume-Stock-4"])
df_test["Volume-Stock-5"] = np.log1p(df_test["Volume-Stock-5"])
df_test["Turnover-Stock-1"] = np.log1p(df_test["Turnover-Stock-1"])
df_test["Turnover-Stock-2"] = np.log1p(df_test["Turnover-Stock-2"])
df_test["Turnover-Stock-3"] = np.log1p(df_test["Turnover-Stock-3"])
df_test["Turnover-Stock-4"] = np.log1p(df_test["Turnover-Stock-4"])
df_test["Turnover-Stock-5"] = np.log1p(df_test["Turnover-Stock-5"])
df_test["Open-Stock-1"] , lam1= boxcox(df_test["Open-Stock-1"])
df_test["High-Stock-1"] , lam2 = boxcox(df_test["High-Stock-1"])
df_test["Low-Stock-1"] , lam3 = boxcox(df_test["Low-Stock-1"])
df_test["VWAP-Stock-1"] , lam4 = boxcox(df_test["VWAP-Stock-1"])
df_test["Open-Stock-2"] , lam1= boxcox(df_test["Open-Stock-2"])
df_test["High-Stock-2"] , lam2 = boxcox(df_test["High-Stock-2"])
df_test["Low-Stock-2"] , lam3 = boxcox(df_test["Low-Stock-2"])
df_test["VWAP-Stock-2"] , lam4 = boxcox(df_test["VWAP-Stock-2"])
df<jupyter_output><empty_output><jupyter_text>Declaring X and Y<jupyter_code>df.head()
cols= list(df.columns.values)
X1 = df[cols[1:4] +cols[5:6]]
X2 = df[cols[8:11] + cols[12:13]]
X3 = df[cols[15:18] +cols[19:20]]
X4 = df[cols[22:25] + cols[26:27]]
X5 = df[cols[29:32] +cols[33:34]]
y1 = df[cols[4]]
y2 = df[cols[11]]
y3 = df[cols[18]]
y4 = df[cols[25]]
y5 = df[cols[32]]
X5.head()
y1
cols_t= list(df_test.columns.values)
X1_test = df_test[cols_t[1:5] ]
X2_test = df_test[cols_t[7:11] ]
X3_test = df_test[cols_t[13:17] ]
X4_test = df_test[cols_t[19:23] ]
X5_test = df_test[cols_t[25:29] ]

X3_test<jupyter_output><empty_output><jupyter_text>Applying Linear Regression<jupyter_code>from sklearn.linear_model import LinearRegression
model1 = LinearRegression()
model2 = LinearRegression()
model3 = LinearRegression()
model4 = LinearRegression()
model5 = LinearRegression()
model1.fit(X1, y1)
model2.fit(X2, y2)
model3.fit(X3, y3)
model4.fit(X4, y4)
model5.fit(X5, y5)
y1_pred = model1.predict(X1_test)
y2_pred = model2.predict(X2_test)
y3_pred = model3.predict(X3_test)
y4_pred = model4.predict(X4_test)
y5_pred = model5.predict(X5_test)
y1_pred
col_1 = df_test[cols_t[0]]
rows = zip(col_1,y1_pred,y2_pred,y3_pred,y4_pred,y5_pred)
col_1
import csv

with open('submission20.csv', "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)<jupyter_output><empty_output><jupyter_text>Decision Tree Regressor<jupyter_code>from sklearn.tree import DecisionTreeRegressor

regr_1 = DecisionTreeRegressor(max_depth=5)
regr_2 = DecisionTreeRegressor(max_depth=5)
regr_3 = DecisionTreeRegressor(max_depth=5)
regr_4 = DecisionTreeRegressor(max_depth=5)
regr_5 = DecisionTreeRegressor(max_depth=5)
regr_1.fit(X1, y1)
regr_2.fit(X2, y2)
regr_3.fit(X3, y3)
regr_4.fit(X4, y4)
regr_5.fit(X5, y5)
y_1 = regr_1.predict(X1_test)
y_2 = regr_2.predict(X2_test)
y_3 = regr_3.predict(X3_test)
y_4 = regr_4.predict(X4_test)
y_5 = regr_5.predict(X5_test)
col_1 = df_test[cols_t[0]]
rows = zip(col_1,y_1,y_2,y_3,y_4,y_5)
import csv

with open('submission3.csv', "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)<jupyter_output><empty_output><jupyter_text>Lasso Regression<jupyter_code>from sklearn import linear_model
clf1 = linear_model.Lasso(alpha=1e-10)
clf2 = linear_model.Lasso(alpha=1e-10)
clf3 = linear_model.Lasso(alpha=1e-10)
clf4 = linear_model.Lasso(alpha=1e-10)
clf5 = linear_model.Lasso(alpha=1e-10)
clf1.fit(X1,y1)
clf2.fit(X2,y2)
clf3.fit(X3,y3)
clf4.fit(X4,y4)
clf5.fit(X5,y5)
yl1 = clf1.predict(X1_test)
yl2 = clf2.predict(X2_test)
yl3 = clf3.predict(X3_test)
yl4 = clf4.predict(X4_test)
yl5 = clf5.predict(X5_test)
col_1 = df_test[cols_t[0]]
rows = zip(col_1,yl1,yl2,yl3,yl4,yl5)
import csv

with open('submission13.csv', "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)<jupyter_output><empty_output><jupyter_text>Ridge Regression<jupyter_code>from sklearn.linear_model import Ridge
rmodel1 = Ridge(alpha=10)
rmodel2 = Ridge(alpha=10)
rmodel3 = Ridge(alpha=10)
rmodel4 = Ridge(alpha=10)
rmodel5 = Ridge(alpha=10)
rmodel1.fit(X1, y1)
rmodel2.fit(X2, y2)
rmodel3.fit(X3, y3)
rmodel4.fit(X4, y4)
rmodel5.fit(X5, y5)
y1_pred = rmodel1.predict(X1_test)
y2_pred = rmodel2.predict(X2_test)
y3_pred = rmodel3.predict(X3_test)
y4_pred = rmodel4.predict(X4_test)
y5_pred = rmodel5.predict(X5_test)
col_1 = df_test[cols_t[0]]
rows = zip(col_1,y1_pred,y2_pred,y3_pred,y4_pred,y5_pred)
col_1
import csv

with open('submission21.csv', "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)<jupyter_output><empty_output><jupyter_text>ANN<jupyter_code>from joblib.numpy_pickle_utils import xrange
from random import random
class NeuralNet(object): 
    def __init__(self): 
        # Generate random numbers 
        random.seed(1) 
  
        # Assign random weights to a 3 x 1 matrix, 
        self.synaptic_weights = 2 * random.random((3, 1)) - 1
  
    # The Sigmoid function 
    def __sigmoid(self, x): 
        return 1 / (1 + exp(-x)) 
  
    # The derivative of the Sigmoid function. 
    # This is the gradient of the Sigmoid curve. 
    def __sigmoid_derivative(self, x): 
        return x * (1 - x) 
  
    # Train the neural network and adjust the weights each time. 
    def train(self, inputs, outputs, training_iterations): 
        for iteration in xrange(training_iterations): 
            # Pass the training set through the network. 
            output = self.learn(inputs) 
  
            # Calculate the error 
            error = outputs - output 
  
            # Adjust the weights by a factor 
            factor = dot(inputs.T, error * self.__sigmoid_derivative(output)) 
            self.synaptic_weights += factor 
  
        # The neural network thinks. 
  
    def learn(self, inputs): 
        return self.__sigmoid(dot(inputs, self.synaptic_weights)) 
  
  
if __name__ == "__main__": 
    # Initialize 
    neural_network = NeuralNet() 
  
    # The training set. 
    inputs = X1
    outputs = y1
  
    # Train the neural network 
    neural_network.train(inputs, outputs, 10000) 
  
    # Test the neural network with a test example. 
    print(neural_network.learn(X1))<jupyter_output><empty_output><jupyter_text>MLP Regressor<jupyter_code>from sklearn.neural_network import MLPRegressor
regr1 = MLPRegressor(random_state=1, max_iter=1000000, alpha = 0.0001, activation='relu').fit(X1, y1)
regr2 = MLPRegressor(random_state=1, max_iter=1000).fit(X2, y2)
regr3 = MLPRegressor(random_state=1, max_iter=1000).fit(X3, y3)
regr4 = MLPRegressor(random_state=1, max_iter=1000).fit(X4, y4)
regr5 = MLPRegressor(random_state=1, max_iter=1000).fit(X5, y5)
y1p = regr1.predict(X1_test)
y2p = regr2.predict(X2_test)
y3p = regr3.predict(X3_test)
y4p = regr4.predict(X4_test)
y5p = regr5.predict(X5_test)
y1p
col_1 = df_test[cols_t[0]]
rows = zip(col_1,y1p,y2p,y3p,y4p,y5p)
col_1
import csv

with open('submission15.csv', "w") as f:
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)<jupyter_output><empty_output><jupyter_text>LSTM<jupyter_code>training_data = df.iloc[:, 4:5]

training_data.shape
training_data.head()
from sklearn.preprocessing import MinMaxScaler

mm = MinMaxScaler(feature_range = (0, 1))
training_data = mm.fit_transform(training_data)<jupyter_output><empty_output><jupyter_text>New Code<jupyter_code>import time
import math
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.recurrent import LSTM
import numpy as np
import pandas as pd
import sklearn.preprocessing as prep
df.head()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14391885984086736, 'fraction_numerical': 0.056395908017836845, 'mean_word_length': 4.1826008155867695, 'pattern_counts': {'":': 1, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 2, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '223104', 'license': 'MIT', 'n_tokens_mistral': 5092, 'n_tokens_neox': 4775, 'n_words': 780, 'path': 'DockshipMLChallenge.ipynb', 'repo_name': 'IntellectualCoders/DockshipMLComp', 'size': '11437'}