一、boston房价预测
#1.读取数据from sklearn.datasets import load_boston #导入房价数据集boston=load_boston() boston.data #读取房价数据boston.targetboston.data.shape
#2. 训练集与测试集划分from sklearn.cross_validation import train_test_splitx_train, x_test, y_train, y_test = train_test_split(boston.data,boston.target,test_size=0.3)x_train.shapey_train.shape
#3.线性回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。from sklearn.linear_model import LinearRegressionLineR=LinearRegression() #线性回归LineR.fit(x_train,y_train) #对数据进行训练print(LineR.coef_,LineR.intercept_) #通过数据训练得出回归方程的斜率和截距from sklearn.metrics import regression # 检测模型好坏y_pred= LineR.predict(x_test)print("预测的均方误差:", regression.mean_squared_error(y_test,y_pred)) # 计算模型的预测指标print("预测的平均绝对误差:", regression.mean_absolute_error(y_test,y_pred))print("模型的分数:",LineR.score(x_test, y_test)) # 输出模型的分数
#4.多项式回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。from sklearn.preprocessing import PolynomialFeaturespoly=PolynomialFeatures(degree=2)x_poly_train=poly.fit_transform(x_train)LineR=LinearRegression() #建立多项回归模型LineR.fit(x_poly_train,y_train) x_poly_test=poly.transform(x_test) #多项回归预测模型y_pred1=LineR.predict(x_poly_test)# 检测模型好坏print("预测的均方误差:", regression.mean_squared_error(y_test,y_pred1))print("预测的平均绝对误差:", regression.mean_absolute_error(y_test,y_pred1)) # 计算模型的预测指标print("模型的分数:",LineR.score(x_poly_test, y_test)) # 输出模型的分数
5. 比较线性模型与非线性模型的性能,并说明原因:
非线性模型的模型性能较好,因为它是有很多点连接而成的曲线,对样本的拟合程度较高,而且多项式模型是一条平滑的曲线,更贴合样本点的分布,预测效果误差较小。
二、中文文本分类
import osimport numpy as npimport sysfrom datetime import datetimeimport gcpath = 'F:\\jj147'# 导入结巴库,并将需要用到的词库加进字典import jieba# 导入停用词:with open(r'F:\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 tokenstokenList = []targetList = []# 用os.walk获取需要的变量,并拼接文件路径再打开每一个文件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))#划分训练集和测试,用TF-IDF算法进行单词权值的计算from sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.model_selection import train_test_splitvectorizer= TfidfVectorizer()x_train,x_test,y_train,y_test=train_test_split(tokenList,targetList,test_size=0.2)X_train=vectorizer.fit_transform(x_train)X_test=vectorizer.transform(x_test)#构建贝叶斯模型from sklearn.naive_bayes import MultinomialNB #用于离散特征分类,文本分类单词统计,以出现的次数作为特征值mulp=MultinomialNB ()mulp_NB=mulp.fit(X_train,y_train)#对模型进行预测y_predict=mulp.predict(X_test)# # 从sklearn.metrics里导入classification_report做分类的性能报告from sklearn.metrics import classification_reportprint('模型的准确率为:', mulp.score(X_test, y_test))print('classification_report:\n',classification_report(y_test, y_predict))
# 将预测结果和实际结果进行对比import collectionsimport matplotlib.pyplot as plt# 统计测试集和预测集的各类新闻个数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)