• 数据分析,numpy pandas常用api记录


    1、 np.percentile(train_list["wnum1"], [10, 90, 95, 99])  计算一个多维数组的任意百分比分位数,此处的百分位是从小到大排列

    2、fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 6)) 定义画图的画布

      - 在画图时,要注意首先定义画图的画布:fig = plt.figure( ) 
      - 然后定义子图ax ,使用 ax= fig.add_subplot( 行,列,位置标) 
      - 当上述步骤完成后,可以用 ax.plot()函数或者 df.plot(ax = ax) 
      - 在jupternotebook 需要用%定义:%matplotlib notebook;如果是在脚本编译器上则不用,但是需要一次性按流程把代码写完; 
      - 结尾时都注意记录上plt.show()

          DataFrame.plot( )函数具体解释:https://blog.csdn.net/brucewong0516/article/details/80524442

    3、train_list.plot(kind="hist", y=["wnum1", "wnum2"], bins=20, alpha=0.5, density=True, ax=ax1)

      kind代表的图的类型,hlist是柱状图

           bins代表柱状图有几个柱子,设置为20,默认是10

      alpha 代表填充的不透明度  0-1取值

    4、ax1.legend()

      显示图例

    5、train_unique_q = np.concatenate([train_data["q2"].unique(), train_data["q1"].unique()]) 

      示例:

      >>> a=np.array([1,2,3])
      >>> b=np.array([11,22,33])
      >>> c=np.array([44,55,66])
      >>> np.concatenate((a,b,c),axis=0)  # 默认情况下,axis=0可以不写
      array([ 1,  2,  3, 11, 22, 33, 44, 55, 66]) #对于一维数组拼接,axis的值不影响最后的结果

    6、from collections import Counter     

      train_question = np.concatenate([train_data["q1"], train_data["q2"]])
      test_question = np.concatenate([test_data["q1"], test_data["q2"]])
      train_counter = Counter(train_question)
      test_counter = Counter(test_question)
      print(train_counter.most_common(10))
      print(test_counter.most_common(10))

    输出:

    [('Q489328', 112), ('Q119369', 109), ('Q632400', 109), ('Q382228', 108), ('Q081677', 107), ('Q555455', 107), ('Q149996', 105), ('Q436579', 105), ('Q143237', 105), ('Q424359', 104)]
    [('Q066137', 34), ('Q209532', 31), ('Q526780', 31), ('Q665740', 29), ('Q405292', 28), ('Q092597', 28), ('Q195819', 26), ('Q150486', 26), ('Q263180', 26), ('Q492945', 25)]

      counter工具用于支持便捷和快速地计数

    7、words = questions["words"].str.split(" ").tolist()

    8、from gensim.corpora import Dictionary

      word_dict = Dictionary(words)
      char_dict = Dictionary(chars)

      dictionary.id2token      结果:{0: 'human', 1: 'interface', 2: 'computer', 3: 'survey', 4: 'user'}

           dictionary.token2id      结果:{'human': 0, 'interface': 1, 'computer': 2, 'survey': 3, 'user': 4, 'system': 5, ..................}

      dictionary.dfs    结果:{0: 2, 1: 2, 2: 2, 3: 2, 4: 3, 5: 3, 6: 2, 7: 2, 8: 2, 9: 3, 10: 3, 11: 2}

      参考博客:https://blog.csdn.net/qq_19707521/article/details/79174533

    9、char_df = pd.concat([char_series, char_prop], axis=1)

    10、train_list = train_data.merge(questions, how="left", left_on="q1", right_on="qid").drop("qid", axis=1)

    11、

      train_list = train_data.merge(questions, how="left", left_on="q1", right_on="qid").drop("qid", axis=1)
      train_list.head()
      train_list = train_list.rename(columns={"words": "w1", "chars": "c1"})
      train_list = train_list.merge(questions, how="left", left_on="q2", right_on="qid").drop("qid", axis=1)
      train_list = train_list.rename(columns={"words": "w2", "chars": "c2"})
      train_list["wnum1"] = train_list["w1"].map(len)
      train_list["cnum1"] = train_list["c1"].map(len)
      train_list["wnum2"] = train_list["w2"].map(len)
      train_list["cnum2"] = train_list["c2"].map(len)
      train_list["wboth"] = train_list.apply(lambda x: len([t for t in x["w1"] if t in x["w2"]]), axis=1)
      train_list["cboth"] = train_list.apply(lambda x: len([t for t in x["c1"] if t in x["c2"]]), axis=1)
      train_list.head()

    12、label = train_data["label"].values.copy()  值的复制

    13、question_feature = total_question.value_counts().reset_index()       

      value_counts确认数据出现的频率  注意:from collections import Counter  Counter()函数也能计数,返回值格式可能不一致

      .reset_index 是改变index,重置index

    14、

      unique_question = total_question.drop_duplicates().reset_index(drop=True)
      question_dict = pd.Series(unique_question.index,unique_question).to_dict()

      注释:.drop_duplicates() 去重     .reset_index()重置index      series.to_dict()转换为字典、

    15、 from keras.preprocessing.text import Tokenizer

      char_tokenizer = Tokenizer()
      char_tokenizer.fit_on_texts(question_data["chars"])
      char_tokenizer.word_index

          使用Tokenizer的方法是,首先用Tokenizer的 fit_on_texts 方法学习出文本的字典,然后word_index 就是对应的单词和数字的映射关系dict,通过

          这个dict可以将每个string的每个词转成数字

    16、word_count = sorted(list(word_tokenizer.word_counts.items()), key=lambda x: x[1], reverse=True)

      lambda是一个隐函数,是固定写法,不要写成别的单词;x表示列表中的一个元素,在这里,表示一个元组,x只是临时起的一个名字,你可以使用任意的名字;x[0]表

      示元组里的第一个元素,当然第二个元素就是x[1];所以这句命令的意思就是按照列表中第一个元素排序     

         items() 函数以列表返回可遍历的(键, 值) 元组数组

      reverse=true参数实现倒序排列 

    17、data["word_same"] = data.apply(lambda x: len(set(x["words1"]).intersection(set(x["words2"]))), axis=1)

      解释:假设a,b为两个list 
        1、交集

        list(set(a).intersection(set(b)))

        2、并集

        list(set(a).union(set(b)))

        3、差集(list a中有,而list b中没有的)

        list(set(a).difference(set(b)))

     18、word_embedding_data = pd.read_csv(WORD_EMBED_PATH, delimiter=" ", header=None, index_col=0)

       分隔符为空格、列表头为none、列的索引去第0列

       

  • 相关阅读:
    kubernetes-handbook 阅读笔记
    kubernetes-notes--阅读笔记
    SpringInAction4笔记——复习
    spring源码解析——2容器的基本实现(第2版笔记)
    把node加入master节点时,日志内容分析
    初始化master节点时,日志内容分析
    Mac OS用minikube安装单节点kubernetes
    Mac OS用vmvare安装多节点kubernetes
    FatMouse's Speed 基础DP
    FatMouse and Cheese 动态化搜索
  • 原文地址:https://www.cnblogs.com/smartwhite/p/9580676.html
Copyright © 2020-2023  润新知