#导入鸢尾花数据集, sklearn中的数据为array格式,使用pd.DataFrame(X).to_csv把array文件保存为csv文件
import pandas as pd
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
import os
from sklearn.datasets import load_iris
iris_dataset = load_iris()
X= iris_dataset.data
pd.DataFrame(X).to_csv("C:/Users/S/Desktop/data.csv")
y= iris_dataset.target
pd.DataFrame(y).to_csv("C:/Users/S/Desktop/target.csv")
data=pd.read_csv("C:/Users/S/Desktop/data.csv")
target=pd.read_csv("C:/Users/S/Desktop/target.csv")
#把两个csv文件整合到一起
cvdMergeData = data.merge(target,how='inner',on = 'biaoqian')
cvdMergeData.to_csv('C:/Users/S/Desktop/kuanbiao.csv')
#取出特征
Data=pd.read_csv('C:/Users/S/Desktop/kuanbiao.csv')
Data
#data.info
X=Data.iloc[:,2:6]
X
#去除标签
y=Data.iloc[:,6]
y
#导入卡方函数 ,对数据进行整合
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
#假设在这里我一直我需要3个特征
X_fschi = SelectKBest(chi2, k=2).fit_transform(X, y)
X_fschi.shape #(42000, 300)
# 建模
#建立模型
from sklearn.ensemble import RandomForestClassifier as RFC
from sklearn.neighbors import KNeighborsClassifier as KNN
from sklearn.model_selection import cross_val_score
#输出
cross_val_score(RFC(n_estimators=10,random_state=0),X,y,cv=5).mean()
cross_val_score(RFC(n_estimators=10,random_state=0),X_fschi,y,cv=5).mean()