#线性回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。
from sklearn.datasets import load_boston
import matplotlib.pyplot as plt from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split boston=load_boston()#导入数据集 x = boston.data y = boston.target x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)#划分训练集和测试集 lineR=LinearRegression()#线性模型 lineR.fit(x_train,y_train) #判断模型的好坏 print('预测的准确率:',lineR.score(x_test,y_test))
#4. 多项式回归模型:建立13个变量与房价之间的预测模型,并检测模型好坏。
from sklearn.preprocessing import PolynomialFeatures
poly=PolynomialFeatures(degree=2) from sklearn.linear_model import LinearRegression lineR=LinearRegression() x= boston.data y = boston.target x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3)#划分训练集和测试集 #多项式操作 x_train_poly=poly.fit_transform(x_train) x_test_poly=poly.transform(x_test) lineR.fit(x_train_poly,y_train)#建立模型 print('预测的准确率:',lineR.score(x_test_poly,y_test)) #图形化 from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt lineR=LinearRegression() lineR.fit(x_train_poly,y_train) y_poly_pred=lineR.predict(x_test_poly) plt.plot(y,y,'r') plt.scatter(y_test,y_poly_pred) plt.show()
#新闻文本分类 import os import jieba #读取文件内容 content=[]#存放新闻的内容 label=[]#存放新闻的类别 def read_txt(path): folder_list=os.listdir(path)#遍历data下的文件名 for file in folder_list: new_path=os.path.join(path,file) #读取文件夹的名称,生成新的路径 files=os.listdir(new_path)#存放文件的内容 #遍历每个txt文件 for f in files: with open(os.path.join(new_path,f),'r',encoding='UTF-8')as f: #打开txt文件 temp_file=f.read() content.append(processing(temp_file)) label.append(file) #对数据进行预处理 with open(r'stopsCN.txt', encoding='utf-8') as f: stopwords = f.read().split('\n') def processing(texts): # 去掉非法的字符 texts = "".join([char for char in texts if char.isalpha()]) # 用jieba分词 texts = [text for text in jieba.cut(texts,cut_all=True) if len(text) >=2] # 去掉停用词 texts = " ".join([text for text in texts if text not in stopwords]) return texts if __name__== '__main__': path='data'
read_txt(path)
#划分训练集和测试,用TF-IDF算法进行单词权值的计算 from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.model_selection import train_test_split,cross_val_score#用于划分数据集和交叉验证 tfidf= TfidfVectorizer() x_train,x_test,y_train,y_test=train_test_split(content,label,test_size=0.2)#划分训练集和测试集 X_train=tfidf.fit_transform(x_train) X_test=tfidf.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_report scores = cross_val_score(mulp,X_test,y_test,cv=5,scoring='accuracy')#交叉验证输出模型的精确度 print('模型的准确率为:', scores.mean()) print('classification_report:\n',classification_report(y_test, y_predict))#输出分类的性能报告
#将测试数据和预测结果进行比较 import collections y_test_total=collections.Counter(y_test) pre_total=collections.Counter(y_predict) print('实际:',y_test_taotal,'\n','预测:',pre_total) news_list=list(y_test_total.keys()) test_list=list(y_test_total.values()) pre_list=list(pre_total.values()) print('类别:',news_list,'\n','实际:',test_list,'\n','预测:',pre_list,)
线性模型与非线性模型性能的区别:
一个模型如果是线性的,就意味着它的参数项要么是常数,要么是原参数和要预测的特征之间的乘积加和就是我们要预测的值。