生成两组线性均匀分布的数据(完全线性可分)。 生成两组线性均匀分布的数据(线性不可分)。 生成两组高斯分布的数据(完全线性可分)。 生成两组高斯分布的数据(线性不可分)。 使用 K-means聚类程序进行二分类。
时间: 2024-03-02 07:50:50 浏览: 19
好的,我明白了。以下是我的回答:
1. 生成两组线性均匀分布的数据(完全线性可分):
可以使用numpy库中的random模块来生成数据。代码如下:
```python
import numpy as np
# 生成第一组数据
np.random.seed(0)
mean1 = [0, 0]
cov1 = [[1, 0], [0, 1]]
data1 = np.random.multivariate_normal(mean1, cov1, 100)
label1 = np.zeros((100, 1))
# 生成第二组数据
np.random.seed(1)
mean2 = [5, 5]
cov2 = [[1, 0], [0, 1]]
data2 = np.random.multivariate_normal(mean2, cov2, 100)
label2 = np.ones((100, 1))
# 合并数据
data = np.concatenate((data1, data2), axis=0)
label = np.concatenate((label1, label2), axis=0)
```
2. 生成两组线性均匀分布的数据(线性不可分):
可以使用numpy库中的random模块来生成数据,并在其中加入一些随机噪声。代码如下:
```python
import numpy as np
# 生成第一组数据
np.random.seed(0)
mean1 = [0, 0]
cov1 = [[1, 0], [0, 1]]
data1 = np.random.multivariate_normal(mean1, cov1, 100)
label1 = np.zeros((100, 1))
# 生成第二组数据
np.random.seed(1)
mean2 = [3, 3]
cov2 = [[1, 0], [0, 1]]
data2 = np.random.multivariate_normal(mean2, cov2, 100)
label2 = np.ones((100, 1))
# 在数据中加入噪声
noise1 = np.random.uniform(-2, 2, (100, 2))
data1 += noise1
noise2 = np.random.uniform(-2, 2, (100, 2))
data2 += noise2
# 合并数据
data = np.concatenate((data1, data2), axis=0)
label = np.concatenate((label1, label2), axis=0)
```
3. 生成两组高斯分布的数据(完全线性可分):
可以使用numpy库中的random模块来生成数据。代码如下:
```python
import numpy as np
# 生成第一组数据
np.random.seed(0)
mean1 = [-2, -2]
cov1 = [[1, 0], [0, 1]]
data1 = np.random.multivariate_normal(mean1, cov1, 100)
label1 = np.zeros((100, 1))
# 生成第二组数据
np.random.seed(1)
mean2 = [2, 2]
cov2 = [[1, 0], [0, 1]]
data2 = np.random.multivariate_normal(mean2, cov2, 100)
label2 = np.ones((100, 1))
# 合并数据
data = np.concatenate((data1, data2), axis=0)
label = np.concatenate((label1, label2), axis=0)
```
4. 生成两组高斯分布的数据(线性不可分):
可以使用numpy库中的random模块来生成数据,并在其中加入一些随机噪声。代码如下:
```python
import numpy as np
# 生成第一组数据
np.random.seed(0)
mean1 = [-2, -2]
cov1 = [[1, 0], [0, 1]]
data1 = np.random.multivariate_normal(mean1, cov1, 100)
label1 = np.zeros((100, 1))
# 生成第二组数据
np.random.seed(1)
mean2 = [2, 2]
cov2 = [[1, 0], [0, 1]]
data2 = np.random.multivariate_normal(mean2, cov2, 100)
label2 = np.ones((100, 1))
# 在数据中加入噪声
noise1 = np.random.uniform(-2, 2, (100, 2))
data1 += noise1
noise2 = np.random.uniform(-2, 2, (100, 2))
data2 += noise2
# 合并数据
data = np.concatenate((data1, data2), axis=0)
label = np.concatenate((label1, label2), axis=0)
```
5. 使用K-means聚类程序进行二分类:
可以使用sklearn库中的KMeans模块来进行聚类。代码如下:
```python
from sklearn.cluster import KMeans
# 定义K-means模型
kmeans = KMeans(n_clusters=2, random_state=0)
# 训练模型
kmeans.fit(data)
# 预测结果
pred = kmeans.predict(data)
# 计算准确率
acc = np.sum(np.equal(pred, label).astype(np.float32)) / len(label)
print('Accuracy:', acc)
```
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)