鸢尾花的聚类和分类算法代码,天气预测的回归算法代码
时间: 2023-10-03 15:02:26 浏览: 107
以下是使用Python实现鸢尾花数据集的K-Means聚类和决策树分类算法代码:
```
# 导入所需的库
from sklearn.datasets import load_iris
from sklearn.cluster import KMeans
from sklearn.tree import DecisionTreeClassifier
# 加载数据集
iris = load_iris()
# 使用K-Means算法进行聚类
kmeans = KMeans(n_clusters=3, random_state=0)
kmeans.fit(iris.data)
# 输出聚类结果
print(kmeans.labels_)
# 使用决策树算法进行分类
dtc = DecisionTreeClassifier(random_state=0)
dtc.fit(iris.data, iris.target)
# 输出分类结果
print(dtc.predict(iris.data))
```
以下是使用Python实现天气预测数据集的线性回归算法代码:
```
# 导入所需的库
import pandas as pd
from sklearn.linear_model import LinearRegression
# 加载数据集
weather = pd.read_csv('weather.csv')
# 提取特征和标签
X = weather[['temperature', 'humidity']]
y = weather['pressure']
# 使用线性回归算法进行回归
lr = LinearRegression()
lr.fit(X, y)
# 输出回归系数和截距
print('Coefficients:', lr.coef_)
print('Intercept:', lr.intercept_)
# 预测新的数据
new_data = [[20, 60], [25, 70]]
print('Predictions:', lr.predict(new_data))
```
需要注意的是,以上代码仅供参考,实际应用中可能需要根据具体情况进行调整。
阅读全文
相关推荐
















