利用sklearn中的make_bolbs()函数随机生成200个样本数据,样本数据特征数为3个,然后利用sklearn 中的KMeans()函数进行聚类分析,并将结果显示出来。
时间: 2024-01-22 15:18:00 浏览: 31
好的,下面是代码实现:
```python
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# 生成随机样本数据
X, y = make_blobs(n_samples=200, n_features=3, centers=4, random_state=42)
# KMeans聚类分析
kmeans = KMeans(n_clusters=4, random_state=42)
kmeans.fit(X)
# 绘制聚类结果
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_)
plt.show()
```
解释一下代码:
1. 我们首先调用`make_blobs()`函数生成了200个样本数据,每个样本数据包含3个特征,共分为4类。
2. 然后我们调用`KMeans()`函数进行聚类分析,设置聚类数为4。
3. 最后我们将聚类结果绘制在二维平面上,通过不同颜色的散点图来表示不同的聚类类别。
运行以上代码,你应该可以看到一张散点图,图中的不同颜色代表不同的聚类类别。
相关问题
通过sklearn中的make_blobs函数生成数据集,进行聚类操作,任选另一种聚类算法完成聚类,并进行可视化
好的,我理解了你的问题。你可以使用KMeans算法代替make_blobs函数生成的数据集进行聚类操作。首先,导入必要的库:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from sklearn.cluster import KMeans
```
然后,使用make_blobs函数生成数据集:
```python
X, y = make_blobs(n_samples=300, centers=4, cluster_std=0.60, random_state=0)
```
该函数生成300个样本,4个中心,标准差为0.6的数据集。下一步是使用KMeans聚类算法来对数据进行聚类,代码如下:
```python
kmeans = KMeans(n_clusters=4)
kmeans.fit(X)
```
接下来,我们将绘制聚类结果图。代码如下:
```python
plt.scatter(X[:, 0], X[:, 1], c=kmeans.labels_, cmap='viridis')
centers = kmeans.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1], c='black', s=200, alpha=0.5);
```
该代码使用散点图显示数据集,并用不同颜色表示不同的聚类,中心点用黑色实心圆表示。这样就可以可视化聚类结果了。
用sklearn mysql_sklearn 生成随机数据
你可以使用sklearn库中的make_classification和make_regression函数生成随机数据,再使用mysql_sklearn库中的pandas_to_mysql函数将数据存储到MySQL数据库中。
以下是一个简单的例子:
```python
from sklearn.datasets import make_classification, make_regression
from mysql_sklearn import pandas_to_mysql
import pandas as pd
import mysql.connector
# 生成分类数据
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2)
# 将数据转换为DataFrame
df = pd.DataFrame(data=X, columns=[f"feature_{i}" for i in range(X.shape[1])])
df["target"] = y
# 将数据存储到MySQL数据库中
conn = mysql.connector.connect(user='your_username', password='your_password',
host='your_host', database='your_database')
cursor = conn.cursor()
pandas_to_mysql(df, cursor, 'classification_data')
conn.commit()
# 生成回归数据
X, y = make_regression(n_samples=1000, n_features=5)
# 将数据转换为DataFrame
df = pd.DataFrame(data=X, columns=[f"feature_{i}" for i in range(X.shape[1])])
df["target"] = y
# 将数据存储到MySQL数据库中
pandas_to_mysql(df, cursor, 'regression_data')
conn.commit()
# 关闭数据库连接
cursor.close()
conn.close()
```
这个例子中,我们使用make_classification函数生成了1000个样本,每个样本包含10个特征和一个目标变量(二分类),然后将数据存储到名为classification_data的MySQL表中。接着,我们使用make_regression函数生成了1000个样本,每个样本包含5个特征和一个目标变量(回归),然后将数据存储到名为regression_data的MySQL表中。你可以根据自己的需要修改这个例子。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)