# demo1. 使用tfidf 处理新闻数据, 并制作模型
# 导入数据集
from sklearn.datasets import fetch_20newsgroups
# 导入分割数据集函数
from sklearn.model_selection import train_test_split
# 导入特征工程的类
from sklearn.feature_extraction.text import TfidfVectorizer
# 导入估计器对象
from sklearn.naive_bayes import MultinomialNB
# 导入波士顿房价数据
from sklearn.datasets import load_boston
# 导入标准化的数据集
from sklearn.preprocessing import StandardScaler
# 导入线性回归模型
from sklearn.linear_model import LinearRegression
# 导入损失方程
from sklearn.metrics import mean_squared_error
def mlbcls():
"""
tfidf 处理数据,制作朴素贝叶斯算法
:return:
"""
# 1.获取数据集
new = fetch_20newsgroups(subset="all")
# 2.确认好特征值与目标值
x = new.data
y = new.target
# 3.分割数据集
x_train,x_test, y_train, y_test = train_test_split(x, y,test_size=0.3)
# 4.特征工程处理数据
tfidf = TfidfVectorizer()
x_train = tfidf.fit_transform(x_train)
x_test = tfidf.transform(x_test)
# 5.导入估计器对象制作模型
mlb = MultinomialNB()
mlb.fit(x_train, y_train)
# 6. 计算准确度
score = mlb.score(x_test, y_test)
print("模型准确度: ",score)
return None
# mlbcls()
def line():
"""
线性回归,预测波士顿房价
:return:
"""
# 1.获取数据集
lb = load_boston()
# 2. 确定特征值与目标值
x = lb.data
y = lb.target
# 3. 分割数据集
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3)
# 4. 特征工程处理数据
std = StandardScaler()
x_train = std.fit_transform(x_train)
x_test = std.transform(x_test)
# 5. 创建估计器
liner = LinearRegression(fit_intercept=True)
liner.fit(x_train, y_train)
print("模型的回归系数:", liner.coef_)
print("模型的偏置:", liner.intercept_)
# 6.计算模型的损失
y_predict = liner.predict(x_test)
error = mean_squared_error(y_test, y_predict)
print("模型的损失:", error)
return None
line()
demo1:使用tfidf 处理新闻数据, 并制作模型
最新推荐文章于 2024-09-01 23:36:16 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)