读取csv文件
import csv #加载csv包便于读取csv文件 import numpy as np import sys from pandas import DataFrame import pandas as pd def getdata(): # 读取文件 filepath = "C:/Users/user/Downloads/" traindata = pd.read_csv(filepath + 'train.csv') testdata = pd.read_csv(filepath + 'test.csv') train = traindata[traindata['observation'] == 'PM2.5'] test = testdata[testdata['AMB_TEMP'] == 'PM2.5'] train = train.drop(['Date', 'stations', 'observation'], axis=1) test_x = test.iloc[:, 2:] train_x = [] train_y = [] for i in range(15): x = train.iloc[:, i:i + 9] x.columns = np.array(range(9)) y = train.iloc[:, i + 9] y.columns = np.array(range(1)) train_x.append(x) train_y.append(y) train_x = pd.concat(train_x) train_y = pd.concat(train_y) # train_y = np.array(train_y, float) test_x = np.array(test_x, float) print(train_x.shape, train_y.shape) return train_x,test_x,train_yLR算法
import numpy as np import math import pandas as pd from sklearn.preprocessing import StandardScaler '''损失函数''' def lossfunc(x_train,y_train,theta): x_train = np.array(x_train,float) y_train = np.array(y_train, float) return np.sum((y_train - x_train.dot(theta)) ** 2) / len(y_train) '''损失函数导数''' def dlossfunc(x_train,y_train,theta): x_train = np.array(x_train,float) y_train = np.array(y_train, float) return x_train.T.dot(x_train.dot(theta) - y_train) * 2. / len(y_train) '''归一化''' def standardfunc(data): standard = StandardScaler() standard.fit(data) data =standard.transform(data) return data '''预测''' def predict( predict_x,theta): x = np.hstack([np.ones((len(predict_x), 1)), predict_x]) result = x .dot(theta) return result '''梯度下降''' def gdmethod(x_train, y_train, ini_theta, eta, itbumber=1e4, epsilon=1e-8): theta = ini_theta inumber = 0 while inumber < itbumber: gradient = dlossfunc(x_train, y_train,theta) pretheta = theta theta = theta - eta * gradient if (abs(lossfunc( x_train, y_train,pretheta) - lossfunc( x_train, y_train,pretheta)) < epsilon): break inumber += 1 return thetamain:
import numpy as np import DataHandler import regressalgorithm train_x,test_x,train_y = DataHandler.getdata() print(train_x.shape) print(train_y.shape) print(test_x.shape) train_x = regressalgorithm.standardfunc(train_x) test_x = regressalgorithm.standardfunc(test_x) train_x = np.hstack([np.ones((len(train_x), 1)), train_x]) initial_theta = np.zeros(train_x.shape[1]) theta =regressalgorithm.gdmethod(train_x,train_y,initial_theta,0.01,1e4) predict_y = regressalgorithm.predict(test_x,theta) print(predict_y)结果文件: