15 KiB
15 KiB
import lzma
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import mean_squared_error
import pandas as pd
with lzma.open('train/train.tsv.xz', 'rt', encoding="utf-8") as f:
df = pd.read_csv(f, sep='\t', names=['Begin', 'End', 'Title', 'Publisher', 'Text'])
def readFile(filename):
result = []
with open(filename, 'r', encoding="utf-8") as f:
for line in f:
text = line.split("\t")[0].strip()
result.append(text)
return result
def predict(filename, predictions):
with open(filename, "w") as f:
for p in predictions:
f.write(str(p) + "\n")
df = df[['Text', 'Begin']]
X_train = df['Text']
y_train = df['Begin']
model = make_pipeline(TfidfVectorizer(), LinearRegression())
model.fit(X_train, y_train)
[Pipeline] ... (step 1 of 2) Processing tfidfvectorizer, total= 1.3min
[1;31m---------------------------------------------------------------------------[0m [1;31mKeyboardInterrupt[0m Traceback (most recent call last) [1;32m~\AppData\Local\Temp/ipykernel_21668/3545253539.py[0m in [0;36m<module>[1;34m[0m [0;32m 4[0m [0my_train[0m [1;33m=[0m [0mdf[0m[1;33m[[0m[1;34m'Begin'[0m[1;33m][0m[1;33m[0m[1;33m[0m[0m [0;32m 5[0m [0mmodel[0m [1;33m=[0m [0mmake_pipeline[0m[1;33m([0m[0mTfidfVectorizer[0m[1;33m([0m[1;33m)[0m[1;33m,[0m [0mLinearRegression[0m[1;33m([0m[1;33m)[0m[1;33m,[0m [0mverbose[0m[1;33m=[0m[1;36m2[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [1;32m----> 6[1;33m [0mmodel[0m[1;33m.[0m[0mfit[0m[1;33m([0m[0mX_train[0m[1;33m,[0m [0my_train[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m [1;32m~\anaconda3\lib\site-packages\sklearn\pipeline.py[0m in [0;36mfit[1;34m(self, X, y, **fit_params)[0m [0;32m 392[0m [1;32mif[0m [0mself[0m[1;33m.[0m[0m_final_estimator[0m [1;33m!=[0m [1;34m"passthrough"[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [0;32m 393[0m [0mfit_params_last_step[0m [1;33m=[0m [0mfit_params_steps[0m[1;33m[[0m[0mself[0m[1;33m.[0m[0msteps[0m[1;33m[[0m[1;33m-[0m[1;36m1[0m[1;33m][0m[1;33m[[0m[1;36m0[0m[1;33m][0m[1;33m][0m[1;33m[0m[1;33m[0m[0m [1;32m--> 394[1;33m [0mself[0m[1;33m.[0m[0m_final_estimator[0m[1;33m.[0m[0mfit[0m[1;33m([0m[0mXt[0m[1;33m,[0m [0my[0m[1;33m,[0m [1;33m**[0m[0mfit_params_last_step[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 395[0m [1;33m[0m[0m [0;32m 396[0m [1;32mreturn[0m [0mself[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py[0m in [0;36mfit[1;34m(self, X, y, sample_weight)[0m [0;32m 704[0m [1;33m[0m[0m [0;32m 705[0m [1;32mif[0m [0my[0m[1;33m.[0m[0mndim[0m [1;33m<[0m [1;36m2[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 706[1;33m [0mout[0m [1;33m=[0m [0msparse_lsqr[0m[1;33m([0m[0mX_centered[0m[1;33m,[0m [0my[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 707[0m [0mself[0m[1;33m.[0m[0mcoef_[0m [1;33m=[0m [0mout[0m[1;33m[[0m[1;36m0[0m[1;33m][0m[1;33m[0m[1;33m[0m[0m [0;32m 708[0m [0mself[0m[1;33m.[0m[0m_residues[0m [1;33m=[0m [0mout[0m[1;33m[[0m[1;36m3[0m[1;33m][0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\lsqr.py[0m in [0;36mlsqr[1;34m(A, b, damp, atol, btol, conlim, iter_lim, show, calc_var, x0)[0m [0;32m 411[0m [1;31m# beta*u = a*v - alfa*u,[0m[1;33m[0m[1;33m[0m[0m [0;32m 412[0m [1;31m# alfa*v = A'*u - beta*v.[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 413[1;33m [0mu[0m [1;33m=[0m [0mA[0m[1;33m.[0m[0mmatvec[0m[1;33m([0m[0mv[0m[1;33m)[0m [1;33m-[0m [0malfa[0m [1;33m*[0m [0mu[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 414[0m [0mbeta[0m [1;33m=[0m [0mnp[0m[1;33m.[0m[0mlinalg[0m[1;33m.[0m[0mnorm[0m[1;33m([0m[0mu[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0;32m 415[0m [1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\scipy\sparse\linalg\interface.py[0m in [0;36mmatvec[1;34m(self, x)[0m [0;32m 230[0m [1;32mraise[0m [0mValueError[0m[1;33m([0m[1;34m'dimension mismatch'[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0;32m 231[0m [1;33m[0m[0m [1;32m--> 232[1;33m [0my[0m [1;33m=[0m [0mself[0m[1;33m.[0m[0m_matvec[0m[1;33m([0m[0mx[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 233[0m [1;33m[0m[0m [0;32m 234[0m [1;32mif[0m [0misinstance[0m[1;33m([0m[0mx[0m[1;33m,[0m [0mnp[0m[1;33m.[0m[0mmatrix[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\scipy\sparse\linalg\interface.py[0m in [0;36m_matvec[1;34m(self, x)[0m [0;32m 528[0m [1;33m[0m[0m [0;32m 529[0m [1;32mdef[0m [0m_matvec[0m[1;33m([0m[0mself[0m[1;33m,[0m [0mx[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 530[1;33m [1;32mreturn[0m [0mself[0m[1;33m.[0m[0m__matvec_impl[0m[1;33m([0m[0mx[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 531[0m [1;33m[0m[0m [0;32m 532[0m [1;32mdef[0m [0m_rmatvec[0m[1;33m([0m[0mself[0m[1;33m,[0m [0mx[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\sklearn\linear_model\_base.py[0m in [0;36mmatvec[1;34m(b)[0m [0;32m 694[0m [1;33m[0m[0m [0;32m 695[0m [1;32mdef[0m [0mmatvec[0m[1;33m([0m[0mb[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 696[1;33m [1;32mreturn[0m [0mX[0m[1;33m.[0m[0mdot[0m[1;33m([0m[0mb[0m[1;33m)[0m [1;33m-[0m [0mb[0m[1;33m.[0m[0mdot[0m[1;33m([0m[0mX_offset_scale[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 697[0m [1;33m[0m[0m [0;32m 698[0m [1;32mdef[0m [0mrmatvec[0m[1;33m([0m[0mb[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\scipy\sparse\base.py[0m in [0;36mdot[1;34m(self, other)[0m [0;32m 357[0m [1;33m[0m[0m [0;32m 358[0m """ [1;32m--> 359[1;33m [1;32mreturn[0m [0mself[0m [1;33m*[0m [0mother[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 360[0m [1;33m[0m[0m [0;32m 361[0m [1;32mdef[0m [0mpower[0m[1;33m([0m[0mself[0m[1;33m,[0m [0mn[0m[1;33m,[0m [0mdtype[0m[1;33m=[0m[1;32mNone[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\scipy\sparse\base.py[0m in [0;36m__mul__[1;34m(self, other)[0m [0;32m 465[0m [1;31m# Fast path for the most common case[0m[1;33m[0m[1;33m[0m[0m [0;32m 466[0m [1;32mif[0m [0mother[0m[1;33m.[0m[0mshape[0m [1;33m==[0m [1;33m([0m[0mN[0m[1;33m,[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 467[1;33m [1;32mreturn[0m [0mself[0m[1;33m.[0m[0m_mul_vector[0m[1;33m([0m[0mother[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 468[0m [1;32melif[0m [0mother[0m[1;33m.[0m[0mshape[0m [1;33m==[0m [1;33m([0m[0mN[0m[1;33m,[0m [1;36m1[0m[1;33m)[0m[1;33m:[0m[1;33m[0m[1;33m[0m[0m [0;32m 469[0m [1;32mreturn[0m [0mself[0m[1;33m.[0m[0m_mul_vector[0m[1;33m([0m[0mother[0m[1;33m.[0m[0mravel[0m[1;33m([0m[1;33m)[0m[1;33m)[0m[1;33m.[0m[0mreshape[0m[1;33m([0m[0mM[0m[1;33m,[0m [1;36m1[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [1;32m~\anaconda3\lib\site-packages\scipy\sparse\compressed.py[0m in [0;36m_mul_vector[1;34m(self, other)[0m [0;32m 476[0m [1;31m# csr_matvec or csc_matvec[0m[1;33m[0m[1;33m[0m[0m [0;32m 477[0m [0mfn[0m [1;33m=[0m [0mgetattr[0m[1;33m([0m[0m_sparsetools[0m[1;33m,[0m [0mself[0m[1;33m.[0m[0mformat[0m [1;33m+[0m [1;34m'_matvec'[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [1;32m--> 478[1;33m [0mfn[0m[1;33m([0m[0mM[0m[1;33m,[0m [0mN[0m[1;33m,[0m [0mself[0m[1;33m.[0m[0mindptr[0m[1;33m,[0m [0mself[0m[1;33m.[0m[0mindices[0m[1;33m,[0m [0mself[0m[1;33m.[0m[0mdata[0m[1;33m,[0m [0mother[0m[1;33m,[0m [0mresult[0m[1;33m)[0m[1;33m[0m[1;33m[0m[0m [0m[0;32m 479[0m [1;33m[0m[0m [0;32m 480[0m [1;32mreturn[0m [0mresult[0m[1;33m[0m[1;33m[0m[0m [1;31mKeyboardInterrupt[0m:
filenames=[('dev-0/in.tsv',"dev-0/out.tsv"), ('dev-1/in.tsv', "dev-1/out.tsv"), ('test-A/in.tsv', 'test-A/out.tsv')]
for filename in filenames:
f=readFile(filename[0])
y_predict=model.predict(f)
predict(filename[1],y_predict)