Kaggle competition: hackStat 2.0
<SOS> metric: categorizationaccuracy Kaggle data source: hackstat-2.0<create_dataframe>
for dirname, _, filenames in os.walk('/kaggle/input/'):
for filename in filenames:
print(os.path.join(dirname, filename))
Trainset = pd.read_csv("/kaggle/input/hackstat2k19/Trainset.csv", header = 0)
 Trainset = Trainset.dropna()
xset = pd.read_csv("/kaggle/input/hackstat2k19/xtest.csv")
 xset = xset.dropna()
Trainset = Trainset.replace(to_replace=['Returning_Visitor', 'New_Visitor'], value=[1, 2])
 Trainset = Trainset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], value=[1, 2,3,4,5,6,7,8,9,10,11,12])
 Trainset = Trainset.replace(to_replace=[False, True], value=[1, 2])
 xset = xset.replace(to_replace=['Returning_Visitor', 'New_Visitor'], value=[1, 2])
 xset = xset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'], value=[1, 2,3,4,5,6,7,8,9,10,11,12])
 xset = xset.replace(to_replace=[False, True], value=[1, 2])
 print(Trainset.shape)
 print(list(Trainset.columns))
print(xset.shape)
 Trainset
xArray = Trainset.iloc[:, [0,1,2,3,4,5,6,7,8,9,11,12,13,14]].values
yArray = Trainset.iloc[:, 17].values
print(xArray)
 print(len(yArray))
m = 10466
M =[]
xsetArray = xset.iloc[:, [0,1,2,3,4,5,6,7,8,9,11,12,13,14]].values
alpha = 0.05
class LogisticRegression:
def gradient_decent(self, x, y):
w = np.ones(( np.shape(x)[1],1))
print(type(x))
wARR = np.mat(w)
 print(type(wARR))
print(type(y))
for i in range(m):
w = w - alpha * x.transpose() *(self.logistic(x * w)- y)
 return w
def classify(self, x, w):
prob = self.logistic(sum(x * w))
classification = 0
if prob > 0.5: classification = 1
return classification
def logistic(self,ws):
return 1.0/(1 + np.exp(-ws))
logisticRegression = LogisticRegression()
wArray = logisticRegression.gradient_decent(np.mat(xArray), np.mat(yArray ).transpose())
 print("close to window proceed")
 score = xsetArray
print(score)
 for p in range(1850):
M += [(p+1,logisticRegression.classify([float(score[p][0]), float(score[p][1]),float(score[p][2]), float(score[p][3]),float(score[p][4]), float(score[p][5]),float(score[p][6]),float(score[p][7]),float(score[p][8]),float(score[p][9]),float(score[p][10]), float(score[p][11]),float(score[p][12]),float(score[p][13])], wArray)) ]
print(M)
 dfObj = pd.DataFrame(M, columns = ["ID","Revenue"])
result = pd.DataFrame({'PassengerId':test['PassengerId'],'Survived':predictions} )<save_to_csv>
def handle_command_line() :
flag = True
while(flag):
entry = input("input:")
 if(entry != "exit"):
score = entry.split()
p = logisticRegression.classify([float(score[0]), float(score[1]),float(score[2]), float(score[3]),float(score[4]), float(score[5]),float(score[6]), float(score[7],float(score[8]), float(score[9])], wArray)
 else:
flag = False
result.to_csv("submission.csv", index=False )<import_modules>
for dirname, _, filenames in os.walk('/kaggle/input/'):
for filename in filenames:
print(os.path.join(dirname, filename))
Trainset = pd.read_csv("/kaggle/input/hackstat2k19/Trainset.csv", header = 0)
 Trainset = Trainset.dropna()
Trainset.values.tolist()
Trainset
Trainset = Trainset.replace(to_replace=['Returning_Visitor', 'New_Visitor'], value=[1, 2])
 print(Trainset )
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from sklearn.model_selection import GridSearchCV<load_from_csv>
for dirname, _, filenames in os.walk('/kaggle/input/'):
for filename in filenames:
print(os.path.join(dirname, filename))
Trainset = pd.read_csv("/kaggle/input/hackstat2k19/Trainset.csv", header = 0)
 Trainset = Trainset.dropna()
Trainset = Trainset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec'], value=[0,2,166,0,307,21,54,69,72,97,641,183])
 Trainset = Trainset.replace(to_replace=['Returning_Visitor', 'New_Visitor' , 'Other'], value=[1242, 366, 11])
 xset = pd.read_csv("/kaggle/input/hackstat2k19/xtest.csv")
 xset = xset.dropna()
Trainset['is_train'] = np.random.uniform(0,1,len(Trainset)) <= 0.75
print(Trainset)
 Train, Test = Trainset[Trainset['is_train'] == True], Trainset[Trainset['is_train'] == False]
print('Number of Training examples', len(Train))
print('Number of Testing examples', len(Test))
features = Trainset.columns[:16]
print(features)
 y = Train['Revenue']
y
clf = RandomForestClassifier(n_jobs = 2, random_state = 0)
 clf.fit(Train[features],y)
 clf.predict(Test[features])
 clf.predict_proba(Test[features])
 preds = clf.predict(Test[features])
 pd.crosstab(Test['Revenue'], preds, rownames = ['Actual Revenue'], colnames = ['Predicted Revenue'])
<drop_column><EOS> 
for dirname, _, filenames in os.walk('/kaggle/input/'):
for filename in filenames:
print(os.path.join(dirname, filename))
Trainset = pd.read_csv("/kaggle/input/hackstat2k19/Trainset.csv", header = 0)
 Trainset = Trainset.dropna()
sampleset = pd.read_csv("/kaggle/input/hackstat2k19/sample_submisison.csv", header = 0)
 sampleset = sampleset.dropna()
Trainset = Trainset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec'], value=[0,2,166,0,307,21,54,69,72,97,641,183])
 Trainset = Trainset.replace(to_replace=['Returning_Visitor', 'New_Visitor' , 'Other'], value=[1242, 366, 11])
 xset = pd.read_csv("/kaggle/input/hackstat2k19/xtest.csv")
 xset = xset.dropna()
xset = xset.replace(to_replace=['Jan', 'Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec'], value=[0,2,166,0,307,21,54,69,72,97,641,183])
 xset = xset.replace(to_replace=['Returning_Visitor', 'New_Visitor' , 'Other'], value=[1242, 366, 11])
 print(Trainset)
 Train, Test = Trainset,xset
print('Number of Training examples', len(Train))
print('Number of Testing examples', len(Test))
features = Trainset.columns[:16]
print(features)
 y = Train['Revenue']
y
id_ = sampleset['ID']
clf = RandomForestClassifier(n_jobs = 2, random_state = 0)
 clf.fit(Train[features],y)
 clf.predict(Test[features])
 clf.predict_proba(Test[features])
 preds = clf.predict(Test[features])
 YArray= id_.as_matrix(columns=None)
 print(YArray)
 df = pd.DataFrame({"ID" : YArray, "Revenue" : preds})
 df.to_csv("submission.csv", index=False)