日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python特征工程插件_python特征工程

發(fā)布時間:2024/9/27 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python特征工程插件_python特征工程 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

python特征工程

代碼星球閱讀(149)2020-04-04收藏0次評論

#特征工程

#1-1sklearn中進行特征選擇

#篩選法-方差篩選過濾

import numpy as np

import array

from sklearn.feature_selection import VarianceThreshold

x=[[0,0,1],[0,1,0],[1,0,0],[0,1,1],[0,1,0],[0,1,1]]

sel=VarianceThreshold(threshold=(.8*(1-.8))) #第一列0的比例超過了80%,在結(jié)果里剔除這一個特征

print(sel.fit_transform(x))

#卡方檢驗s篩選2個最好的特征

from sklearn.datasets import load_iris

from sklearn.feature_selection import SelectKBest

from sklearn.feature_selection import chi2

from scipy.stats import pearsonr

iris=load_iris()

x,y=iris.data,iris.target

print(x.shape)

x_new=SelectKBest(chi2,k=2).fit_transform(x,y)

print(x_new.shape)

print(np.hstack([x,x_new]))

'''

#使用相關(guān)系數(shù)的方法進行特征選擇

#x_new=SelectKBest(lambda X, Y: array(map(lambda x: pearsonr(x, Y),X.T)).T, k=2).fit_transform(x,y)

#print(x_new.shape)

#print(np.hstack([x,x_new]))

#基于互信息法來進行相關(guān)性的判斷

from sklearn.feature_selection import SelectKBest

from minepy import MINE

# 由于MINE的設(shè)計不是函數(shù)式的,定義mic方法將其為函數(shù)式的,返回一個二元組,二元組的第2項設(shè)置成固定的P值0.5

def mic(x, y):

m = MINE()

m.compute_score(x, y)

return (m.mic(), 0.5)

# 選擇K個最好的特征,返回特征選擇后的數(shù)據(jù)

SelectKBest(lambda X, Y: array(map(lambda x: mic(x, Y), X.T)).T, k=2).fit_transform(iris.data, iris.target)

'''

#包裝法-根據(jù)模型選擇特征-遞歸特性消除法

#選定一些算法,根據(jù)算法在數(shù)量上的表現(xiàn)來進行特征集合,一般選擇用的算法包括隨機森林,支持向量機和k近鄰算法

import matplotlib.pyplot as plt

from sklearn.svm import SVC

from sklearn.model_selection import StratifiedKFold

from sklearn.feature_selection import RFECV

from sklearn.datasets import make_classification

#創(chuàng)建一個虛擬分類數(shù)據(jù)集1000個樣本,8個分類結(jié)果,25個特征

x,y=make_classification(n_samples=1000,n_features=25,n_informative=3,n_redundant=2,

n_repeated=0,n_classes=8,n_clusters_per_class=1,random_state=0)

svc=SVC(kernel="linear")

rfec=RFECV(estimator=svc,step=1,cv=StratifiedKFold(2),scoring="accuracy")

rfec.fit(x,y)

print("Optimal number of features: %d" % rfec.n_features_)

print(rfec.ranking_) #輸出各個特征重要性序號,選擇的特征是1,其他依次排序

print(rfec.support_)

plt.figure()

plt.xlabel("number of features selected")

plt.ylabel("Cross validation score(nb of correct classificaions)")

plt.plot(range(1,len(rfec.grid_scores_)+1),rfec.grid_scores_)

plt.ylim([0,1])

plt.show()

#嵌入法-基于懲罰項的特征選擇方法-很少用這個方法

from sklearn.svm import LinearSVC

from sklearn.datasets import load_iris

from sklearn.feature_selection import SelectFromModel

iris=load_iris()

x,y=iris.data,iris.target

print("原來數(shù)據(jù)的特征維度為:",x.shape)

lsvc=LinearSVC(C=0.01,penalty="l1",dual=False)

lsvc.fit(x,y)

model=SelectFromModel(lsvc,prefit=True)

x_new=model.transform(x)

print("l1懲罰項處理之后的數(shù)據(jù)維度為:",x_new.shape)

#嵌入法之基于樹模型的特征選擇法

from sklearn.ensemble import ExtraTreesClassifier

from sklearn.datasets import load_iris

x,y=iris.data,iris.target

print("原來數(shù)據(jù)的特征維度為:",x.shape)

clf=ExtraTreesClassifier()

clf.fit(x,y)

print(clf.feature_importances_)

model=SelectFromModel(clf,prefit=True)

x_new=model.transform(x)

print("新數(shù)據(jù)維度為:",x_new.shape)

#1-2特征變換與特征提取

#ong-hot的兩種方法

from sklearn.preprocessing import OneHotEncoder

from sklearn.datasets import load_iris

iris=load_iris()

print(OneHotEncoder().fit_transform(iris.target.reshape(-1,1)).toarray())

#pandas中的one-hot方法

import pandas as pd

print(pd.get_dummies(iris.target))

#特征組合和降維:主要是從業(yè)務(wù)的層面進行考慮,在單特征不能取得進一步效果時,需要對于各個原生的單特征進行進行計算組合出新的特征,特別需要業(yè)務(wù)考量,而不是隨意組合

#2 招聘數(shù)據(jù)的特征工程探索

import warnings

warnings.filterwarnings("ignore")

import numpy as np

import pandas as pd

#導(dǎo)入數(shù)據(jù)

la=pd.read_csv("D:Byrbt2018Studypython機器學(xué)習(xí)全流程項目實戰(zhàn)精講配套課件第六講 特征工程lagou_data5.csv",encoding="gbk")

print(la.head())

#advantage和label這兩個特征作用不大,可以在最后剔除掉

#分類變量one-hot處理

#pandas ona-hot方法

print(pd.get_dummies(la["city"].head()))

'''

#sklearn方法

#先將文本信息進行分列編碼

from sklearn.preprocessing import OneHotEncoder

from sklearn.preprocessing import LabelEncoder

la1=LabelEncoder()

la1.fit(list(la["city"].values))

la["city"]=la1.transform(list(la["city"].values))

print(la["city"].head())

#再由硬編碼轉(zhuǎn)變?yōu)閛ne-hot編碼

df=OneHotEncoder().fit_transform(la["city"].values.reshape(-1,1)).toarray()

print(df[:5])

'''

#對于招聘數(shù)據(jù)特征分類變量進行逐個的one-hot處理

f=["city","industry","education","position_name","size","stage","work_year"]

for i in f:

temp=pd.get_dummies(la[i])

la=pd.concat([la,temp],axis=1) #將轉(zhuǎn)換的列合并

la=la.drop([i],axis=1) #刪掉之前的變量

print(la.shape)

#刪掉原來的特征即可

pd.options.display.max_columns=99

la=la.drop(["advantage","label","position_detail","salary"],axis=1)

print(la.shape)

print(la.head())

la1=la

#文本類信息的特征提取方法Python-Java-Excel-SQL-R等特征有誤分類列

la=pd.read_csv("D:Byrbt2018StudyPython機器學(xué)習(xí)全流程項目實戰(zhàn)精講配套課件第六講 特征工程lagou_data5.csv",encoding="gbk")

la=la[["position_detail","salary"]]

#提取python信息的特征列

for i,j in enumerate(la["position_detail"]):

if "python" in j:

la["position_detail"][i]=j.replace("python","Python")

la["Python"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "Python" in j:

la["Python"][i] =1

else:

la["Python"][i] =0

print(la["Python"].head())

la["R"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "R" in j:

la["R"][i] =1

else:

la["R"][i] =0

print(la["R"].value_counts())

for i,j in enumerate(la["position_detail"]):

if "sql" in j:

la["position_detail"][i]=j.replace("sql","SQL")

la["SQL"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "SQL" in j:

la["SQL"][i] =1

else:

la["SQL"][i] =0

print(la["SQL"].value_counts())

la["Excel"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "Excel" in j:

la["Excel"][i] =1

else:

la["Excel"][i] =0

print(la["Excel"].value_counts())

la["Java"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "Java" in j:

la["Java"][i] =1

else:

la["Java"][i] =0

print(la["Java"].value_counts())

for i,j in enumerate(la["position_detail"]):

if "linux" in j:

la["position_detail"][i]=j.replace("linux","Linux")

la["Linux"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "Linux" in j:

la["Linux"][i] =1

else:

la["Linux"][i] =0

print(la["Linux"].value_counts())

la["C++"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "C++" in j:

la["C++"][i] =1

else:

la["C++"][i] =0

print(la["C++"].value_counts())

for i,j in enumerate(la["position_detail"]):

if "spark" in j:

la["position_detail"][i]=j.replace("spark","Spark")

la["Spark"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "Spark" in j:

la["Spark"][i] =1

else:

la["Spark"][i] =0

print(la["Spark"].value_counts())

for i,j in enumerate(la["position_detail"]):

if "tensorflow" in j:

la["position_detail"][i]=j.replace("tensorflow","Tensorflow")

if "TensorFlow" in j:

la["position_detail"][i]=j.replace("TensorFlow","Tensorflow")

la["Tensorflow"]=pd.Series()

for i, j in enumerate(la["position_detail"]):

if "Tensorflow" in j:

la["Tensorflow"][i] =1

else:

la["Tensorflow"][i] =0

print(la["Tensorflow"].value_counts())

la=la.drop(["position_detail"],axis=1)

print(la.head())

la=pd.concat((la,la1),axis=1).reset_index(drop=True)

print(la.head())

print(la.shape)

以上就是python特征工程的全部內(nèi)容。

原文鏈接: https://www.cnblogs.com/Yanjy-OnlyOne/p/12591085.html

版權(quán)說明:

轉(zhuǎn)載請注明原文鏈接

本站聲明:本文由用戶自發(fā)上傳,51dev.com僅提供信息存儲空間服務(wù)。如發(fā)現(xiàn)本文有涉嫌侵權(quán)的內(nèi)容,請?zhí)峁┫嚓P(guān)證據(jù),將于24小時內(nèi)刪除。

總結(jié)

以上是生活随笔為你收集整理的python特征工程插件_python特征工程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。