Demo entry 6734682

代码

   

Submitted by anonymous on Apr 21, 2018 at 11:17
Language: Python 3. Code size: 2.2 kB.

#-*- coding: UTF-8 -*-
import time
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import mean_squared_error
from sklearn.metrics import mean_absolute_error
import sys
print("特征数目:", sys.argv[1])

pdata = pd.read_csv(r'TJU_for_SelectFeature.csv',names = ["V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13","V14","V15","V16","V17","V18","V19","V20","V21","V22","V23","V24","V25","V26","V27","V28","thrust"])
y = pdata['thrust'].astype(int)
x = pdata.drop('thrust',axis=1)

#特征选择
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.preprocessing import MinMaxScaler

#[0,1]标准化,返回值为标准化后的数据
newdata = MinMaxScaler().fit_transform(x)
np.savetxt(r'newdata1.txt',newdata)
#卡方检验
model = SelectKBest(chi2, k=int(sys.argv[1]))#选择k个最佳特征  
model.fit_transform(newdata, y)#iris.data是特征数据,iris.target是标签数据,该函数可以选择出k个特征   
traindata = model.fit_transform(newdata, y)
np.savetxt(r'traindata1.txt',traindata)
print(model.scores_)

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(traindata, y,random_state=0,train_size=0.5)

###########2.回归部分##########
def try_different_method(model):
    model.fit(x_train,y_train)
    score = model.score(x_test, y_test)
    result = model.predict(x_test)
    #平均绝对误差(Mean absolute error)
    #score =  mean_absolute_error(y_test, result)
    plt.figure()
    plt.plot(np.arange(len(result)), y_test,'go-',label='true value')
    plt.plot(np.arange(len(result)),result,'ro-',label='predict value')
    plt.title('score: %f'%score)
    plt.legend()
    plt.show()


###########3.具体方法选择##########
####1.KNN回归####
from sklearn import neighbors
model_KNeighborsRegressor = neighbors.KNeighborsRegressor()

####2.ANN回归####
from sklearn.neural_network import MLPRegressor
model_MLPRegressor = MLPRegressor(hidden_layer_sizes=(10,10))

####3.决策树回归####
from sklearn import tree
model_DecisionTreeRegressor = tree.DecisionTreeRegressor()

####4.SVM回归####
from sklearn import svm
model_SVR = svm.SVR()

###########4.具体方法调用部分##########
#try_different_method(model_KNeighborsRegressor)

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).