用seaborn绘制复杂柱形图将数据集iris可视化的代码
时间: 2024-10-21 14:04:23 浏览: 42
Seaborn是一个基于matplotlib的数据可视化库,它提供了一种更高级的方式来创建复杂的统计图形。要使用Seaborn绘制Iris数据集的复杂柱状图,首先你需要安装并导入必要的库,然后加载数据。以下是一个示例代码:
```python
# 导入所需库
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
# 加载Iris数据集
iris = load_iris()
df_iris = pd.DataFrame(data=iris.data, columns=iris.feature_names)
df_iris['species'] = iris.target.astype('category').cat.codes
# 使用Seaborn画出复杂柱状图(例如箱线图+分类)
g = sns.catplot(
x="species", y="petal_length", hue="species",
kind="boxen", palette="Set3", data=df_iris,
height=6, aspect=1.5
)
# 添加标题和标签
g.set_titles("Petal Length by Species")
g.set_axis_labels("Species", "Petal Length (cm)")
plt.show()
相关问题
用python做iris数据集做可视化分析,并用来做多元线性回归和逻辑回归
在Python中,我们可以使用一些流行的数据科学库如`matplotlib`, `seaborn`, `pandas`以及机器学习库`sklearn`来进行Iris数据集的可视化分析、多元线性回归和逻辑回归。
首先,你需要导入所需的库并加载Iris数据集,通常使用的是`sklearn.datasets.load_iris`:
```python
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.metrics import accuracy_score
# 加载Iris数据集
iris = datasets.load_iris()
X = iris.data
y = iris.target
```
然后,你可以对数据进行基本描述性统计和可视化,比如绘制散点图或者箱线图展示特征之间的关系:
```python
sns.pairplot(iris, hue="target")
plt.show()
# 或者针对某一列绘制成柱状图
plt.figure(figsize=(10,6))
sns.countplot(x='species', data=iris)
plt.xlabel('Species')
plt.ylabel('Count')
plt.title('Distribution of Iris Species')
plt.show()
```
接下来,我们将数据分为训练集和测试集:
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
```
对于多元线性回归,可以创建模型并进行拟合:
```python
linear_reg = LinearRegression()
linear_reg.fit(X_train, y_train)
# 预测并评估结果
y_pred = linear_reg.predict(X_test)
print("Linear Regression Coefficients:", linear_reg.coef_)
accuracy = r2_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100}%")
```
逻辑回归用于分类问题,替换掉上述的`LinearRegression`,使用`LogisticRegression`:
```python
logistic_reg = LogisticRegression()
logistic_reg.fit(X_train, y_train)
y_pred_logreg = logistic_reg.predict(X_test)
print("Logistic Regression Coefficients (Intercept and Features):", logistic_reg.intercept_, logistic_reg.coef_)
accuracy_logreg = accuracy_score(y_test, y_pred_logreg)
print(f"Accuracy with Logistic Regression: {accuracy_logreg * 100}%")
```
阅读全文
相关推荐

















