一、boston房价预测
# 1.读取数据集 from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split data = load_boston
# 2.训练集与测试集划分 x_train, x_test, y_train, y_test = train_test_split(data.data,data.target,test_size=0.3)
#3.线性回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。 #建立模型 from sklearn.linear_model import LinearRegression mlr = LinearRegression() mlr.fit(x_train,y_train) print('系数',mlr.coef_,"\n截距",mlr.intercept_)
#检测模型好坏 from sklearn.metrics import regression y_predict = mlr.predict(x_test) print('线性回归模型:') print("预测的均方误差:",regression.mean_squared_error(y_test,y_predict)) print("预测的平均绝对误差:",regression.mean_absolute_error(y_test,y_predict)) print("模型的分数:",mlr.score(x_test,y_test))
#4.多项式回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏 from sklearn.preprocessing import PolynomialFeatures # 多项式化 poly2 =PolynomialFeatures(degree=2) x_poly_train = poly2.fit_transform(x_train) x_poly_test = poly2.transform(x_test)
# 建立模型 mlrp = LinearRegression() mlrp.fit(x_poly_train, y_train)
# 测模型好坏 y_predict2 = mlrp.predict(x_poly_test) print("多项式回归模型:") print("预测的均方误差:",regression.mean_squared_error(y_test,y_predict2)) print("预测平均绝对误差:",regression.mean_absolute_error(y_test,y_predict2)) print("模型的分数:",mlrp.score(x_poly_test,y_test))
#5.比较线性模型与非线性模型的性能,并说明原因 多项式回归模型误差比线性模型小,而且是一条平滑的曲线,对样本的拟合程度较高,所以非线性模型的性能比线性的性能要好。
二、中文文本分类
import os import numpy as np import sys from datetime import datetime import gc path = 'E:\\147'
import jieba # 导入停用词: with open(r'E:\\stopsCN.txt',encoding='utf-8') as f: stopwords = f.read().split('\n')
def processing(tokens): # 去掉非字母汉字的字符 tokens = "".join([char for char in tokens if char.isalpha()]) # 结巴分词 tokens = [token for token in jieba.cut(tokens,cut_all=True) if len(token) >=2] # 去掉停用词 tokens = " ".join([token for token in tokens if token not in stopwords]) return tokens
tokenList = [] targetList = [] for root,dirs,files in os.walk(path): for f in files: filePath = os.path.join(root,f) with open(filePath,encoding='utf-8') as f: content = f.read() # 获取新闻类别标签,并处理该新闻 target = filePath.split('\\')[-2] targetList.append(target) tokenList.append(processing(content))
from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.model_selection import train_test_split from sklearn.naive_bayes import GaussianNB,MultinomialNB from sklearn.model_selection import cross_val_score from sklearn.metrics import classification_report
# 划分训练集和测试集 x_train,x_test,y_train,y_test = train_test_split(tokenList,targetList,test_size=0.3,stratify=targetList)
# 文本特征提取 vectorizer = TfidfVectorizer() X_train = vectorizer.fit_transform(x_train) X_test = vectorizer.transform(x_test)
from sklearn.naive_bayes import MultinomialNB # 多项式朴素贝叶斯 mnb = MultinomialNB() module = mnb.fit(X_train,y_train)
y_predict = module.predict(X_test) # 对数据进行5次分割 scores=cross_val_score(mnb,X_test,y_test,cv=5) print("Accuracy:%.3f"%scores.mean()) print("classification_report:\n",classification_report(y_predict,y_test))
targetList.append(target) print(targetList[0:10]) tokenList.append(processing(content)) tokenList[0:10]
import collections # 统计测试集和预测集的各类新闻个数 testCount = collections.Counter(y_test) predCount = collections.Counter(y_predict) print('实际:',testCount,'\n', '预测', predCount) # 建立标签列表,实际结果列表,预测结果列表, nameList = list(testCount.keys()) testList = list(testCount.values()) predictList = list(predCount.values()) x = list(range(len(nameList))) print("新闻类别:",nameList,'\n',"实际:",testList,'\n',"预测:",predictList)