基于Python的數(shù)據(jù)分析實(shí)戰(zhàn)教程與案例_第1頁
基于Python的數(shù)據(jù)分析實(shí)戰(zhàn)教程與案例_第2頁
基于Python的數(shù)據(jù)分析實(shí)戰(zhàn)教程與案例_第3頁
基于Python的數(shù)據(jù)分析實(shí)戰(zhàn)教程與案例_第4頁
基于Python的數(shù)據(jù)分析實(shí)戰(zhàn)教程與案例_第5頁
已閱讀5頁,還剩17頁未讀, 繼續(xù)免費(fèi)閱讀

付費(fèi)下載

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

基于Python的數(shù)據(jù)分析實(shí)戰(zhàn)教程與案例數(shù)據(jù)分析已成為現(xiàn)代商業(yè)決策不可或缺的一部分,而Python憑借其豐富的庫和強(qiáng)大的功能,成為數(shù)據(jù)分析師的首選工具。本文將通過實(shí)戰(zhàn)教程與案例,詳細(xì)介紹如何利用Python進(jìn)行數(shù)據(jù)分析,涵蓋數(shù)據(jù)采集、清洗、分析、可視化等全流程,并結(jié)合具體案例展示Python在數(shù)據(jù)分析中的應(yīng)用價(jià)值。一、Python數(shù)據(jù)分析環(huán)境搭建進(jìn)行數(shù)據(jù)分析前,需要搭建合適的開發(fā)環(huán)境。推薦使用Anaconda發(fā)行版,它集成了Python解釋器、JupyterNotebook、Pandas、NumPy等常用數(shù)據(jù)分析庫。python創(chuàng)建虛擬環(huán)境condacreate-ndata_analysispython=3.9pandasnumpymatplotlibseaborn激活環(huán)境condaactivatedata_analysis安裝必要庫pipinstalljupyterplotlyscikit-learnJupyterNotebook是數(shù)據(jù)分析師常用的交互式開發(fā)工具,支持代碼、文本和可視化的混合編輯,便于展示分析過程。通過以下命令啟動(dòng)JupyterNotebook:pythonjupyternotebook二、數(shù)據(jù)采集與導(dǎo)入數(shù)據(jù)是數(shù)據(jù)分析的基礎(chǔ),常見的獲取方式包括API接口、數(shù)據(jù)庫、網(wǎng)頁爬蟲等。2.1從CSV文件導(dǎo)入數(shù)據(jù)pythonimportpandasaspd讀取本地CSV文件data=pd.read_csv('sales_data.csv')查看數(shù)據(jù)前5行print(data.head())查看數(shù)據(jù)基本信息print(())查看描述性統(tǒng)計(jì)信息print(data.describe())2.2從SQL數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)pythonimportpandasaspdimportsqlite3連接SQLite數(shù)據(jù)庫conn=sqlite3.connect('company.db')讀取表數(shù)據(jù)data=pd.read_sql('SELECTFROMsales',conn)關(guān)閉數(shù)據(jù)庫連接conn.close()2.3使用requests庫獲取網(wǎng)頁數(shù)據(jù)pythonimportrequestsimportpandasaspd獲取JSON數(shù)據(jù)url='/data'response=requests.get(url)data=pd.json_normalize(response.json())保存為CSV文件data.to_csv('web_data.csv',index=False)三、數(shù)據(jù)清洗與預(yù)處理原始數(shù)據(jù)往往需要經(jīng)過清洗才能用于分析,常見的數(shù)據(jù)清洗任務(wù)包括處理缺失值、重復(fù)值、異常值和格式轉(zhuǎn)換。3.1處理缺失值python檢查缺失值print(data.isnull().sum())刪除含有缺失值的行data_clean=data.dropna()填充缺失值,例如使用均值填充data_filled=data.fillna(data.mean())前向填充或后向填充data_filled_forward=data.fillna(method='ffill')data_filled_backward=data.fillna(method='bfill')3.2處理重復(fù)值python檢查重復(fù)值print(data.duplicated().sum())刪除重復(fù)值data_clean=data.drop_duplicates()保留第一次出現(xiàn)的重復(fù)值data_keep_first=data.drop_duplicates(keep='first')保留最后一次出現(xiàn)的重復(fù)值data_keep_last=data.drop_duplicates(keep='last')3.3處理異常值python使用Z-score方法檢測(cè)異常值importnumpyasnpz_scores=np.abs((data['sales']-data['sales'].mean())/data['sales'].std())outliers=data[z_scores>3]刪除異常值data_clean=data[z_scores<=3]替換異常值data['sales']=np.where(z_scores>3,data['sales'].median(),data['sales'])3.4數(shù)據(jù)類型轉(zhuǎn)換python轉(zhuǎn)換數(shù)據(jù)類型data['date']=pd.to_datetime(data['date'])data['sales']=data['sales'].astype(float)創(chuàng)建新列data['year']=data['date'].dt.yeardata['month']=data['date'].dt.monthdata['day']=data['date'].dt.day四、探索性數(shù)據(jù)分析探索性數(shù)據(jù)分析(EDA)是理解數(shù)據(jù)特征和關(guān)系的關(guān)鍵步驟,常用的分析方法包括描述性統(tǒng)計(jì)、數(shù)據(jù)可視化等。4.1描述性統(tǒng)計(jì)python基本統(tǒng)計(jì)量print(data[['sales','profit']].describe())分位數(shù)分析print(data['sales'].quantile([0.25,0.5,0.75]))相關(guān)性分析print(data[['sales','profit','units']].corr())4.2數(shù)據(jù)可視化Matplotlib和Seaborn是Python中常用的可視化庫。pythonimportmatplotlib.pyplotaspltimportseabornassns直方圖plt.figure(figsize=(10,6))sns.histplot(data['sales'],kde=True)plt.title('SalesDistribution')plt.xlabel('Sales')plt.ylabel('Frequency')plt.show()散點(diǎn)圖plt.figure(figsize=(10,6))sns.scatterplot(x='sales',y='profit',data=data)plt.title('SalesvsProfit')plt.xlabel('Sales')plt.ylabel('Profit')plt.show()箱線圖plt.figure(figsize=(10,6))sns.boxplot(x='category',y='sales',data=data)plt.title('SalesbyCategory')plt.xlabel('Category')plt.ylabel('Sales')plt.xticks(rotation=45)plt.show()相關(guān)性熱力圖plt.figure(figsize=(10,8))corr_matrix=data[['sales','profit','units','price']].corr()sns.heatmap(corr_matrix,annot=True,cmap='coolwarm')plt.title('CorrelationHeatmap')plt.show()五、數(shù)據(jù)分組與聚合數(shù)據(jù)分組與聚合是數(shù)據(jù)分析中的常見操作,可以幫助我們發(fā)現(xiàn)數(shù)據(jù)中的模式和趨勢(shì)。python按類別分組計(jì)算總銷售額grouped=data.groupby('category')['sales'].sum()print(grouped)多列分組grouped_multi=data.groupby(['year','month'])['sales'].sum()print(grouped_multi)聚合操作agg_data=data.groupby('category').agg({'sales':['sum','mean','count'],'profit':'mean'})print(agg_data)分位數(shù)聚合quantile_data=data.groupby('category')['sales'].quantile([0.25,0.5,0.75])print(quantile_data)六、時(shí)間序列分析時(shí)間序列分析是數(shù)據(jù)分析中的重要組成部分,尤其在金融、經(jīng)濟(jì)和銷售領(lǐng)域。python設(shè)置日期為索引data.set_index('date',inplace=True)按月聚合銷售額monthly_sales=data['sales'].resample('M').sum()繪制時(shí)間序列圖plt.figure(figsize=(12,6))monthly_sales.plot()plt.title('MonthlySalesTrend')plt.xlabel('Date')plt.ylabel('TotalSales')plt.show()移動(dòng)平均rolling_mean=monthly_sales.rolling(window=3).mean()plt.figure(figsize=(12,6))monthly_sales.plot(label='Original')rolling_mean.plot(label='3-MonthMovingAverage')plt.title('SaleswithMovingAverage')plt.legend()plt.show()季節(jié)性分解fromstatsmodels.tsa.seasonalimportseasonal_decomposedecomposition=seasonal_decompose(monthly_sales,model='additive',period=12)decomposition.plot()plt.show()七、機(jī)器學(xué)習(xí)應(yīng)用Python的scikit-learn庫提供了豐富的機(jī)器學(xué)習(xí)算法,可用于預(yù)測(cè)、分類等任務(wù)。7.1線性回歸預(yù)測(cè)銷售額pythonfromsklearn.model_selectionimporttrain_test_splitfromsklearn.linear_modelimportLinearRegressionfromsklearn.metricsimportmean_squared_error,r2_score準(zhǔn)備數(shù)據(jù)X=data[['units','price','marketing_spend']]y=data['sales']劃分訓(xùn)練集和測(cè)試集X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=42)創(chuàng)建模型model=LinearRegression()訓(xùn)練模型model.fit(X_train,y_train)預(yù)測(cè)y_pred=model.predict(X_test)評(píng)估模型mse=mean_squared_error(y_test,y_pred)r2=r2_score(y_test,y_pred)print(f'MeanSquaredError:{mse}')print(f'R-squared:{r2}')查看系數(shù)print(f'Intercept:{ercept_}')print(f'Coefficients:{model.coef_}')7.2聚類分析pythonfromsklearn.clusterimportKMeansfromsklearn.preprocessingimportStandardScaler選擇特征features=data[['sales','profit','units']]標(biāo)準(zhǔn)化數(shù)據(jù)scaler=StandardScaler()features_scaled=scaler.fit_transform(features)確定聚類數(shù)量inertia=[]forkinrange(1,11):kmeans=KMeans(n_clusters=k,random_state=42)kmeans.fit(features_scaled)inertia.append(kmeans.inertia_)繪制肘部圖plt.figure(figsize=(10,6))plt.plot(range(1,11),inertia,marker='o')plt.title('ElbowMethodforOptimalk')plt.xlabel('NumberofClusters')plt.ylabel('Inertia')plt.show()應(yīng)用K-Means聚類kmeans=KMeans(n_clusters=4,random_state=42)clusters=kmeans.fit_predict(features_scaled)添加聚類結(jié)果到原始數(shù)據(jù)data['cluster']=clusters分析聚類結(jié)果cluster_analysis=data.groupby('cluster').agg({'sales':'mean','profit':'mean','units':'mean'})print(cluster_analysis)八、案例研究:電商銷售數(shù)據(jù)分析8.1數(shù)據(jù)背景假設(shè)我們擁有某電商平臺(tái)過去兩年的銷售數(shù)據(jù),包括訂單ID、產(chǎn)品類別、價(jià)格、銷售數(shù)量、利潤、購買日期和營銷支出等信息。8.2分析目標(biāo)1.找出銷售額最高的產(chǎn)品類別2.分析銷售額的時(shí)間趨勢(shì)和季節(jié)性3.識(shí)別高價(jià)值客戶群體4.建立銷售額預(yù)測(cè)模型5.評(píng)估營銷活動(dòng)的效果8.3分析過程python加載數(shù)據(jù)data=pd.read_csv('ecommerce_sales.csv')數(shù)據(jù)清洗處理缺失值data.dropna(subset=['product_category','price','quantity'],inplace=True)轉(zhuǎn)換數(shù)據(jù)類型data['order_date']=pd.to_datetime(data['order_date'])data['price']=data['price'].astype(float)data['quantity']=data['quantity'].astype(int)創(chuàng)建新列data['sales']=data['price']data['quantity']data['year']=data['order_date'].dt.yeardata['month']=data['order_date'].dt.monthdata['day']=data['order_date'].dt.day1.銷售額最高的產(chǎn)品類別category_sales=data.groupby('product_category')['sales'].sum().sort_values(ascending=False)print("TopProductCategoriesbySales:")print(category_sales.head())2.銷售額時(shí)間趨勢(shì)分析monthly_sales=data.groupby([data['order_date'].dt.to_period('M')])['sales'].sum()plt.figure(figsize=(12,6))monthly_sales.plot()plt.title('MonthlySalesTrend')plt.xlabel('Month')plt.ylabel('TotalSales')plt.show()季節(jié)性分析seasonal_sales=data.groupby(['month'])['sales'].mean()plt.figure(figsize=(10,6))seasonal_sales.plot(kind='bar')plt.title('AverageSalesbyMonth')plt.xlabel('Month')plt.ylabel('AverageSales')plt.xticks(rotation=0)plt.show()3.客戶價(jià)值分析創(chuàng)建RFM模型data['recency']=(data['order_date'].max()-data['order_date']).dt.daysfrequency=data.groupby('customer_id')['order_id'].count()monetary=data.groupby('customer_id')['sales'].sum()rfm=pd.DataFrame({'recency':data.groupby('customer_id')['order_date'].max(),'frequency':frequency,'monetary':monetary})分位數(shù)分組rfm['r_quartile']=pd.qcut(rfm['recency'],4,labels=['4','3','2','1'])rfm['f_quartile']=pd.qcut(rfm['frequency'],4,labels=['1','2','3','4'])rfm['m_quartile']=pd.qcut(rfm['monetary'],4,labels=['1','2','3','4'])rfm['rfm_score']=rfm['r_quartile'].astype(str)+rfm['f_quartile'].astype(str)+rfm['m_quartile'].astype(str)print("CustomerRFMScores:")print(rfm.head())4.銷售額預(yù)測(cè)模型選擇特征features=data[['year','month','day','quantity','price','marketing_spend']]target=data['sales']劃分?jǐn)?shù)據(jù)集train_size=int(len(features)0.8)X_train,X_test=features[:train_size],features[train_size:]y_train,y_test=target[:train_size],target[trai

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論