SHMT / test4.py
zeroMN's picture
Upload 4 files
7120259 verified
raw
history blame
2.09 kB
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.feature_extraction.text import TfidfVectorizer
import matplotlib.pyplot as plt
import seaborn as sns
# 数据集 URL
data_url = 'https://archive.ics.uci.edu/static/public/591/data.csv'
# 加载数据集
df = pd.read_csv(data_url)
# 查看数据集的前几行
print("数据集的前几行:")
print(df.head())
# 数据预处理
# 将 Gender 列中的 M 和 F 转换为 1 和 0
df['Gender'] = df['Gender'].map({'M': 1, 'F': 0})
# 特征和目标
X = df[['Name', 'Count', 'Probability']] # 特征
y = df['Gender'] # 目标
# 使用 TfidfVectorizer 对 Name 特征进行处理
vectorizer = TfidfVectorizer()
X_name = vectorizer.fit_transform(X['Name'])
# 将 Count 和 Probability 特征与 Name 特征合并
import scipy
X_combined = scipy.sparse.hstack((X_name, X[['Count', 'Probability']].values))
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_combined, y, test_size=0.2, random_state=42)
# 训练模型
model = RandomForestClassifier(random_state=42)
model.fit(X_train, y_train)
# 预测
y_pred = model.predict(X_test)
# 输出分类报告
print("\n分类报告:")
print(classification_report(y_test, y_pred))
# 可视化混淆矩阵
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', xticklabels=['Female', 'Male'], yticklabels=['Female', 'Male'])
plt.ylabel('Actual')
plt.xlabel('Predicted')
plt.title('Confusion Matrix')
plt.show()
#############################################
from ucimlrepo import fetch_ucirepo
# fetch dataset
gender_by_name = fetch_ucirepo(id=591)
# data (as pandas dataframes)
X = gender_by_name.data.features
y = gender_by_name.data.targets
# metadata
print(gender_by_name.metadata)
# variable information
print(gender_by_name.variables)