-
Notifications
You must be signed in to change notification settings - Fork 0
/
nifty50dnn1.py
133 lines (120 loc) · 4.46 KB
/
nifty50dnn1.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib as plt
import keras
import os
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn import svm
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_moons, make_circles, make_classification
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVC
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
np.random.seed(1)
cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Future_Close']
data = pd.read_csv('^NSEI_withoutnull.csv', header=0, names=cols)
data = data.drop(['Date'], axis=1)
arr = data.copy()
arr = arr.dropna(axis=0, how='any')
train_start=0
train_end=int(np.floor(0.8*arr.shape[0]))
test_start=train_end+1
test_end=int(arr.shape[0])
arr = arr.values
# shuffle_indices = np.random.permutation(np.arange(2466))
# arr=arr[shuffle_indices]
data_train=arr[np.arange(train_start, train_end),:]
data_test=arr[np.arange(test_start,test_end),:]
data_train=pd.DataFrame(data_train)
data_test=pd.DataFrame(data_test)
data_train=data_train.astype(float)
data_test=data_test.astype(float)
data_train.columns = cols[1:]
data_test.columns = cols[1:]
data_train['Close'] = pd.to_numeric(data_train['Close'], errors='coerce').fillna(0).astype(float)
data_train['Future_Close'] = pd.to_numeric(data_train['Future_Close'], errors='coerce').fillna(0).astype(float)
data_train['Ratio'] = data_train['Future_Close']/data_train['Close']
data_test['Close'] = pd.to_numeric(data_test['Close'], errors='coerce').fillna(0).astype(float)
data_test['Future_Close'] = pd.to_numeric(data_test['Future_Close'], errors='coerce').fillna(0).astype(float)
data_test['Ratio'] = data_test['Future_Close']/data_test['Close']
data_train['Direction'] = np.where(data_train['Future_Close'] > data_train['Close'], 1, 0)
data_test['Direction'] = np.where(data_test['Future_Close'] > data_test['Close'], 1, 0)
# scaler=MinMaxScaler()
# scaler.fit(data_train)
# data_train=scaler.transform(data_train)
# data_test=scaler.transform(data_test)
x_train=data_train.iloc[:,0:4]
y_train=data_train.iloc[:, 6]
x_test=data_test.iloc[:,0:4]
y_test=data_test.iloc[:, 6]
features = 4
"""
Logistic Regression
log = LogisticRegression()
log.fit(x_train, y_train)
pred = log.predict(x_test)
Random Forest Classifier
clf = RandomForestClassifier()
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
Decision Tree Classifier
dec = DecisionTreeClassifier()
dec.fit(x_train, y_train)
pred = dec.predict(x_test)
Naïve Bayes Classifier Algorithm
gau = GaussianNB()
gau.fit(x_train, y_train)
pred = gau.predict(x_test)
Support Vector Machines
model = svm.SVC(kernel='sigmoid', C = 100, gamma = 0.001)
model.fit(x_train, y_train)
pred = model.predict(x_test)
KNeighborsClassifier
neigh = KNeighborsRegressor(n_neighbors=2)
neigh.fit(x_train, y_train)
pred = neigh.predict(x_test)
MLPClassifier
clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5, 2), random_state = 1)
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
AdaBoostClassifier
clf = AdaBoostClassifier()
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
QuadraticDiscriminantAnalysis
clf = QuadraticDiscriminantAnalysis()
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
"""
clf = QuadraticDiscriminantAnalysis()
clf.fit(x_train, y_train)
pred = clf.predict(x_test)
pred = pred.reshape(len(pred),1)
error = np.sum(np.subtract(pred,(y_test.values.reshape(len(pred),1)))!=0)
print(float(error)/len(pred)*100)
"""
Errors
RandomForestClassifier = 54.24430641821946
Naïve Bayes Classifier Algorithm = 53.83022774327122
Decision Tree = 53.41614906832298
Logistic Regression = 49.68944099378882
Support Vector Machine = 46.16977225672878
KNeighborsClassifier = 84.67908902691511
MLPClassifier = 46.16977225672878
AdaBoostClassifier = 51.13871635610766
QuadraticDiscriminantAnalysis = 49.06832298136646
"""