Python機器學習(3)

編程語言 機器學習 數據結構 技術 IT靜熙老師 2017-07-30

上兩篇介紹了numpy、scipy的簡單使用,下面再介紹一個機器學習中經常使用的python庫-pandas。

Pandas提供了一套名為DataFrame的數據結構,比較契合統計分析中的表結構,並且提供了計算接口,可用Numpy或其它方式進行計算。

下面看看其簡單使用:

#encoding=utf8

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

s = pd.Series([1,3,5,np.nan,6,8])

print(s)

dates = pd.date_range('20130101', periods=6)

print(dates)

#創建DataFrame

df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))

print(df)

#通過字典創建DataFrame

f2 = pd.DataFrame({ 'A' : 1.,

'B' : pd.Timestamp('20130102'),

'C' : pd.Series(1,index=list(range(4)),dtype='float32'),

'D' : np.array([3] * 4,dtype='int32'),

'E' : pd.Categorical(["test","train","test","train"]),

'F' : 'foo' })

print(f2)

#探索數據

print("前五行:",df.head())

print("後三行:",df.tail(3))

print("index: ",df.index)

print("columns: ",df.columns)

print("values: ",df.values)

print("describe: ",df.describe())

print("轉置:",df.T)

print("按照axis排列:",df.sort_index(axis=0, ascending=False))

print("按照某列排序:",df.sort_values(by='B'))

print("刪除nan:",s.dropna(how='any'))

print("填充nan值:",s.fillna(0))

總之pandas是一個強大的庫,它的複雜用法,還需我們後面通過實踐不斷探索。

學習過程中遇到什麼問題或者想獲取學習資源的話,歡迎加入學習交流群

626062078,我們一起學Python!

Python機器學習(3)

相關推薦

推薦中...