最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python機(jī)器學(xué)習(xí)應(yīng)用之工業(yè)蒸汽數(shù)據(jù)分析篇詳解

 更新時間:2022年01月18日 17:08:15   作者:柚子味的羊  
本篇文章介紹了如何用Python進(jìn)行工業(yè)蒸汽數(shù)據(jù)分析的過程及思路,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下

一、數(shù)據(jù)集

1. 訓(xùn)練集 提取碼:1234

2. 測試集 提取碼:1234

二、數(shù)據(jù)分析

1 數(shù)據(jù)導(dǎo)入

#%%導(dǎo)入基礎(chǔ)包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
import warnings
warnings.filterwarnings("ignore")
#%%讀取數(shù)據(jù)
train_data_file = "D:\Python\ML\data\zhengqi_train.txt"
test_data_file =  "D:\Python\ML\data\/zhengqi_test.txt"
train_data = pd.read_csv(train_data_file, sep='\t', encoding='utf-8')
test_data = pd.read_csv(test_data_file, sep='\t', encoding='utf-8')
#%%查看訓(xùn)練集特征變量信息
train_infor=train_data.describe()
test_infor=test_data.describe()

2 數(shù)據(jù)特征探索(數(shù)據(jù)可視化)

#%%可視化探索數(shù)據(jù)
# 畫v0箱式圖
fig = plt.figure(figsize=(4, 6))  # 指定繪圖對象寬度和高度
sns.boxplot(y=train_data['V0'],orient="v", width=0.5)
#%%可以將所有的特征都畫出
'''
column = train_data.columns.tolist()[:39]  # 列表頭
fig = plt.figure(figsize=(20, 40))  # 指定繪圖對象寬度和高度
for i in range(38):
    plt.subplot(13, 3, i + 1)  # 13行3列子圖
    sns.boxplot(train_data[column[i]], orient="v", width=0.5)  # 箱式圖
    plt.ylabel(column[i], fontsize=8)
plt.show()
'''
#%%查看v0的數(shù)據(jù)分布直方圖,繪制QQ圖查看數(shù)據(jù)是否近似于正態(tài)分布
plt.figure(figsize=(10,5))
ax=plt.subplot(1,2,1)
sns.distplot(train_data['V0'],fit=stats.norm)
ax=plt.subplot(1,2,2)
res = stats.probplot(train_data['V0'], plot=plt)
#%%查看所有特征的數(shù)據(jù)分布情況
'''
train_cols = 6
train_rows = len(train_data.columns)
plt.figure(figsize=(4*train_cols,4*train_rows))

i=0
for col in train_data.columns:
    i+=1
    ax=plt.subplot(train_rows,train_cols,i)
    sns.distplot(train_data[col],fit=stats.norm)
    
    i+=1
    ax=plt.subplot(train_rows,train_cols,i)
    res = stats.probplot(train_data[col], plot=plt)
plt.show()
'''

#%%對比統(tǒng)一特征訓(xùn)練集和測試集的分布情況,查看數(shù)據(jù)分布是否一致
ax = sns.kdeplot(train_data['V0'], color="Red", shade=True)
ax = sns.kdeplot(test_data['V0'], color="Blue", shade=True)
ax.set_xlabel('V0')
ax.set_ylabel("Frequency")
ax = ax.legend(["train","test"])

#%%查看所有特征的訓(xùn)練集和測試集分布情況
'''
dist_cols = 6
dist_rows = len(test_data.columns)
plt.figure(figsize=(4*dist_cols,4*dist_rows))

i=1
for col in test_data.columns:
    ax=plt.subplot(dist_rows,dist_cols,i)
    ax = sns.kdeplot(train_data[col], color="Red", shade=True)
    ax = sns.kdeplot(test_data[col], color="Blue", shade=True)
    ax.set_xlabel(col)
    ax.set_ylabel("Frequency")
    ax = ax.legend(["train","test"])
    
    i+=1
plt.show()
'''

#%%查看v5,v9,v11,v22,v28的數(shù)據(jù)分布
drop_col = 6
drop_row = 1

plt.figure(figsize=(5*drop_col,5*drop_row))
i=1
for col in ["V5","V9","V11","V17","V22","V28"]:
    ax =plt.subplot(drop_row,drop_col,i)
    ax = sns.kdeplot(train_data[col], color="Red", shade=True)
    ax = sns.kdeplot(test_data[col], color="Blue", shade=True)
    ax.set_xlabel(col)
    ax.set_ylabel("Frequency")
    ax = ax.legend(["train","test"])
    
    i+=1
plt.show()
#%%刪除這些特征
drop_columns=["V5","V9","V11","V17","V22","V28"]
train_data=train_data.drop(columns=drop_columns)
test_data=test_data.drop(columns=drop_columns)

當(dāng)訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)分布不一致的時候,會導(dǎo)致模型的泛化能力差,采用刪除此類特征的方法

#%%可視化線性回歸關(guān)系
fcols = 2
frows = 1
plt.figure(figsize=(8,4))
ax=plt.subplot(1,2,1)
sns.regplot(x='V0', y='target', data=train_data, ax=ax, 
            scatter_kws={'marker':'.','s':3,'alpha':0.3},
            line_kws={'color':'k'});
plt.xlabel('V0')
plt.ylabel('target')

ax=plt.subplot(1,2,2)
sns.distplot(train_data['V0'].dropna())
plt.xlabel('V0')

plt.show()
#%%查看所有特征變量與target變量的線性回歸關(guān)系
'''
fcols = 6
frows = len(test_data.columns)
plt.figure(figsize=(5*fcols,4*frows))

i=0
for col in test_data.columns:
    i+=1
    ax=plt.subplot(frows,fcols,i)
    sns.regplot(x=col, y='target', data=train_data, ax=ax, 
                scatter_kws={'marker':'.','s':3,'alpha':0.3},
                line_kws={'color':'k'});
    plt.xlabel(col)
    plt.ylabel('target')
    
    i+=1
    ax=plt.subplot(frows,fcols,i)
    sns.distplot(train_data[col].dropna())
    plt.xlabel(col)
'''

#%%查看特征變量的相關(guān)性
train_corr = train_data.corr()
# 畫出相關(guān)性熱力圖
ax = plt.subplots(figsize=(20, 16))#調(diào)整畫布大小
ax = sns.heatmap(train_corr, vmax=.8, square=True, annot=True)#畫熱力圖   annot=True 顯示系數(shù)

#%%找出相關(guān)程度
plt.figure(figsize=(20, 16))  # 指定繪圖對象寬度和高度
colnm = train_data.columns.tolist()  # 列表頭
mcorr = train_data[colnm].corr(method="spearman")  # 相關(guān)系數(shù)矩陣,即給出了任意兩個變量之間的相關(guān)系數(shù)
mask = np.zeros_like(mcorr, dtype=np.bool)  # 構(gòu)造與mcorr同維數(shù)矩陣 為bool型
mask[np.triu_indices_from(mask)] = True  # 角分線右側(cè)為True
cmap = sns.diverging_palette(220, 10, as_cmap=True)  # 返回matplotlib colormap對象
g = sns.heatmap(mcorr, mask=mask, cmap=cmap, square=True, annot=True, fmt='0.2f')  # 熱力圖(看兩兩相似度)
plt.show()

#%%查找特征變量和target變量相關(guān)系數(shù)大于0.5的特征變量
#尋找K個最相關(guān)的特征信息
k = 10 # number of variables for heatmap
cols = train_corr.nlargest(k, 'target')['target'].index

cm = np.corrcoef(train_data[cols].values.T)
hm = plt.subplots(figsize=(10, 10))#調(diào)整畫布大小
hm = sns.heatmap(train_data[cols].corr(),annot=True,square=True)
plt.show()

threshold = 0.5
corrmat = train_data.corr()
top_corr_features = corrmat.index[abs(corrmat["target"])>threshold]
plt.figure(figsize=(10,10))
g = sns.heatmap(train_data[top_corr_features].corr(),annot=True,cmap="RdYlGn")

#%% Threshold for removing correlated variables
threshold = 0.05

# Absolute value correlation matrix
corr_matrix = train_data.corr().abs()
drop_col=corr_matrix[corr_matrix["target"]<threshold].index
#%%刪除相關(guān)性小于0.05的列
train_data=train_data.drop(columns=drop_col)
test_data=test_data.drop(columns=drop_col)

#%%將train和test合并
train_x=train_data.drop(['target'],axis=1)
data_all=pd.concat([train_x,test_data])

#%%標(biāo)準(zhǔn)化
cols_numeric=list(data_all.columns)

def scale_minmax(col):
    return (col-col.min())/(col.max()-col.min())

data_all[cols_numeric] = data_all[cols_numeric].apply(scale_minmax,axis=0)
print(data_all[cols_numeric].describe())
train_data_process = train_data[cols_numeric]
train_data_process = train_data_process[cols_numeric].apply(scale_minmax,axis=0)

test_data_process = test_data[cols_numeric]
test_data_process = test_data_process[cols_numeric].apply(scale_minmax,axis=0)

#%%查看v0-v3四個特征的箱盒圖,查看其分布是否符合正態(tài)分布
cols_numeric_0to4 = cols_numeric[0:4]
## Check effect of Box-Cox transforms on distributions of continuous variables

train_data_process = pd.concat([train_data_process, train_data['target']], axis=1)

fcols = 6
frows = len(cols_numeric_0to4)
plt.figure(figsize=(4*fcols,4*frows))
i=0

for var in cols_numeric_0to4:
    dat = train_data_process[[var, 'target']].dropna()
        
    i+=1
    plt.subplot(frows,fcols,i)
    sns.distplot(dat[var] , fit=stats.norm);
    plt.title(var+' Original')
    plt.xlabel('')
        
    i+=1
    plt.subplot(frows,fcols,i)
    _=stats.probplot(dat[var], plot=plt)
    plt.title('skew='+'{:.4f}'.format(stats.skew(dat[var])))
    plt.xlabel('')
    plt.ylabel('')
        
    i+=1
    plt.subplot(frows,fcols,i)
    plt.plot(dat[var], dat['target'],'.',alpha=0.5)
    plt.title('corr='+'{:.2f}'.format(np.corrcoef(dat[var], dat['target'])[0][1]))
 
    i+=1
    plt.subplot(frows,fcols,i)
    trans_var, lambda_var = stats.boxcox(dat[var].dropna()+1)
    trans_var = scale_minmax(trans_var)      
    sns.distplot(trans_var , fit=stats.norm);
    plt.title(var+' Tramsformed')
    plt.xlabel('')
        
    i+=1
    plt.subplot(frows,fcols,i)
    _=stats.probplot(trans_var, plot=plt)
    plt.title('skew='+'{:.4f}'.format(stats.skew(trans_var)))
    plt.xlabel('')
    plt.ylabel('')
        
    i+=1
    plt.subplot(frows,fcols,i)
    plt.plot(trans_var, dat['target'],'.',alpha=0.5)
    plt.title('corr='+'{:.2f}'.format(np.corrcoef(trans_var,dat['target'])[0][1]))

三、特征優(yōu)化

import pandas as pd

train_data_file =  "D:\Python\ML\data\zhengqi_train.txt"
test_data_file =   "D:\Python\ML\data\zhengqi_test.txt"

train_data = pd.read_csv(train_data_file, sep='\t', encoding='utf-8')
test_data = pd.read_csv(test_data_file, sep='\t', encoding='utf-8')

#%%定義特征構(gòu)造方法,構(gòu)造特征
epsilon=1e-5

#組交叉特征,可以自行定義,如增加: x*x/y, log(x)/y 等等,使用lambda函數(shù)更方便快捷
func_dict = {
            'add': lambda x,y: x+y,
            'mins': lambda x,y: x-y,
            'div': lambda x,y: x/(y+epsilon),
            'multi': lambda x,y: x*y
            }
#%%定義特征構(gòu)造函數(shù)
def auto_features_make(train_data,test_data,func_dict,col_list):
    train_data, test_data = train_data.copy(), test_data.copy()
    for col_i in col_list:
        for col_j in col_list:
            for func_name, func in func_dict.items():
                for data in [train_data,test_data]:
                    func_features = func(data[col_i],data[col_j])
                    col_func_features = '-'.join([col_i,func_name,col_j])
                    data[col_func_features] = func_features
    return train_data,test_data
#%%對訓(xùn)練集和測試集進(jìn)行特征構(gòu)造
train_data2, test_data2 = auto_features_make(train_data,test_data,func_dict,col_list=test_data.columns)

四、對特征構(gòu)造后的訓(xùn)練集和測試集進(jìn)行主成分分析

#%%PCA
from sklearn.decomposition import PCA   #主成分分析法

#PCA方法降維
pca = PCA(n_components=500)
train_data2_pca = pca.fit_transform(train_data2.iloc[:,0:-1])
test_data2_pca = pca.transform(test_data2)
train_data2_pca = pd.DataFrame(train_data2_pca)
test_data2_pca = pd.DataFrame(test_data2_pca)
train_data2_pca['target'] = train_data2['target']
X_train2 = train_data2[test_data2.columns].values
y_train = train_data2['target']

五、使用LightGBM模型進(jìn)行訓(xùn)練和預(yù)測

#%%使用lightgbm模型對新構(gòu)造的特征進(jìn)行模型訓(xùn)練和評估
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error
import lightgbm as lgb
import numpy as np

# 5折交叉驗證
kf = KFold(len(X_train2), shuffle=True, random_state=2019)
#%%
# 記錄訓(xùn)練和預(yù)測MSE
MSE_DICT = {
    'train_mse':[],
    'test_mse':[]
}

# 線下訓(xùn)練預(yù)測
for i, (train_index, test_index) in enumerate(kf.split(X_train2)):
    # lgb樹模型
    lgb_reg = lgb.LGBMRegressor(
        learning_rate=0.01,
        max_depth=-1,
        n_estimators=5000,
        boosting_type='gbdt',
        random_state=2019,
        objective='regression',
    )
   
    # 切分訓(xùn)練集和預(yù)測集
    X_train_KFold, X_test_KFold = X_train2[train_index], X_train2[test_index]
    y_train_KFold, y_test_KFold = y_train[train_index], y_train[test_index]
    
    # 訓(xùn)練模型
    lgb_reg.fit(
            X=X_train_KFold,y=y_train_KFold,
            eval_set=[(X_train_KFold, y_train_KFold),(X_test_KFold, y_test_KFold)],
            eval_names=['Train','Test'],
            early_stopping_rounds=100,
            eval_metric='MSE',
            verbose=50
        )


    # 訓(xùn)練集預(yù)測 測試集預(yù)測
    y_train_KFold_predict = lgb_reg.predict(X_train_KFold,num_iteration=lgb_reg.best_iteration_)
    y_test_KFold_predict = lgb_reg.predict(X_test_KFold,num_iteration=lgb_reg.best_iteration_) 
    
    print('第{}折 訓(xùn)練和預(yù)測 訓(xùn)練MSE 預(yù)測MSE'.format(i))
    train_mse = mean_squared_error(y_train_KFold_predict, y_train_KFold)
    print('------\n', '訓(xùn)練MSE\n', train_mse, '\n------')
    test_mse = mean_squared_error(y_test_KFold_predict, y_test_KFold)
    print('------\n', '預(yù)測MSE\n', test_mse, '\n------\n')
    
    MSE_DICT['train_mse'].append(train_mse)
    MSE_DICT['test_mse'].append(test_mse)
print('------\n', '訓(xùn)練MSE\n', MSE_DICT['train_mse'], '\n', np.mean(MSE_DICT['train_mse']), '\n------')
print('------\n', '預(yù)測MSE\n', MSE_DICT['test_mse'], '\n', np.mean(MSE_DICT['test_mse']), '\n------')

..... 不想等它跑完了,會一直跑到score不再變化或者round=100的時候為止~

到此這篇關(guān)于Python機(jī)器學(xué)習(xí)應(yīng)用之工業(yè)蒸汽數(shù)據(jù)分析篇詳解的文章就介紹到這了,更多相關(guān)Python 工業(yè)蒸汽數(shù)據(jù)分析內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python“靜態(tài)”變量、實例變量與本地變量的聲明示例

    python“靜態(tài)”變量、實例變量與本地變量的聲明示例

    這篇文章主要給大家介紹了關(guān)于python“靜態(tài)”變量、實例變量與本地變量的聲明的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python簡單實現(xiàn)9宮格圖片實例

    python簡單實現(xiàn)9宮格圖片實例

    在本篇內(nèi)容里小編給各位分享的是一篇關(guān)于python實現(xiàn)朋友圈中的九宮格圖片的實例講解,有需要的朋友們可以參考下。
    2020-09-09
  • 一文了解Python3的錯誤和異常

    一文了解Python3的錯誤和異常

    Python 的語法錯誤或者稱之為解析錯,是初學(xué)者經(jīng)常碰到的。即便 Python 程序的語法是正確的,在運行它的時候,也有可能發(fā)生錯誤。運行期檢測到的錯誤被稱為異常。本文就來和大家聊聊Python3的錯誤和異常,感興趣的可以學(xué)習(xí)一下
    2022-09-09
  • 關(guān)于TensorBoard的使用以及遇到的坑記錄

    關(guān)于TensorBoard的使用以及遇到的坑記錄

    這篇文章主要介紹了關(guān)于TensorBoard的使用以及遇到的坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • python中import和from-import的區(qū)別解析

    python中import和from-import的區(qū)別解析

    這篇文章主要介紹了python中import和from-import的區(qū)別解析,本文通過實例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • 解決阿里云郵件發(fā)送不能使用25端口問題

    解決阿里云郵件發(fā)送不能使用25端口問題

    這篇文章主要介紹了解決阿里云郵件發(fā)送不能使用25端口問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • VSCode Python開發(fā)環(huán)境配置的詳細(xì)步驟

    VSCode Python開發(fā)環(huán)境配置的詳細(xì)步驟

    這篇文章主要介紹了VSCode Python開發(fā)環(huán)境配置的詳細(xì)步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-02-02
  • OpenCV學(xué)習(xí)方框濾波實現(xiàn)圖像處理代碼示例

    OpenCV學(xué)習(xí)方框濾波實現(xiàn)圖像處理代碼示例

    這篇文章主要為大家介紹了OpenCV學(xué)習(xí)如何使用方框濾波實現(xiàn)對圖像處理代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • 解決cupy-cuda安裝下載報錯以及速度太慢的問題

    解決cupy-cuda安裝下載報錯以及速度太慢的問題

    在嘗試下載Cupy-CUDA時可能會遇到報錯"ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE.",這通常是由于網(wǎng)絡(luò)問題導(dǎo)致的,出現(xiàn)這種情況時,可以嘗試使用清華大學(xué)的鏡像源來加速下載,這樣不僅可以提高下載速度
    2024-09-09
  • python調(diào)用有道智云API實現(xiàn)文件批量翻譯

    python調(diào)用有道智云API實現(xiàn)文件批量翻譯

    這篇文章主要介紹了python如何調(diào)用有道智云API實現(xiàn)文件批量翻譯,幫助大家更好得理解和使用python,感興趣的朋友可以了解下
    2020-10-10

最新評論

丹东市| 宜君县| 礼泉县| 西乌珠穆沁旗| 兴仁县| 泸州市| 从江县| 岳阳县| 定南县| 江西省| 乌兰浩特市| 兴仁县| 武陟县| 将乐县| 大厂| 清新县| 汝南县| 定襄县| 台湾省| 日喀则市| 油尖旺区| 西峡县| 扬州市| 绿春县| 咸阳市| 鄂尔多斯市| 邯郸县| 梁平县| 陈巴尔虎旗| 屏山县| 茶陵县| 礼泉县| 深州市| 威海市| 赤水市| 宝山区| 余庆县| 临城县| 杭州市| 龙海市| 开平市|