Last active
January 1, 2016 03:39
-
-
Save alfard/8087086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
import csv | |
import random | |
a=[] | |
######################################################################### | |
#f = open('/home/alfard/Documents/Kaggle/Facebook2/TrainClean.csv',"rb") | |
f = open('/home/ubuntu/TrainClean.csv',"rb") | |
fileopen = csv.reader(f,delimiter=',', quotechar='"') | |
p=0 | |
for row in fileopen: | |
a.append(row) | |
#p=p+1 | |
#print p | |
f.close() | |
########################################################################### | |
del a[0] | |
b = random.sample(a, 400000) | |
#a=[] | |
b=np.array(b) | |
########################################################################### | |
#b[:,0].astype(np.int32) | |
#b.sort(axis=0) | |
#b[:,0].astype(np.str) | |
#b=['24'] | |
#b=np.array(b) | |
#b=b[(b[:,0].astype(np.int32)).argsort()] | |
########################################################################## | |
##### TITLE ########################################## | |
########################################################################## | |
from sklearn.feature_extraction.text import CountVectorizer | |
vectorizer=CountVectorizer(min_df=1) | |
X=vectorizer.fit_transform(b[:,1]) | |
I=vectorizer.get_feature_names() | |
c=[] | |
for i in I: | |
c.append(i.encode('utf-8')) | |
########################################################################### | |
#np.where(pred=='7695860') | |
########################################################################### | |
##### TAGS ################################################## | |
########################################################################### | |
t=list(b[:,2]) | |
########################################################################### | |
t0=[] | |
for i in range(0,len(t)): | |
t0.append(t[i].split(' ')[0]) | |
t0=np.array(t0) | |
########################################################################### | |
t1=[] | |
for i in range(0,len(t)): | |
if len(t[i].split(' '))>1: | |
t1.append(t[i].split(' ')[1]) | |
else: | |
t1.append('') | |
t1=np.array(t1) | |
########################################################################### | |
t2=[] | |
for i in range(0,len(t)): | |
if len(t[i].split(' '))>2: | |
t2.append(t[i].split(' ')[2]) | |
else: | |
t2.append('') | |
t2=np.array(t2) | |
########################################################################### | |
t3=[] | |
for i in range(0,len(t)): | |
if len(t[i].split(' '))>3: | |
t3.append(t[i].split(' ')[3]) | |
else: | |
t3.append('') | |
t3=np.array(t3) | |
########################################################################## | |
t4=[] | |
for i in range(0,len(t)): | |
if len(t[i].split(' '))>4: | |
t4.append(t[i].split(' ')[4]) | |
else: | |
t4.append('') | |
t4=np.array(t4) | |
############################################################################ | |
#################TEST TITLE ########################################## | |
############################################################################ | |
A=[] | |
############################################################################ | |
#f = open('/home/alfard/Documents/Kaggle/Facebook2/TestClean.csv',"rb") | |
f = open('/home/ubuntu/TestClean.csv',"rb") | |
fileopen = csv.reader(f,delimiter=',', quotechar='"') | |
p=0 | |
for row in fileopen: | |
A.append(row) | |
#p=p+1 | |
#print p | |
f.close() | |
########################################################################### | |
del A[0] | |
#B = random.sample(A, 200) | |
#A=[] | |
#B=np.array(B) | |
B=np.array(A) | |
A=[] | |
from sklearn.feature_extraction.text import CountVectorizer | |
vectorizer=CountVectorizer(min_df=1,vocabulary=c) | |
Z=vectorizer.fit_transform(B[:,0]) | |
############################################################################# | |
### MODEL ############ | |
############################################################################# | |
from sklearn.linear_model import SGDClassifier | |
from sklearn.externals import joblib | |
svc = SGDClassifier() | |
########################################################################### | |
########################################################################### | |
#########Prediction par ligne############################################## | |
print "Q1" | |
train=svc.fit(X,t0) | |
Q1=[] | |
a=0 | |
b=20001 | |
for i in range(0,100): | |
prediction0=svc.predict(Z[range(a,b),:]) | |
#prediction0=list(prediction0) | |
#print i | |
Q1.extend(prediction0) | |
a=a+20001 | |
b=b+20001 | |
prediction0=svc.predict(Z[range(2000100,2013337),:]) | |
Q1.extend(prediction0) | |
############################################################################ | |
print "Q2" | |
train=svc.fit(X,t1) | |
Q2=[] | |
a=0 | |
b=20001 | |
for i in range(0,100): | |
prediction1=svc.predict(Z[range(a,b),:]) | |
#prediction0=list(prediction0) | |
#print i | |
Q2.extend(prediction1) | |
a=a+20001 | |
b=b+20001 | |
prediction1=svc.predict(Z[range(2000100,2013337),:]) | |
Q2.extend(prediction1) | |
############################################################################# | |
print "Q3" | |
train=svc.fit(X,t2) | |
Q3=[] | |
a=0 | |
b=20001 | |
for i in range(0,100): | |
prediction2=svc.predict(Z[range(a,b),:]) | |
#prediction0=list(prediction0) | |
#print i | |
Q3.extend(prediction2) | |
a=a+20001 | |
b=b+20001 | |
prediction2=svc.predict(Z[range(2000100,2013337),:]) | |
Q3.extend(prediction2) | |
############################################################################## | |
print "Q4" | |
train=svc.fit(X,t3) | |
Q4=[] | |
a=0 | |
b=20001 | |
for i in range(0,100): | |
prediction3=svc.predict(Z[range(a,b),:]) | |
#prediction0=list(prediction0) | |
#print i | |
Q4.extend(prediction3) | |
a=a+20001 | |
b=b+20001 | |
prediction3=svc.predict(Z[range(2000100,2013337),:]) | |
Q4.extend(prediction3) | |
############################################################################ | |
print "Q5" | |
train=svc.fit(X,t4) | |
Q5=[] | |
a=0 | |
b=20001 | |
for i in range(0,100): | |
prediction4=svc.predict(Z[range(a,b),:]) | |
#prediction0=list(prediction0) | |
#print i | |
Q5.extend(prediction4) | |
a=a+20001 | |
b=b+20001 | |
prediction4=svc.predict(Z[range(2000100,2013337),:]) | |
Q5.extend(prediction4) | |
############################################################################# | |
prediction0=np.array(Q1) | |
prediction1=np.array(Q2) | |
prediction2=np.array(Q3) | |
prediction3=np.array(Q4) | |
prediction4=np.array(Q5) | |
predicted_obs=np.column_stack((prediction0,prediction1,prediction2,prediction3,prediction4)) | |
predicted_obs=predicted_obs.astype(object) | |
predicted_obs[:,0] += " " + predicted_obs[:,1] + " " + predicted_obs[:,2] + " " + predicted_obs[:,3]+ " " + predicted_obs[:,4] | |
predicted_obs=predicted_obs[:,:1] | |
############################################################################# | |
###### RESULT ############################################################### | |
############################################################################# | |
A=[] | |
#f = open('/home/alfard/Documents/Kaggle/Facebook2/TestID.csv',"rb") | |
f = open('/home/ubuntu/TestID.csv',"rb") | |
fileopen = csv.reader(f,delimiter=',', quotechar='"') | |
p=0 | |
for row in fileopen: | |
A.append(row[0]) | |
#p=p+1 | |
#print p | |
f.close() | |
############################################################################ | |
del A[0] | |
b= np.array(A) | |
RESULTMP=np.column_stack((b,predicted_obs)) | |
klk=dict(RESULTMP) | |
#Resultupdate=np.load('/home/alfard/Documents/Kaggle/Facebook2/Resultupdate.npz') | |
Resultupdate=np.load('/home/ubuntu/Resultupdate.npz') | |
#np.where(CommunIDTest=='6562016') | |
Resultupdate=Resultupdate['arr_0'] | |
Resultupdate=dict(Resultupdate) | |
klk.update(Resultupdate) | |
RESULT=np.array(klk.items()) | |
#np.where(pred=='7695860') | |
for i in range(0,len(RESULT)): | |
RESULT[i,1]='"'+RESULT[i,1]+'"' | |
############################################################################ | |
#np.savetxt('/home/alfard/Documents/Kaggle/Facebook2/submissionSGDDUP3M.csv', RESULT, delimiter=',', fmt='%s,%s',header='"Id","Tags"', comments = '') | |
np.savetxt('/home/ubuntu/submission.csv', RESULT, delimiter=',', fmt='%s,%s',header='"Id","Tags"', comments = '') | |
############################################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment