春江暮客

春江暮客的个人学习分享网站

包含标签 python 的文章

Detailed Examples of Seaborn Plotting Kernel Density Curves

Detailed Examples of Seaborn Plotting Kernel Density Curves

2019年1月20日 | Technology

In a frequency distribution histogram, when the sample size is sufficiently enlarged to its limit, and the bin width is infinitely shortened, the step-like broken line in the frequency histogram will evolve into a smooth curve. This curve is called the density distribution curve of the population.

This article walks through how to use Seaborn with the Iris dataset in Pandas to plot several common kinds of kernel density curves.


1. Basic Density Curve

    import seaborn as sns
    import pandas as pd
    sns.set(color_codes=True)
    sns.set_style("white")
    df = pd.read_csv('iris.csv')
    sns.kdeplot(df['sepal_width'])

‘Detailed Examples of Seaborn Plotting Kernel Density Curves’

To plot a kernel density curve using Seaborn, you only need to use kdeplot. Note that a density curve only requires one variable; here we choose the sepal_width column.


2. Density Curve with Shading

    import seaborn as sns
    import pandas as pd
    sns.set(color_codes=True)
    sns.set_style("white")
    df = pd.read_csv('iris.csv')
    sns.kdeplot(df['sepal_width'], fill=True)

‘Detailed Examples of Seaborn Plotting Kernel Density Curves’

……

阅读全文

Python Implementation for Kugou Music MP3 Download

Python Implementation for Kugou Music MP3 Download

2019年1月13日 | Technology

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》

……

阅读全文

The difference between shallow copy and deepcopy in Python

The difference between shallow copy and deepcopy in Python

2018年12月29日 | Technology

In Python, copying objects is common, and a lot of “why did both variables change?” bugs come from mixing up three different operations: assignment, shallow copy, and deep copy.

“The difference between shadowcopy and deepcopy in python”

Among assignment (=), shallow copy (copy), and deep copy (deepcopy), the most confusing part is usually not assignment vs. copy, but whether a shallow copy still shares nested mutable objects.

Assignment does not copy an object. It only binds another variable name to the same object. Because of that, changes seen through one variable are still changes to the same underlying object.

A shallow copy duplicates the outer container, but nested mutable objects may still be shared. A deep copy recursively duplicates nested objects too, so it is usually more isolated.

A practical rule of thumb is:

  • Use assignment when you only want another name for the same object.
  • Use shallow copy when copying the outer container is enough.
  • Use deep copy when nested lists or dictionaries also need to be independent.

Below is a practical code example illustrating the differences among the three.

……

阅读全文

python3 requests module usage examples

python3 requests module usage examples

2018年12月28日 | Technology
This article explains common Python requests usage through examples including GET, POST, headers, authentication, file download, upload, and cookies, with a couple of practical habits that make real scripts more reliable.……

阅读全文

Kaggle Local Dog Breed Recognition

Kaggle Local Dog Breed Recognition

2018年12月15日 | Technology
This article shows how to run local dog-image prediction with a pretrained ResNet50 model, covering image preprocessing, model loading, prediction decoding, and the files needed for local inference.……

阅读全文

Drawing the Butterfly Curve with Python

Drawing the Butterfly Curve with Python

2018年11月7日 | Technology

The butterfly curve, discovered by Temple H. Fay, is a beautiful curve that can be expressed using a polar coordinate function. Because of its elegance, I wanted to use it as my blog’s favicon.ico. Here, I’ll use Python’s matplotlib.pyplot package to draw the desired butterfly curve. First, let’s admire the beautiful butterfly curve.


butter

1. First, We Need to Define the Mathematical Expression of the Butterfly Curve

math

math2

It can also be expressed using spherical coordinates:

math3


2. Choosing matplotlib.pyplot as the Plotting Tool in Python

1. First, import the necessary Python packages

import numpy as np
import matplotlib.pyplot as plt

2. Set the parameter values

t = np.arange(0.0, 12*np.pi, 0.01)
x = np.sin(t)*(np.e**np.cos(t) - 2*np.cos(4*t)-np.sin(t/12)**5)
y = np.cos(t)*(np.e**np.cos(t) - 2*np.cos(4*t)-np.sin(t/12)**5)

3. According to the formula, use numpy functions with plt to draw the required image

plt.figure(figsize=(8,6))
plt.axis('off')
plt.plot(x,y,color='blue',linewidth = '2')
#plt.show()
plt.savefig("butter.jpg",dpi=400)

butter_fly

4. Use Pillow to resize the image to an appropriate size for a favicon

from PIL import Image
im = Image.open("butter.jpg")
favicon = im.resize((50,50))
favicon.save("favicon.ico")

image_ico

……

阅读全文

友情链接

其它