python的sklearn分析酒店評分影響因素

2021-10-13 18:46:24 字數 2798 閱讀 5201

#多元回歸 分析獲得客戶評價對推薦人數的影響

import pandas as pd

import statsmodels.api as sm

from sklearn.linear_model import linearregression

filename =

"../../data/各項評分.xls"

data = pd.read_excel(filename)

print(data.describe(

))x = data[

['location', 'service', 'fac', 'health']]

y = data[

['comment_recommend']]

regr = linearregression(

)regr.fit(x, y)

print(

'各項係數'+str(regr.coef_))

print(

'常數項'+str(regr.intercept_))

x2 = sm.add_constant(x)

est = sm.ols(y, x2).fit(

)print(est.summary(

))

多元回歸和擬合結果如下

用決策樹判斷一下幾個因素對酒店評分影響比例

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.tree import decisiontreeclassifier

from sklearn.metrics import accuracy_score

from sklearn.metrics import roc_curve

filename =

"../data/各項評分.xls"

data = pd.read_excel(filename)

#推薦率小於92%視為不合格的酒店

data.loc[data.comment_recommend <

0.92

,'comment_recommend']=

0data.loc[data.comment_recommend >=

0.92

,'comment_recommend']=

1x = data.drop(columns=

'comment_recommend'

)y = data[

'comment_recommend'

]x_train, x_test,y_train, y_test= train_test_split(x, y, test_size=

0.2, random_state=1)

model = decisiontreeclassifier(max_depth=

4, random_state=1)

model.fit(x_train, y_train)

y_pred = model.predict(x_test)

a = pd.dataframe()a[

'**值']=

list

(y_pred)

a['實際值']=

list

(y_test)

print

(a)#準確度

score = accuracy_score(y_pred, y_test)

print

(score)

#**推薦和不推薦

y_pred_proba = model.predict_proba(x_test)

b = pd.dataframe(y_pred_proba, columns=

['不推薦'

,'推薦'])

print

(b)#模型評估

fpr, tpr, thres = roc_curve(y_test, y_pred_proba[:,

1])a = pd.dataframe()a[

'閾值']=

list

(thres)

a['假報警']=

list

(fpr)

a['命中率']=

list

(tpr)

print

(a)#獲取特徵重要性

fea = x.columns

importances = model.feature_importances_

#以二維**顯示

importances_df = pd.dataframe(

)importances_df[

'特徵名稱'

]= fea

importances_df[

'特徵重要性'

]= importances

importances_df.sort_values(

'特徵重要性'

可能是資料集不夠,結果不太正確

外附加資料超市位址

Python安裝sklearn的問題

我使用的安裝源為,推薦使用豆瓣源,挺全的,不過有的可能沒有,我目前需要的基本都能找到 所以總的命令為 pip install i numpy 記住url 後要有空格,然後再輸入numpy 這樣numpy總算順利安裝好了,不過其他小夥伴如果能用pip install numpy方法直接成功安裝的,就不...

python中sklearn的pipeline模組

最近在看 深度學習 基於keras的python實踐 魏貞原 這本書,書中8.3建立了乙個scikit learn的pipeline,首先標準化資料集,然後建立和評估基線神經網路模型,如下 資料正態化,改進演算法 steps standardize standardscaler mlp model ...

Python中sklearn版本的公升級

呼叫sklearn的impute時,發現sklearn中沒有impute的模組。from sklearn.impute import imputer報錯 modulenotfounderror no module named sklearn.impute 經過檢查,發現anaconda中的sklea...