import pandas as pd
import numpy as np
#input
stock = pd.read_csv('C:/Users/华硕/Desktop/沪深A股2020020.csv',encoding='GBK')
stock=stock.replace('-- ','0')
stock=stock.fillna('0')
print(stock)
#y
y = stock['涨幅%']
print(y.shape)
#X
features = stock.loc[:,'现价':'量比']
X = features.values
print(X.shape)
#MLP
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import RobustScaler
from sklearn.neural_network import MLPRegressor
mlpr=MLPRegressor(random_state=62, hidden_layer_sizes=(100,100),alpha=0.001)
X_train, X_test, y_train, y_test=train_test_split(X,y,random_state=62)
scaler = RobustScaler()
scaler.fit(X_train)
X_train_scaled = scaler.transform(X_train)
X_test_scaled = scaler.transform(X_test)
mlpr.fit(X_train_scaled, y_train)
print('模型准确率:{:.2f}'.format(mlpr.score(X_test_scaled,y_test)))
#select_rfe
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestRegressor
from sklearn.feature_selection import RFE
rfe = RFE(RandomForestRegressor(n_estimators=100,
random_state=38),
n_features_to_select=12)
rfe.fit(X_train_scaled, y_train)
mask = rfe.get_support()
print(mask)
#select_picture
plt.matshow(mask.reshape(1,-1), cmap=plt.cm.cool)
plt.xlabel('Features Selected')
plt.show()
#result_rfe
X_train_rfe = rfe.transform(X_train_scaled)
X_test_rfe = rfe.transform(X_test_scaled)
mlpr_rfe = MLPRegressor(random_state=62, hidden_layer_sizes=(100,100),
alpha=0.001)
mlpr_rfe.fit(X_train_rfe, y_train)
print("RFE选择特征后的模型得分:{:.2f}".format(mlpr_rfe.score(X_test_rfe,
y_test)))
作者:ChenBD