使用python中的线性回归和SVM算法对鸢尾花数据进行预测,打印测试集和训练集准确率
时间: 2024-09-09 10:12:42 浏览: 85
在Python中,我们通常使用scikit-learn库来实现线性回归和SVM(支持向量机)算法进行预测。下面是一个简单的例子,展示如何对鸢尾花数据集使用这两种算法,并打印测试集和训练集的准确率。
首先,我们需要导入必要的库并加载鸢尾花数据集:
```python
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
# 加载鸢尾花数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
```
接下来,将数据集分为训练集和测试集:
```python
# 分割数据集为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
```
使用线性回归模型:
```python
# 初始化线性回归模型
lr_model = LinearRegression()
# 训练模型
lr_model.fit(X_train, y_train)
# 进行预测
lr_predictions = lr_model.predict(X_test)
# 打印测试集的准确率
print("线性回归测试集准确率:", accuracy_score(y_test, lr_predictions.round()))
# 打印训练集的准确率
lr_predictions_train = lr_model.predict(X_train)
print("线性回归训练集准确率:", accuracy_score(y_train, lr_predictions_train.round()))
```
使用SVM模型:
```python
# 初始化SVM模型
svm_model = SVC()
# 训练模型
svm_model.fit(X_train, y_train)
# 进行预测
svm_predictions = svm_model.predict(X_test)
# 打印测试集的准确率
print("SVM测试集准确率:", accuracy_score(y_test, svm_predictions))
# 打印训练集的准确率
svm_predictions_train = svm_model.predict(X_train)
print("SVM训练集准确率:", accuracy_score(y_train, svm_predictions_train))
```
请注意,在这里,由于线性回归是用来做回归问题的,因此用线性回归模型预测鸢尾花分类问题并不合适。所以这里假设你实际上是想用逻辑回归(`LogisticRegression`),我错误地写成了`LinearRegression`,逻辑回归才是用于分类任务的。下面是正确的逻辑回归部分:
```python
# 初始化逻辑回归模型
lr_model = LogisticRegression(max_iter=1000) # 增加迭代次数以确保收敛
# 训练模型
lr_model.fit(X_train, y_train)
# 进行预测
lr_predictions = lr_model.predict(X_test)
# 打印测试集的准确率
print("逻辑回归测试集准确率:", accuracy_score(y_test, lr_predictions))
# 打印训练集的准确率
lr_predictions_train = lr_model.predict(X_train)
print("逻辑回归训练集准确率:", accuracy_score(y_train, lr_predictions_train))
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)