特征工程(六)lsa和lda

lsa

"""
将tfidf(word)特征降维为lsa特征,并将结果保存至本地,并将结果保存到本地

tfidf(article)可做类似处理

"""
from sklearn.decomposition import TruncatedSVD
import pickle
import time

t_start = time.time()


"""=====================================================================================================================
0 读取tfidf特征
"""
with open('tfidf_word.pkl', 'rb') as f:
	x_train, y_train, x_test = pickle.load(f)

"""=====================================================================================================================
1 特征降维:lsa
"""
lsa = TruncatedSVD(n_components=200)
x_train = lsa.fit_transform(x_train)
x_test = lsa.transform(x_test)

"""=====================================================================================================================
2 将lsa特征保存至本地
"""
data = (x_train, y_train, x_test)
with open('lsa_tfidf(word).pkl', 'wb') as f:
	pickle.dump(data, f_data)

t_end = time.time()
print("共耗时:{}min".format((t_end-t_start)/60))

lda

"""
将countvector(word)特征降维为lda特征,并将结果保存至本地

"""
from sklearn.decomposition import LatentDirichletAllocation
import pickle
import time

t_start = time.time()

"""=====================================================================================================================
1 countvector(word)特征加载
"""
with open('countvector_word.pkl', 'rb') as f:
	x_train, y_train, x_test = pickle.load(f_tf)

"""=====================================================================================================================
2 特征降维:lda
"""
lda = LatentDirichletAllocation(n_components=200)
x_train = lda.fit_transform(x_train)
x_test = lda.transform(x_test)

"""=====================================================================================================================
3 将lda特征保存至本地
"""
data = (x_train, y_train, x_test)
with open('lda_countvector(word).pkl', 'wb') as f:
	pickle.dump(data, f)

t_end = time.time()
print("共耗时:{}min".format((t_end-t_start)/60))

你可能感兴趣的:(达观杯nlp算法比赛总结)