1. 程式人生 > >特徵工程(七)SelectFromModel

特徵工程(七)SelectFromModel

linearsvm

"""
用linearsvm從tfidf(word)中挑選特徵,並將結果儲存到本地

tfidf(article)可做類似處理

"""

import time
import pickle
from sklearn.feature_selection import SelectFromModel
from sklearn.svm import LinearSVC

t_start = time.time()

"""讀取特徵"""

with open('tfidf_word.pkl', 'rb') as f:
	x_train, y_train, x_test = pickle.load(f)

"""進行特徵選擇"""
lsvc = LinearSVC(C=0.5, dual=False).fit(x_train, y_train)
slt = SelectFromModel(lsvc, prefit=True)
x_train_s = slt.transform(x_train)
x_test_s = slt.transform(x_test)

"""儲存選擇後的特徵至本地"""
num_features = x_train_s.shape[1]

with open('linearsvm-tfidf(word).pkl', 'wb') as f:
	pickle.dump((x_train_s, y_train, x_test_s), data_f)

t_end = time.time()
print("特徵選擇完成,選擇{}個特徵,共耗時{}min".format(num_features, (t_end-t_start)/60))

# 特徵選擇完成,選擇888357個特徵,共耗時11.78min

lr

'''
用lr從tfidf(word)中挑選特徵,並將結果儲存到本地

tfidf(article)可做類似處理

'''

import time
import pickle
from sklearn.feature_selection import SelectFromModel
from sklearn.linear_model import LogisticRegression

t_start = time.time()

"""讀取tfidf(word)特徵"""
with open('tfidf_word.pkl', 'rb') as fp:
    x_train, y_train, x_test = pickle.load(fp)

"""進行特徵選擇"""

LR = LogisticRegression(C=120, dual=False).fit(x_train, y_train)
slt = SelectFromModel(LR, prefit=True)
x_train_s = slt.transform(x_train)
x_test_s = slt.transform(x_test)

"""儲存選擇後的特徵至本地"""
num_features = x_train_s.shape[1]

with open('lr-tfidf(word).pkl', 'wb') as data_f:
    pickle.dump((x_train_s, y_train, x_test_s), data_f)

t_end = time.time()
print("特徵選擇完成,選擇{}個特徵,共耗時{}min".format(num_features, (t_end-t_start)/60))