Drawing a Stunning "Dream of the Red Chamber" Word Cloud with Python 3

Word clouds, which I’m sure you’ve all seen, are created using wordcloud, a famous Python library. This article will detail how to use wordcloud to create a word cloud for “Dream of the Red Chamber,” one of China’s Four Great Classical Novels.


1. Preparation

This involves three parts:

2. The wordcloud and jieba libraries, which can be installed using pip install wordcloud and pip install jieba.

3. Preparing a Chinese font file.

The .txt text file and font file are bundled together for your convenience to replicate this tutorial’s example.


2. Drawing the “Dream of the Red Chamber” Word Cloud

Here’s the code directly:

    from wordcloud import WordCloud
    import jieba
    text = "".join(jieba.cut(open("红楼梦.txt").read()))
    wordcloud = WordCloud(font_path="kaibold.ttf").generate(text)

    # Display the generated image:
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.margins(x=0, y=0)
    plt.show()

《Drawing a Stunning “Dream of the Red Chamber” Word Cloud with Python 3》

……

阅读全文

python3绘制炫酷的红楼梦词云

词云,相信大家都看到过,这是一个使用python的著名的词云工具wordcloud库绘制而成的,本文将详细介绍如何使用wordcloud绘制中国四大名著之一的《红楼梦》的词云。

1.准备工作

包括3部分:

2.wordcloud库和jieba库,可使用pip install wordcloud,pip install jieba进行安装

3.准备中文字体文件

txt文本文件和字体文件已打包,以方便大家直接复现本教程实例。

2.绘制《红楼梦》词云

直接上代码:

    from wordcloud import WordCloud
    import jieba
    text = "".join(jieba.cut(open("红楼梦.txt").read()))
    wordcloud = WordCloud(font_path="kaibold.ttf").generate(text)

    # Display the generated image:
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.margins(x=0, y=0)
    plt.show()

《python3绘制炫酷的红楼梦词云》

……

阅读全文

python生成器generator,yield

在python学习过程中,像这些使用if else流程控制或者说函数的定义和使用都比较好理解,但是生成器是一个相对来说很难理解的概念,使用也就相对困难,本文将详细解析python里面的生成器……

阅读全文

TypeError: ufunc 'isnan' not supported for the input types - Solution

Today, while using Python’s Seaborn to plot a heatmap (clustermap), I kept encountering this error. My data seemed perfectly fine, and a Google search didn’t yield any good solutions. After some exploration, I’m sharing the final solution here.


1. Generating the DataFrame

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from seaborn import clustermap
    import seaborn as sns; sns.set(color_codes=True)
    df = pd.DataFrame([["a","b","c","d","e","f"],[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8]],  columns=list('ABCDEF')).T
    df
    g = sns.clustermap(df.iloc[:,1:],cmap="PiYG")

After generating and transposing the DataFrame, a TypeError occurs: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule "safe".

《TypeError: ufunc ‘isnan’ not supported for the input types solution》


2. Cause of the Error

This type of error arises because the DataFrame has been transposed, and the original DataFrame contained string columns. Just like in the example above, the first column contains strings (values ‘abcdef’). When transposed, all numerical values in the DataFrame are also converted to object types instead of float or int numerical types. Therefore, trying to plot a heatmap with character types naturally leads to an error.

……

阅读全文

TypeError: ufunc 'isnan' not supported for the input types - Solution

After generating and transposing the DataFrame, a TypeError occurred: TypeError: ufunc ‘isnan’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule “safe”.

《TypeError: ufunc ‘isnan’ not supported for the input types - Solution》

2. Cause of the Error

This type of error occurs because the DataFrame has been transposed, and the original DataFrame contains a column with strings. Just like in the example above, the first column contains string values “abcdef”. After transposition, all numbers in the DataFrame also become “object” type instead of “float” or “int” numeric types. Therefore, when we try to plot a heatmap with character types, an error naturally occurs.

If the DataFrame originally contained only numeric types, there would be no issue here.

3. Solution

Knowing the cause, the solution is simple: convert the corresponding numeric columns in the transposed DataFrame to numeric types. Here’s the code:

……

阅读全文

TypeError: ufunc 'isnan' not supported for the input types解决办法

今天在使用python的seaborn画热图(clustermap)的时候,发现了总是出现这个错误,而且可以知道自己的数据完全是符合条件的,在搜索了谷歌后也没有找到好的解决方法,经过摸索后这里把最终解决方法告诉大家。

1.生成dataframe

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from seaborn import clustermap
    import seaborn as sns; sns.set(color_codes=True)
    df = pd.DataFrame([["a","b","c","d","e","f"],[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8]],  columns=list('ABCDEF')).T
    df
    g = sns.clustermap(df.iloc[:,1:],cmap="PiYG")

生成dataframe并转置后,出现类型错误,TypeError: ufunc ‘isnan’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ”safe”

……

阅读全文

Python Implementation for Kugou Music MP3 Download

After implementing python for Qianqian Music mp3 download, some users found that many songs couldn’t be searched on Qianqian Music. So today, Chunjian Muke extended the download functionality to Kugou Music, with source code provided.

Using the same approach, first search for a song directly on the Kugou official website. Then, open the network monitor in Google Chrome and search for the same keyword again. You’ll then be able to find the API information (Note: It’s best to view the network requests during the second search to filter out unnecessary information).


1. Analyzing Search API Information

《Python Implementation for Kugou Music MP3 Download》 With only 4 network requests, it’s easy to identify that the first request genuinely returns song information, so we can construct this request.

《Python Implementation for Kugou Music MP3 Download》

……

阅读全文

python实现酷狗音乐mp3下载

python实现千千音乐mp3下载 后小伙伴使用发现很多音乐在千千音乐都搜不到,所以今天春江暮客就拓展了一下酷狗音乐的下载,有源码。

同样的配方,首先在酷狗官网上直接搜索歌曲,然后打开谷歌浏览器的网络监视器,再次搜索同样的关键字就可以发现接口信息(注:此处最好再次搜索的时候查看网络,可以剔除很多多余的信息)。

1.分析搜索接口信息

《python实现酷狗音乐mp3下载》 只有4条网络请求,可以很方便的知道是第一条请求是真正的返回了歌曲信息,因此构造此条请求即可。

……

阅读全文

Drawing NetworkX Network Graphs in python3

NetworkX is a Python library for studying graphs and networks. NetworkX is free software released under the BSD-new license. It can be used to create and manipulate complex networks, and to study the structure and function of complex networks.

With NetworkX, you can load or store networks in standard or non-standard data formats. It can generate many types of random or classic networks, analyze network structure, build network models, design new network algorithms, and draw networks.

Of course, NetworkX alone cannot be powerful. Here, Chunjian Muke will use other widely used common Python libraries to draw various basic network graphs.


1. Drawing the Most Basic Network Graph

A network graph consists of nodes and edges. In NetworkX, each row of a pandas DataFrame represents the points in a connection, and a connection is generated at the corresponding position. In the example, a connection is generated between each corresponding position of ‘from’ and ’to’.

……

阅读全文

python3中NetworkX网络图绘制

NetworkX是一个用于研究图形和网络的Python库。 NetworkX是根据BSD-new许可证发布的免费软件。可用于创造和操作复杂网络,学习复杂网络的结构及其功能。 

有了NetworkX你就可以用标准或者不标准的数据格式加载或者存储网络,它可以产生许多种类的随机网络或经典网络,也可以分析网络结构,建立网络模型,设计新的网络算法,绘制网络。

……

阅读全文

python3搭配selenium实现12306抢票程序

春节临近了,小伙伴们都在忙着抢票了,很多人总是被各种乱七八糟的抢票程序误导,诱骗,不如自己用python写一个12306抢票脚本好了。

言归正传,有了需求自然就要实现。技术部分,抢票程序由selenium驱动chrome进行,主要分为几下几部分。(别问我为什么不直接用requests买,有点难0-0)

……

阅读全文

Python implementation for Qianqian Music MP3 download

Entering the Qianqian Music homepage and selecting Jay Chou’s “Confession Balloon” reveals that it’s a 2016 song with no preview available, which is sad. Is there a way to get the MP3 file? The answer is yes. A runnable program for music download is available at the end of the article.

Without further ado, I opened the charts and chose a new song that could be previewed; “Sheng Pi Zi” was the first one I could listen to.

Python implementation for Qianqian Music MP3 download

1. Analyze API information

Opening the developer tools, I found that music files are definitely submitted through an API. Among many requests, I found a request that could retrieve music files. See the image below:

Python implementation for Qianqian Music MP3 download

Check the request details:

Python implementation for Qianqian Music MP3 download The songid parameter can be found in the current URL: http://music.taihe.com/song/611238837. It’s simple. from should be “web” or “app”, etc. The format defining the return data type doesn’t need to be changed. method doesn’t need to be changed. The _ parameter is a 13-digit timestamp. callback is the prefix of the returned JSON data. The 1546915161467 after the underscore is a 13-digit timestamp, while the preceding 17200943498528136486 is unknown. We’ll try to use the known parameters to see if we can retrieve information without changing the unknown content.

……

阅读全文

最近文章

分类

标签

友情链接

其它