-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHacktober.py
More file actions
203 lines (179 loc) · 7.03 KB
/
Hacktober.py
File metadata and controls
203 lines (179 loc) · 7.03 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import numpy as numpy
import pandas as pd
import matplotlib.pyplot as plt
def userFilePath():
filepath=input("Enter the location of the file: ")
data=pd.read_csv(filepath,low_memory=True)
return data
#Ask them features and target if model is Supervised
#Input the name of the features and variable
#to put comma after each feature
def selectFeatures():
x = input("Enter the names of features : ")
features=x.split(',')
target = input("Enter the target between '' : ")
return features, target
def replaceNull(data, features):
#to count the number of null values in each column
count_null=data[data.columns].isna().sum()
count_null=dict(count_null)
rows=len(data.index)
for i in features:
k=count_null[i]
if k!=0:
if k>(rows/2):
data.drop([i], axis = 1)
else:
print('How do u want to replace the null values for feature: ',i)
print('1. with 0\n2.with mean\n3.with median\n4.with mode')
ch=int(input())
if ch==1:
data[i] = data[i].fillna(0)
elif ch==2:
mean=data[i].mean()
data[i] = data[i].fillna(mean)
elif ch==3:
med=data[i].median()
data[i] = data[i].fillna(med)
elif ch==2:
mode=data[i].mode()
data[i] = data[i].fillna(mode)
return data
#Function to load the features into the X and y respectively and return it
def featget(data, features, target):
X = data.loc[:,features]
print(target)
y = data.loc[:,target]
return (X,y)
#Pre-processing
#OneHotEncoding
#Function that returns X after performing oneHotEncoding
# columnNumber --> takes the columnNumber for which one hot encoding is to be done
def oneHotEncoding(X, columnNumber):
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer(transformers=[('encoder', OneHotEncoder(), [columnNumber])], remainder='passthrough')
X = np.array(ct.fit_transform(X))
return X
#LabelEncoder
# X--> takes only a single column
def labelEncodingColumn(X):
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
X = le.fit_transform(X)
return X
#traintestsplit
def split_dataset(X, y, testSize):
from sklearn.model_selection import train_test_split
# testSize--> defines the size of the test from the dataset(takes decimal less than 1)
# randomState --> takes integer input
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = testSize)
return (X_train, X_test, y_train, y_test)
#Standardization
def standard(X_train,X_test):
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
return (X_train, X_test)
#Normalisation
def normalize(X_train,X_test):
from sklearn.preprocessing import Normalizer
scaler = Normalizer().fit(X_train)
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
return (X_train, X_test)
#Methods to predict the values using ML values
#Logistic Regression
def logReg(X_train,X_test, y_train):
from sklearn.linear_model import LogisticRegression
logr = LogisticRegression()
logr.fit(X_train,y_train)
y_pred = logr.predict(X_test)
return y_pred
#SV Regression
def svcModel(X_train,X_test, y_train):
from sklearn.svm import SVC
svc = SVC(kernel='linear')
svc.fit(X_train, y_train)
y_pred = svc.predict(X_test)
return y_pred
#KNN
def knnModel(X_train,X_test,y_train,n):
from sklearn import neighbors
knn = neighbors.KNeighborsClassifier(n_neighbors=n, metric = 'minkowski', p=2)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
return y_pred
#To find the accuracy of your model.
def accuracy(y_pred,y_test):
from sklearn.metrics import r2_score
return r2_score(y_pred, y_test)
filepath = input('Enter location of the file: ')
df = pd.read_csv(filepath, low_memory=True)
#replacing null values
features, target = selectFeatures()
replaceNull(df, features)
X,y = featget(df,features, target)
print("Do you want to perform One Hot Encoding?[y/n]")
if(input() == 'y'):
X = oneHotEncoding(X, input('Enter the Column number : '))
print('Do you want to perform Label Encoding?[y/n]')
if(input() == 'y'):
col_num = input('Enter the column name : ')
X[col_num] = labelEncodingColumn(X[col_num])
standardizationYN = input("Do you want to perform Standardization ?[y/n]")
normalizationYN = input("Do you want to perform Normalization ?[y/n]")
#after this we have to call the train test split and other prediction models
test_size = 0.1
while(test_size <= 0.45):
X_train,X_test,y_train,y_test = split_dataset(X, y, test_size, 0)
if(standardizationYN == 'y'):
X_train, X_test = standard(X_train,X_test)
if(normalizationYN == 'y'):
X_train, X_test = normalize(X_train,X_test)
test_size+=0.05
#Now when the accuracy calculation function is done
#call all the ML model inside them and then print their respective accuracy
print("1. for Regression: ")
print("2. for Classification: ")
c=int(input("Enter: "))
if c==1:
y_pred1=linearReg(X_train,X_test, y_train)
p=accuracy(y_pred1,y_test)
y_pred2=svcModel(X_train,X_test, y_train)
q=accuracy(y_pred2,y_test)
acc={'Linear Regression':p,'Support Vector Regression':q}
table = pd.DataFrame(acc, columns = ['Linear Regression','Support Vector Regression'], index=['Accuracy'])
print(table)
print("1. for Linear Regression: ")
print(" 2. for Support Vector Regression: ")
result=int(input("Enter: "))
if result==1:
print("Prediction for Linear Regression model: ")
output = pd.DataFrame({target: y_pred1})
print(output,'\n')
else:
print("\nPrediction for Support Vector Regression model: ")
output = pd.DataFrame({target: y_pred2})
print(output,'\n')
elif c==2:
y_pred1=logReg(X_train,X_test, y_train)
p=accuracy(y_pred1,y_test)
n=int(input("\nEnter the value for K"))
y_pred2=knnModel(X_train,X_test, y_train, n)
q=accuracy(y_pred2,y_test)
acc={'Logistic Regression':p,'K-nearest neighbours':q}
table = pd.DataFrame(acc, columns = ['Logistic Regression','K-nearest neighbours'], index=['Accuracy'])
print(table)
print("\n1. for Logistic Regression: ")
print("\n2. for K-nearest neighbours: ")
result=int(input("Enter: "))
if p>q:
print("Prediction for Logistic Regression model: ")
output = pd.DataFrame({target: y_pred1})
print(output,'\n')
else:
print("\nPrediction for K-nearest neighbours model: ")
output = pd.DataFrame({target: y_pred2})
print(output,'\n')