kaggle比赛地址:leaf-classification
数据集形式:
train_csv:
对于每一张图片都有192个特征。
test.csv没有species这一列,该比赛的目标就是预测这一列,判断属于哪一类。
训练集共有990张,测试集共有594张,共99类
图片如下所示:
#对数据进行标签编码
le = LabelEncoder().fit(train.species)
labels = le.transform(train.species)
labels
将数据标签进行编码,将离散数据进行编码
划分数据集和验证集
train = train.drop(['id','species'],axis=1)
test = test.drop(['id'],axis=1)
x_train,x_test,y_train,y_test = train_test_split(train,labels,test_size=0.2,random_state=0)
from sklearn.metrics import accuracy_score, log_loss
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC, LinearSVC, NuSVC
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier, GradientBoostingClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
classifiers = [
KNeighborsClassifier(3),
SVC(kernel='rbf',C =0.025,probability=True),
NuSVC(probability=True),
DecisionTreeClassifier(),
RandomForestClassifier(),
AdaBoostClassifier(),
GradientBoostingClassifier(),
GaussianNB(),
LinearDiscriminantAnalysis(),
QuadraticDiscriminantAnalysis()
]
log_cols = ['Classifier','Accuracy']
log = pd.DataFrame(columns=log_cols)
for clf in classifiers:
clf.fit(x_train,y_train)
name = clf.__class__.__name__
print('='*30)
print(name)
print('*****Results*****')
train_pred = clf.predict(x_test)
acc = accuracy_score(y_test,train_pred)
print('Accuracy:{:.4%}'.format(acc))
log_entry = pd.DataFrame([[name,acc*100]],columns=log_cols)
log.append(log_entry)
预测
clf = LinearDiscriminantAnalysis()
clf.fit(x_train,y_train)
test_pred = clf.predict_proba(test)
使用神经网络预测
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.layers import Dense,Dropout,Activation
from keras.utils.np_utils import to_categorical
from keras.callbacks import EarlyStopping
import pandas as pd
import numpy as np
data = pd.read_csv('../树叶分类/train.csv')
parent_data = data.copy()
ID = data.pop('id')
y = data.pop('species')
y = LabelEncoder().fit(y).transform(y)
x = StandardScaler().fit(data).transform(data)
y_cat = to_categorical(y)
model = Sequential()
model.add(Dense(1500,input_dim=192,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(1500,activation='sigmoid'))
model.add(Dropout(0.1))
model.add(Dense(99,activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss',patience=280)
history = model.fit(x,y_cat,batch_size=192,
epochs=800,verbose=0,validation_split=0.1,
callbacks=[early_stopping])
test = pd.read_csv('../树叶分类/test.csv')
index = test.pop('id')
test = StandardScaler().fit(test).transform(test)
pred = model.predict(test)
sub = pd.DataFrame(pred,columns=sorted(parent_data.species.unique()))
sub.insert(0,'id',index)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)