from sklearn.feature_extraction.text import CountVectorizer from sklearn.model_selection import train_test_split from sklearn.naive_bayes import MultinomialNB from sklearn import metrics
# 示例数据 documents = ["This is a spam email", "This is a normal email", "Buy now!", "Meeting at noon"] labels = [1, 0, 1, 0] # 1表示垃圾邮件,0表示正常邮件
# 特征提取 vectorizer = CountVectorizer() X = vectorizer.fit_transform(documents) y = labels
# 拆分数据集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
# 训练模型 model = MultinomialNB() model.fit(X_train, y_train)
from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error
# 加载数据 boston = load_boston() X, y = boston.data, boston.target
# 数据拆分 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# 训练模型 model = LinearRegression() model.fit(X_train, y_train)