零散的一些东西吧(随时跟新) numpy pandas plt 等一些操作吧
a=pd.read_csv(’…’)
a.head() #a的前五行 括号内可写想看的行数
b= a[pd.notnull(a.Age)] 找出age不是空值的行
plt.figure(figsize=(10,10)) 一个10*10大小的面积用于画图
plt.subplot(121) 1行2列的分法,第一个位置
plt.scatter(a.loc_x, a.loc_y, color=‘R’, alpha=alpha) 散点图(横坐标,纵坐标,颜色,透明度)
a.action_type.unique() a数据中action-type这列有多少个不同的值
a.shot_type.value_counts() a数据中shot_type 不同的值各有多少个
pd.get_dummies(a[‘combined_shot_type’], prefix=‘combined_shot_type’) 热编码
drops = [‘shot_id’, ‘team_id’, ‘team_name’, ‘shot_zone_area’, ‘shot_zone_range’, ‘shot_zone_basic’,
‘matchup’, ‘lon’, ‘lat’, ‘seconds_remaining’, ‘minutes_remaining’,
‘shot_distance’, ‘loc_x’, ‘loc_y’, ‘game_event_id’, ‘game_id’, ‘game_date’]
for drop in drops:
a = a.drop(drop, 1) 将上述列都消去
.tail(3) 显示后三行
.columns 显示列名
.loc[0] 第一行数据
[‘列名’] 这一列
.max() 最大值
.sort_values(‘列名’,inplace=True) 从小到大排序,在原基础上直接修改
.sort_values(‘列名’,inplace=True,ascending=False) 从大到小排序
a=pd.isnull(train.Age) 判断Age是否为空
train[‘Age’].mean() 计算Age的平均值 nan不影响 不计算
train.pivot_table(index=‘Pclass’,values=‘Survived’,aggfunc=np.mean) 从Pclass的角度来看生存,统计依靠平均值
.dropna(axis=0,subset=[‘Age’]) 按列 把Age这一列确实值去掉
.loc[83,‘Age’] 第83个age
.reset_index(drop=True) 把索引重新按0-。。。
np.logspace(0,2,num=5) 构造等比数列,从10的0次到10的2次 共5个数
from sklearn.preprocessing import StandardScaler
data[‘normAmount’] = StandardScaler().fit_transform(data[‘Amount’].values.reshape(-1, 1)) 数据标准化, 为1列 -1代表任意行
X = data.loc[:, data.columns != ‘Class’] 把class列给提出来
from sklearn.cross_validation import train_test_split #交叉验证
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.3, random_state = 0)
df_news = pd.read_table(‘val.txt’,names=[‘category’,‘theme’,‘URL’,‘content’],encoding=‘utf-8’) #读入txt文件 并每列加标题