标签:shu 输入 方法 意思 ESS values spro 输入数据 解决方法
实现代码:
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression from sklearn.datasets import load_boston from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error from sklearn.externals import joblib from sklearn.metrics import r2_score from sklearn.neural_network import MLPRegressor import pandas as pd import numpy as np lb = load_boston() # train_test_split(train_data,train_target,test_size=0.3,random_state=5) #train_data:待划分样本数据 #train_target:待划分样本数据的结果(标签) #test_size:测试数据占样本数据的比例,若整数则样本数量 #random_state:设置随机数种子,保证每次都是同一个随机数。若为0或不填,则每次得到数据都不一样 #train_test_split()函数是用来随机划分样本数据为训练集和测试集的,当然也可以人为的切片划分 x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.2) # 为数据增加一个维度,相当于把[1, 5, 10] 变成 [[1, 5, 10],] y_train = y_train.reshape(-1, 1) y_test = y_test.reshape(-1, 1) # 进行标准化 std_x = StandardScaler() x_train = std_x.fit_transform(x_train) x_test = std_x.transform(x_test) std_y = StandardScaler() y_train = std_y.fit_transform(y_train) y_test = std_y.transform(y_test) # 正规方程预测 #最小二乘法线性回归 lr = LinearRegression() #fit_transform方法是fit和transform的结合,fit_transform(X_train) 意思是找出X_train的均值和标准差,并应用在X_train上 lr.fit(x_train, y_train) print("r2 score of Linear regression is",r2_score(y_test,lr.predict(x_test))) #岭回归 from sklearn.linear_model import RidgeCV #岭回归模型 cv = RidgeCV(alphas=np.logspace(-3, 2, 100)) cv.fit (x_train , y_train) print("r2 score of Linear regression is",r2_score(y_test,cv.predict(x_test))) #梯度下降 用于判断使用凸loss函数(convex loss function)的分类器 sgd = SGDRegressor() #一个数组X(其size为[n_samples, n_features]):保存着训练样本;一个数组Y:保存着训练样本的target值(class label): sgd.fit(x_train, y_train) print("r2 score of Linear regression is",r2_score(y_test,sgd.predict(x_test))) from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense #基准NN #使用标准化后的数据 seq = Sequential() #构建神经网络模型 #input_dim来隐含的指定输入数据shape seq.add(Dense(64, activation=‘relu‘,input_dim=lb.data.shape[1])) seq.add(Dense(64, activation=‘relu‘)) seq.add(Dense(1, activation=‘relu‘)) seq.compile(optimizer=‘rmsprop‘, loss=‘mse‘, metrics=[‘mae‘]) seq.fit(x_train, y_train, epochs=300, batch_size = 16, shuffle = False) score = seq.evaluate(x_test, y_test,batch_size=16) #loss value & metrics values print("score:",score) print(‘r2 score:‘,r2_score(y_test, seq.predict(x_test)))
运行结果:
正规方程预测:
岭回归结果:
梯队下降:
最终结果:
遇到的问题及解决方法:
原因:
tensorflow 版本过高,该函数已经整合到tensorflow当中。
解决方法:
由
from keras.models import Sequential from keras.layers import Dense
改为:
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense
标签:shu 输入 方法 意思 ESS values spro 输入数据 解决方法
原文地址:https://www.cnblogs.com/cxy0210/p/14533730.html