# -*- coding: utf-8 -*-
#邏輯回歸 自動建模
import pandas as pd
#參數(shù)初始化
filename = 'd:/data/bankloan.xls'
data = pd.read_excel(filename)
x = data.iloc[:,:8].as_matrix()
y = data.iloc[:,8].as_matrix()
from sklearn.linear_model import LogisticRegression as LR
from sklearn.linear_model import RandomizedLogisticRegression as RLR
rlr = RLR() #建立隨機邏輯回歸模型拴驮,篩選變量
rlr.fit(x, y) #訓(xùn)練模型
rlr.get_support() #獲取特征篩選結(jié)果,也可以通過.scores_方法獲取各個特征的分?jǐn)?shù)
print(u'通過隨機邏輯回歸模型篩選特征結(jié)束柴信。')
print(u'有效特征為:%s' % ','.join(data.columns[rlr.get_support()]))
x = data[data.columns[rlr.get_support()]].as_matrix() #篩選好特征
lr = LR() #建立邏輯回歸模型
lr.fit(x, y) #用篩選后的特征數(shù)據(jù)來訓(xùn)練模型
print(u'邏輯回歸模型訓(xùn)練結(jié)束套啤。')
print(u'模型的平均正確率為:%s' % lr.score(x, y)) #給出模型的平均正確率,本例為81.4%
#非線性回歸
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from sklearn import metrics
x=pd.DataFrame([1.5,2.8,4.5,7.5,10.5,13.5,15.1,16.5,19.5,22.5,24.5,26.5])
y=pd.DataFrame([7.0,5.5,4.6,3.6,2.9,2.7,2.5,2.4,2.2,2.1,1.9,1.8])
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.scatter(x,y)
fig.show()
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()
linreg.fit(x,y)
# The coefficients
print('Coefficients: \n', linreg.coef_)
y_pred = linreg.predict(x)
# The mean square error
print "MSE:",metrics.mean_squared_error(y,y_pred)
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % linreg.score(x, y))
#多項式模型
x1=x
x2=x**2
x1['x2']=x2
linreg = LinearRegression()
linreg.fit(x1,y)
# The coefficients
print('Coefficients: \n', linreg.coef_)
y_pred = linreg.predict(x)
# The mean square error
print "MSE:",metrics.mean_squared_error(y,y_pred)
#對數(shù)模型
x2=pd.DataFrame(np.log(x[0]))
linreg = LinearRegression()
linreg.fit(x2,y)
# The coefficients
print('Coefficients: \n', linreg.coef_)
y_pred = linreg.predict(x2)
# The mean square error
print "MSE:",metrics.mean_squared_error(y,y_pred)
#指數(shù)
y2=pd.DataFrame(np.log(y))
linreg = LinearRegression()
linreg.fit(pd.DataFrame(x[0]),y2)
# The coefficients
print('Coefficients: \n', linreg.coef_)
y_pred = linreg.predict(pd.DataFrame(x[0]))
# The mean square error
print "MSE:",metrics.mean_squared_error(y2,y_pred)
#冪函數(shù)
linreg = LinearRegression()
linreg.fit(x2,y2)
# The coefficients
print('Coefficients: \n', linreg.coef_)
y_pred = linreg.predict(x2)
# The mean square error
print "MSE:",metrics.mean_squared_error(y2,y_pred)