用c语言编写程序,读入三个数给a,b,c,然后交换他们的值,把a中原来的数给b,把b中原来的数给c,把c中原来的数给a
时间: 2024-05-02 11:21:31 浏览: 131
以下是一段关于鸢尾花的Python代码,用于绘制散点图、sigmoid函数和损失函数,并绘制决策边界。
首先,需要导入所需的库:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
```
然后,加载鸢尾花数据集:
```python
iris = load_iris()
X = iris.data[:, :2] # 只选择前两个特征,方便绘制散点图和决策边界
y = (iris.target != 0) * 1 # 将类别转换为0和1
```
接下来,定义sigmoid函数:
```python
def sigmoid(z):
return 1 / (1 + np.exp(-z))
```
然后,定义损失函数:
```python
def loss(h, y):
return (-y * np.log(h) - (1 - y) * np.log(1 - h)).mean()
```
接下来,初始化权重和偏置:
```python
w = np.zeros(X.shape[1])
b = 0
```
然后,定义迭代次数和学习率:
```python
num_iter = 100000
lr = 0.1
```
接下来,实现梯度下降算法:
```python
for i in range(num_iter):
z = np.dot(X, w) + b
h = sigmoid(z)
gradient_w = np.dot(X.T, (h - y)) / y.size
gradient_b = (h - y).mean()
w -= lr * gradient_w
b -= lr * gradient_b
```
最后,绘制散点图、sigmoid函数和决策边界:
```python
plt.scatter(X[:, 0], X[:, 1], c=y)
x1_min, x1_max = X[:, 0].min(), X[:, 0].max(),
x2_min, x2_max = X[:, 1].min(), X[:, 1].max(),
xx1, xx2 = np.meshgrid(np.linspace(x1_min, x1_max), np.linspace(x2_min, x2_max))
grid = np.c_[xx1.ravel(), xx2.ravel()]
probs = sigmoid(np.dot(grid, w) + b).reshape(xx1.shape)
plt.contour(xx1, xx2, probs, levels=[0.5], cmap="RdBu")
plt.show()
```
完整代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
阅读全文