Numpy.user How to use it effectively

tailorchen: <p>import numpy as np<br>
import pandas as pd<br>
pd.set_option(‘display.max_columns’, None) <span class="hashtag-raw">#显示所有列</span></p>
<p>import matplotlib.pyplot as plt<br>
plt.rcParams[‘font.sans-serif’]=[‘SimHei’] <span class="hashtag-raw">#显示中文</span></p>
<p>import seaborn as sns</p>
<p>import warnings<br>
warnings.filterwarnings(“ignore”) <span class="hashtag-raw">#忽略警告</span></p>
<p><span class="hashtag-raw">#读取数据</span><br>
data = pd.read_csv(‘teleinfo.csv’,encoding=‘utf-8’) <span class="hashtag-raw">#读取CSV格式文件</span>，编码为utf-8<br>
data</p>
<p>data = data.drop([‘记录电话卡’,‘无线日志’],axis=1) <span class="hashtag-raw">#这两列错误值太多基本没什么用了</span>，直接删除<br>
data.head()</p>
<p><span class="hashtag-raw">#查看特征名和特征个数</span><br>
print(‘特征名称：\n’, data.columns.values)<br>
print(’ '*20)<br>
print(‘特征个数：’, len(data.columns))</p>
<p><span class="hashtag-raw">#查看是否有缺失值</span><br>
data.isnull().sum() <span class="hashtag-raw">#统计每列的缺失值情况</span></p>
<p><span class="hashtag-raw">#查看特征统计量</span><br>
data.describe()</p>
<p><span class="hashtag-raw">#查看数据相关信息</span><br>
data.info() <span class="hashtag-raw">#数据的相关信息</span></p>
<p><span class="hashtag-raw">#柱状图查看流失</span><br>
import matplotlib.pyplot as plt<br>
plt.rcParams[‘font.sans-serif’]=[‘SimHei’]</p>
<p>loss = data[‘流失’].value_counts() <span class="hashtag-raw">#统计不同数的数值</span><br>
loss.plot.bar() <span class="hashtag-raw">#绘制柱状图</span><br>
plt.title(‘流失’) <span class="hashtag-raw">#设置标题</span><br>
plt.show()</p>
<h1><a name="p-457336-br-1" class="anchor" href="#p-457336-br-1"></a>流失大概占到三成<br></h1>
<p>data[‘流失’].value_counts()</p>
<p><span class="hashtag-raw">#绘制柱状图查看客户类别与流失有没有关系</span></p>
<p>relation = data.groupby([‘客户类别’,‘流失’])[‘客户类别’].count().unstack() <span class="hashtag-raw">#按客户类别进行聚合统计</span>，然后行旋转到列<br>
relation.plot.bar(stacked=‘True’) <span class="hashtag-raw">#绘制柱状图</span><br>
plt.title(‘流失统计’) <span class="hashtag-raw">#设置标题</span><br>
plt.ylabel(‘计数’)</p>
<p><span class="hashtag-raw">#将类别数据和数值数据分开</span><br>
num_fea = [‘服务月数’,‘年龄’,‘住址年份’,‘收入’,‘雇用’,‘上个月长途旅行’,<br>
‘上个月免费’,‘上月设备’,‘上月电话卡’,‘无线上网’,‘无线上月’,<br>
‘设备超过使用期限’,‘长途漫游’,‘终身免费’,‘电话卡超过任期’]<br>
print(len(num_fea))</p>
<p><span class="hashtag-raw">#对数值型进行相关性分析</span><br>
correlation = data[num_fea].corr() <span class="hashtag-raw">#相关系数</span><br>
ax = plt.subplots(figsize = (7, 7)) <span class="hashtag-raw">#设置图像大小</span><br>
plt.title(‘Correlation of Numeric Features with Price’) <span class="hashtag-raw">#标题</span><br>
sns.heatmap(correlation,square = True) <span class="hashtag-raw">#绘制热度图</span></p>
<p><span class="hashtag-raw">#每个特征的分布可视化</span><br>
import seaborn as sns<br>
f = pd.melt(data, value_vars=num_fea)<br>
g = sns.FacetGrid(f, col=“variable”,col_wrap=3, sharex=False, sharey=False)<br>
g = g.map(sns.distplot, “value”)</p>
<p><span class="hashtag-raw">#不需要转换到分类的特征</span><br>
num_feature = [‘服务月数’,‘年龄’,‘住址年份’,‘收入’,‘雇用’,‘上个月长途旅行’,‘长途漫游’]</p>
<p><span class="hashtag-raw">#用来取平方根</span><br>
def squar(x):<br>
square = np.sqrt(x) <span class="hashtag-raw">#平方根</span><br>
return square</p>
<p><span class="hashtag-raw">#每个特征都进行取平方根操作</span><br>
data[‘住址年份’] = data[‘住址年份’].apply(lambda x:squar(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘收入’] = data[‘收入’].apply(lambda x:squar(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘雇用’] = data[‘雇用’].apply(lambda x:squar(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘上个月长途旅行’] = data[‘上个月长途旅行’].apply(lambda x:squar(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘长途漫游’] = data[‘长途漫游’].apply(lambda x:squar(x)) <span class="hashtag-raw">#函数调用</span></p>
<p><span class="hashtag-raw">#每个数字特征得分布可视化</span><br>
import seaborn as sns<br>
f = pd.melt(data[num_feature], value_vars=num_feature) <span class="hashtag-raw">#将数据流从宽格式转换为长格式</span><br>
g = sns.FacetGrid(f, col=“variable”,col_wrap=3, sharex=False, sharey=False) <span class="hashtag-raw">#画布</span><br>
g = g.map(sns.distplot, “value”) <span class="hashtag-raw">#绘图</span></p>
<p><span class="hashtag-raw">#数值为0的分到0类</span>，其他的分到1类<br>
def cat(x):<br>
if x==0:<br>
return 0<br>
else:<br>
return 1</p>
<p><span class="hashtag-raw">#数值特征转换为分类特征</span><br>
data[‘无线上网’] = data[‘无线上网’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘无线上月’] = data[‘无线上月’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘上个月免费’] = data[‘上个月免费’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘终身免费’] = data[‘终身免费’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘设备超过使用期限’] = data[‘设备超过使用期限’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘上月设备’] = data[‘上月设备’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘上月电话卡’] = data[‘上月电话卡’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span><br>
data[‘电话卡超过任期’] = data[‘电话卡超过任期’].apply(lambda x:cat(x)) <span class="hashtag-raw">#函数调用</span></p>
<p><span class="hashtag-raw">#类别部分</span><br>
cat_fea = [‘区域’,‘婚姻’,‘退休’,‘性别’,‘住户人数’,‘是否免费服务’,‘设备配备’,‘电话卡服务’,<br>
‘无线服务’,‘多行’,‘语音信箱’,‘寻呼服务’,‘互联网’,‘主叫号码’,‘呼叫等待’,‘呼叫转移’,<br>
‘无线上网’,‘无线上月’,‘三方通话’,‘电子账单’,‘客户类别’,‘上个月免费’,‘终身免费’,<br>
‘设备超过使用期限’,‘上月设备’,‘上月电话卡’,‘电话卡超过任期’]</p>
<p><span class="hashtag-raw">#类别特征的柱形图可视化</span><br>
def bar_plot(x, y, **kwargs):<br>
sns.barplot(x=x, y=y) <span class="hashtag-raw">#绘制条形图</span><br>
x=plt.xticks(rotation=90) <span class="hashtag-raw">#标签旋转90度</span></p>
<p>f = pd.melt(data, id_vars=[‘流失’], value_vars=cat_fea) <span class="hashtag-raw">#将数据流从宽格式转换为长格式</span><br>
g = sns.FacetGrid(f, col=“variable”,  col_wrap=4, sharex=False, sharey=False, size=5) <span class="hashtag-raw">#画布</span><br>
g = g.map(bar_plot, “value”, “流失”) <span class="hashtag-raw">#绘图</span></p>
<p><span class="hashtag-raw">#最后一列是标签</span>，其余是特征<br>
X = data.iloc[:,:-1]<br>
Y = data.iloc[:,-1]</p>
<p><span class="hashtag-raw">#标签编码</span><br>
from sklearn.preprocessing import LabelEncoder</p>
<p>le = LabelEncoder() <span class="hashtag-raw">#标签编码</span><br>
Y = le.fit_transform(Y) <span class="hashtag-raw">#对标签训练转换</span></p>
<p><span class="hashtag-raw">#分割训练集和测试集</span></p>
<p>from sklearn.model_selection import train_test_split<br>
X_train,X_test,Y_train,Y_test = train_test_split(X, Y, test_size=0.2, random_state=42) <span class="hashtag-raw">#分割数据集</span>，20%用作测试</p>
<p>print(X_train.shape)<br>
print(Y_train.shape)<br>
print(X_test.shape)<br>
print(Y_test.shape)</p>
<p><span class="hashtag-raw">#xgboost分类</span><br>
<span class="hashtag-raw">#xgboost参数很多</span>，选取其中几个进行调参<br>
from xgboost.sklearn import  XGBClassifier<br>
clf = XGBClassifier(<br>
max_depth = 7, <span class="hashtag-raw">#最大树深</span><br>
learning_rate = 0.01, <span class="hashtag-raw">#学习率</span><br>
min_child_weight = 7, <span class="hashtag-raw">#叶子节点最小样本数</span><br>
n_estimators = 100, <span class="hashtag-raw">#弱学习器个数</span><br>
)<br>
clf.fit(X_train,Y_train) <span class="hashtag-raw">#训练分类器</span></p>
<p><span class="hashtag-raw">#准确率</span></p>
<p>print(‘训练集准确率：’,clf.score(X_train,Y_train)) <span class="hashtag-raw">#训练集准确率</span><br>
print(‘测试集准确率：’,clf.score(X_test,Y_test)) <span class="hashtag-raw">#测试集准确率</span></p>
<p>print(clf.feature_importances_) <span class="hashtag-raw">#查看特征重要性</span><br>
print()<br>
print(‘特征数量：’,len(clf.feature_importances_)) <span class="hashtag-raw">#查看总的特征数</span></p>
<p><span class="hashtag-raw">#特征重要性绘图</span><br>
from xgboost import plot_importance<br>
fig = plt.figure(figsize=(16,16)) <span class="hashtag-raw">#设置图像大小</span><br>
plot_importance(clf, title=‘特征重要性排序’, xlabel=‘得分’, ylabel=‘特征’, grid=False) <span class="hashtag-raw">#绘制特征图</span><br>
plt.show()</p>
<p><span class="hashtag-raw">#预测测试集标签</span></p>
<p>pred = clf.predict(X_test) <span class="hashtag-raw">#预测标签</span><br>
pred</p>
<p><span class="hashtag-raw">#混淆矩阵</span><br>
from collections import Counter<br>
import seaborn as sns<br>
from sklearn.metrics import confusion_matrix<br>
sns.set()</p>
<p>print(“真实值：”,Counter(Y_test)) <span class="hashtag-raw">#真是标签</span><br>
print(“预测值：”,Counter(pred)) <span class="hashtag-raw">#预测标签</span><br>
print (“总体准确率：”,round(sum(Y_test==pred) / len(Y_test),4)) <span class="hashtag-raw">#计算准确率</span></p>
<p>confmat= confusion_matrix(y_true=Y_test,y_pred=pred) <span class="hashtag-raw">#输出混淆矩阵</span><br>
sns.heatmap(confmat,annot=True) <span class="hashtag-raw">#绘制热度图</span></p>

ptrblck: <p>Could you clarify your question and how it’s related to PyTorch?</p>