生成Python代码进行有监督学习训练:选择SVM支持向量机、逻辑回归、DNN神经网络、XGboost、KNN分类器。
时间: 2023-12-11 14:06:04 浏览: 115
python机器学习支持向量机SVM求解鸢尾花TensorFlow分类问题数据集iris及代码
5星 · 资源好评率100%
以下是使用Python代码进行有监督学习训练的示例,包括选择SVM支持向量机、逻辑回归、DNN神经网络、XGboost、KNN分类器:
1. SVM支持向量机
```python
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载数据集
X, y = load_data()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建SVM模型
svm_model = svm.SVC(kernel='linear')
# 训练模型
svm_model.fit(X_train, y_train)
# 预测测试集
y_pred = svm_model.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("SVM模型准确率为:", accuracy)
```
2. 逻辑回归
```python
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载数据集
X, y = load_data()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建逻辑回归模型
logistic_model = LogisticRegression()
# 训练模型
logistic_model.fit(X_train, y_train)
# 预测测试集
y_pred = logistic_model.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("逻辑回归模型准确率为:", accuracy)
```
3. DNN神经网络
```python
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载数据集
X, y = load_data()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建DNN模型
dnn_model = Sequential()
dnn_model.add(Dense(32, input_dim=X.shape[1], activation='relu'))
dnn_model.add(Dense(1, activation='sigmoid'))
# 编译模型
dnn_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# 训练模型
dnn_model.fit(X_train, y_train, epochs=50, batch_size=32)
# 预测测试集
y_pred = dnn_model.predict_classes(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("DNN模型准确率为:", accuracy)
```
4. XGboost
```python
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载数据集
X, y = load_data()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建XGboost模型
xgb_model = xgb.XGBClassifier()
# 训练模型
xgb_model.fit(X_train, y_train)
# 预测测试集
y_pred = xgb_model.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("XGboost模型准确率为:", accuracy)
```
5. KNN分类器
```python
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 加载数据集
X, y = load_data()
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建KNN模型
knn_model = KNeighborsClassifier()
# 训练模型
knn_model.fit(X_train, y_train)
# 预测测试集
y_pred = knn_model.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("KNN模型准确率为:", accuracy)
```
以上是使用Python代码进行有监督学习训练的示例,其中包括了SVM支持向量机、逻辑回归、DNN神经网络、XGboost、KNN分类器的代码实现。具体选择哪种算法,需要根据具体的数据集和任务来决定。
阅读全文