RandomForestClassifier随机森林分类预测
RandomForestClassifier用于分类划分预测回归
##开始建立模型,随机森林预测
from sklearn.ensemble import RandomForestClassifier ##
rf = RandomForestClassifier(n_estimators=150,min_samples_leaf=2,max_depth=6,oob_score=True)
rf.fit(train_data_X,train_data_Y)
rf.oob_score_ ##包外评估分数
test_data_X=test_data_X.drop(['Name','Ticket'],axis=1)
test["Survived"] = rf.predict(test_data_X)
svm分类
注意这是svm中的一个分类器。linearSVC是svm的一个线性分类器
from sklearn.svm import SVC, LinearSVC
svc = SVC()
svc.fit(X_train, Y_train)
Y_pred = svc.predict(X_test)
acc_svc = round(svc.score(X_train, Y_train) * 100, 2) #测试得分
acc_svc
KNN分类
样本按其邻居的多数票进行分类,样本被分配给其k个最近邻居中最常见的类(k是正整数,通常很小)。 如果k = 1,则简单地将对象分配给该单个最近邻居的类。
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors = 3)
knn.fit(X_train, Y_train)
Y_pred = knn.predict(X_test)
acc_knn = round(knn.score(X_train, Y_train) * 100, 2)
acc_knn #得分
贝叶斯分类
朴素贝叶斯分类器是一系列简单的概率分类器,它基于贝叶斯定理应用特征之间的强(朴素)独立假设。 朴素贝叶斯分类器具有高度可扩展性,在学习问题中需要多个变量(特征)数量的线性参数。
# Gaussian Naive Bayes
from sklearn.naive_bayes import GaussianNB
gaussian = GaussianNB()
gaussian.fit(X_train, Y_train)
Y_pred = gaussian.predict(X_test)
acc_gaussian = round(gaussian.score(X_train, Y_train) * 100, 2)
acc_gaussian #得分
感知器
感知器是用于二元分类器的监督学习的算法(可以决定由数字向量表示的输入是否属于某个特定类的函数)。 它是一种线性分类器,即分类算法,其基于将一组权重与特征向量组合的线性预测器函数进行其预测。 该算法允许在线学习,因为它一次一个地处理训练集中的元素。
# Perceptron
from sklearn.linear_model import Perceptron
from sklearn.linear_model import SGDClassifier
from sklearn.tree import DecisionTreeClassifier
perceptron = Perceptron()
perceptron.fit(X_train, Y_train)
Y_pred = perceptron.predict(X_test)
acc_perceptron = round(perceptron.score(X_train, Y_train) * 100, 2)
acc_perceptron #得分
# Linear SVC
from sklearn.svm import SVC, LinearSVC
linear_svc = LinearSVC()
linear_svc.fit(X_train, Y_train)
Y_pred = linear_svc.predict(X_test)
acc_linear_svc = round(linear_svc.score(X_train, Y_train) * 100, 2)
acc_linear_svc #得分
# Stochastic Gradient Descent
from sklearn.linear_model import SGDClassifier
sgd = SGDClassifier()
sgd.fit(X_train, Y_train)
Y_pred = sgd.predict(X_test)
acc_sgd = round(sgd.score(X_train, Y_train) * 100, 2)
acc_sgd #得分
决策树
# Decision Tree
from sklearn.tree import DecisionTreeClassifier
decision_tree = DecisionTreeClassifier()
decision_tree.fit(X_train, Y_train)
Y_pred = decision_tree.predict(X_test)
acc_decision_tree = round(decision_tree.score(X_train, Y_train) * 100, 2)
acc_decision_tree #得分86.76