Data analysis and mapping for python learning

Data Analysis Drawing Library Matplotlib

* Legend represents the content in the graphic
* Grid, dashed lines in graphics, True displays grid
Point Markers: Represents the shape of a point.

There are three basic visual elements: point, line, and column.Analyze what graphics are drawn for the following requirements?
Look at the relationship between the time spent on a course and the test results.


make markup style


Marker color

First Graphic Drawing Completion Order

#1). Import Drawing Gallery
import  matplotlib.pyplot as plt
import  numpy as np
#2). Create a drawing board figure
figure = plt.figure(figsize=(10, 10))
#3). Create subplots/Axes
ax = plt.subplot(1, 1, 1)
#4). Preparing data
#Divide from 0-50 into 100 portions
x = np.linspace(0, 10, 100)
y = np.sin(x)
#6). Drawing
ax.plot(x, y, color='orange', marker='*', linestyle='-.')
ax.set_title('y = sinx')
ax.set_xlabel('x')
ax.set_ylabel('y')
#7). Displaying graphics
plt.show()

First Graphics Drawing Quick Edition

import matplotlib.pyplot as plt
import numpy as np

#1). Preparing data information
x = np.linspace(0, 20, 100)
y = np.sin(x)

#2). Direct Drawing
plt.plot(x, y)
#plt.scatter(x, y)
plt.title('y = sinx')
plt.xlabel('x')
plt.ylabel('y')

#3). Draw graphics and display
plt.show()

Multigraph drawing
Multi-Picture Case

Drawing steps

Case 1: Scatter and Line
Drawing requirements: A discrete graph based on a function and vibrating within a certain range.What about vibrations in a range? y Add a random number

1.1 Scatter plotting

.
Subgraph Drawing

#1). Import Drawing Gallery
import  matplotlib.pyplot as plt
import  numpy as np
#2). Create a drawing board figure
figure = plt.figure(figsize=(10, 10))
#3). Create subplots/Axes to generate subplots of two rows and one column.
#The first row, the first column, draws the sinx graph, and the second row, the first column, draws the cosx graph
ax1 = plt.subplot(2, 1, 1)
ax2 = plt.subplot(2, 1, 2)
#4). Preparing data
#Divide from 0-50 into 100 portions
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
#6). Drawing
ax1.plot(x, y1, color='orange', linestyle='-.')
ax1.set_title('y = sinx')
ax1.set_xlabel('x')
ax1.set_ylabel('y')

ax2.plot(x, y2, color='m', linestyle='-.')
ax2.set_title('y = cosx')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
#7). Displaying graphics
plt.show()

1.2 Curve

2.

Case 2: Column chart
A bar chart, also known as a column chart, is a statistical chart that takes the length of a rectangle as a variable and has a rectangular shape.
Degree is proportional to its corresponding variable value.Suppose a survey was conducted on the purchase of drinking water by male and female college students.
Table:

Vertical bar drawing

import matplotlib.pyplot as plt

    # 1. Create figure
    fig = plt.figure()

    # 2. Create subgraphs (1 row, 2 columns)
ax1 = plt.subplot(1, 2, 1)
ax1.set_title('Investigation Results of Male Students'Purchase of Drinking Water')
ax2 = plt.subplot(1, 2, 2)
ax2.set_title('Investigation Results of Drinking Water Purchase by Girls')

    # ***********How to Solve Chinese Scrambling
    plt.rcParams['font.sans-serif'] = ['SimHei']

    # 3. Load data information
waters = ['Carbonated drinks', 'Green Tea', 'Mineral water', 'Other', 'fruit juice']
boy_buy_num = [6, 7, 6, 2, 1]
girl_buy_num = [9, 4, 4, 6, 5]

    # 4. Draw bar charts
    bar_width = 0.4
    ax1.bar(waters, boy_buy_num, bar_width, color='orange')
    ax2.bar(waters, girl_buy_num, bar_width, color='g')

    # 5. Save pictures locally
    # plt.show()
plt.savefig('bar.png')

Horizontal bar drawing

    import matplotlib.pyplot as plt

        # 1. Create figure
        fig = plt.figure()

        # 2. Create subgraphs (1 row, 2 columns)
    ax1 = plt.subplot(2, 1, 1)
    ax1.set_title('Investigation Results of Male Students'Purchase of Drinking Water')
    ax2 = plt.subplot(2, 1, 2)
    ax2.set_title('Investigation Results of Drinking Water Purchase by Girls')

        # ***********How to Solve Chinese Scrambling
        plt.rcParams['font.sans-serif'] = ['SimHei']

        # 3. Load data information
    waters = ['Carbonated drinks', 'Green Tea', 'Mineral water', 'Other', 'fruit juice']
    boy_buy_num = [6, 7, 6, 2, 1]
    girl_buy_num = [9, 4, 4, 6, 5]

        # 4. Draw bar charts
    bar_width = 0.4
    ax1.barh(waters, boy_buy_num, height=bar_width, color='orange')
    ax2.barh(waters, girl_buy_num, height=bar_width, color='g')

        # 5. Save pictures locally
        # plt.show()
    plt.savefig('bar.png')

Case 2: Column chart

Side-by-side bar chart: To draw two bar charts showing the survey of boys and girls together, you can use bar twice.
Adjust the bar position coordinates and scale so that the two groups of bars can be displayed side by side


Parallel bar drawing

import matplotlib.pyplot as plt

import numpy as np

    # ***********How to Solve Chinese Scrambling
plt.rcParams['font.sans-serif'] = ['SimHei']
    # Load data information
waters = ['Carbonated drinks', 'Green Tea', 'Mineral water', 'Other', 'fruit juice']
boy_buy_num = [6, 7, 6, 2, 1]
girl_buy_num = [9, 4, 4, 6, 5]

    # Adjust the bars'x-coordinates
bar_width = 0.4
boy_index = np.arange(len(waters))  # [0, 1, 2, 3, 4]
girl_index = boy_index + bar_width  # numpy propagation mechanism, [0.4, 1.4, 2.4, 3.4, 4.4]

    # Draw a bar chart
plt.bar(boy_index, boy_buy_num, bar_width, color='orange', label='Schoolboy')
plt.bar(girl_index, girl_buy_num, bar_width, color='g', label='Girl student')
    # Modify meaningless coordinates to meaningful ones
    plt.xticks(boy_index + bar_width / 2, waters)
    plt.ylabel("Purchase volume")
    plt.title("Questionnaire on purchase of drinking water")

    plt.legend()
        # Save Picture to Local
            # plt.show()
plt.savefig('bar.png')

Visual Analysis of K-Nearest Neighbor Classification

Principle of K-neighbor algorithm
K nearest neighbor algorithm: red near Zhu, black near Ink.New data points belong to the same class as whoever is closest (one or more neighbors)

K Nearest Neighbor Algorithm Dataset
Several Toy Datasets are built into skilit-learn s, and some API s can generate numbers manually
The dataset, as shown in the following code:

K Nearest Neighbor Classification
Now that we have generated a series of datasets as training datasets for machine learning, the next step is to find one based on the KNN algorithm
The model then classifies the unknown data according to the model.

K Nearest Neighbor Regression Visualization Analysis
Regression principle of K-neighbor algorithm
The K-nearest neighbor algorithm can also be used for regression, with the same principle and classification.When calculating the predicted value for each data point, the model chooses to leave the
The nearest points of a data point, and their y-values are averaged as predictions for new data points

K Nearest Neighbor Classification Algorithm

from sklearn.datasets import  make_blobs
from sklearn.neighbors import  KNeighborsClassifier
import  matplotlib.pyplot as plt
    # Generate dataset, eg: x= (petal length, stem length), y= (first flower: Iris 1). Training set
    X, y = make_blobs(n_samples=200, n_features=2, centers=2, random_state=8)

        # How do I find a model and ultimately predict the classification of new test data based on it?
        # Machine Learning: The process of finding a function/model. f(x)='x x x x x x', f(image)='cat', f(alpha-go)=5x5, f('dialog')='dialog'
clf = KNeighborsClassifier()
clf.fit(X, y)       # Fitting (the process of finding a model)
test_data = [6, 3]  # Test Set: A dataset that tests the quality/correctness of a model
class_name = clf.predict([test_data])
print("The classification of new data points is: ", class_name)

    # # Given a new feature information, what kind of analysis does it belong to?
    # test_data = [6, 3]
    #
    # # By plotting the scatterplot, you can clearly see that there are two categories
    # plt.scatter(X[:, 0], X[:, 1], edgecolors='orange', color='white')
    # plt.scatter(test_data[0], test_data[1], marker='*', color='r')
    # plt.show()

k-neighbor regression algorithm

import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_regression
from sklearn.neighbors import KNeighborsRegressor

#Regression Generation Dataset (Training Set)
X, y = make_regression(n_samples=100, n_features=1, n_informative=1,
                       noise=50, random_state=8)

#Fitting/Finding Models Using K-Neighbor Regressors
reg = KNeighborsRegressor()
reg.fit(X, y)

#Given some new data (test set), predict y values
"""
//One-dimensional array: np.linspace [1, 2, 3, 4, 5]
n*1 array: rwshape(-1, 1)
reg.predict([[1], [2]])  # Forecast the information the city needs to convey
"""
test_x = np.linspace(-3, 3, 100).reshape(-1, 1)
test_y = reg.predict(test_x)
#print("Model Accuracy:", reg.score(X, y)))

#***********How to Solve Chinese Scrambling
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
#Draw Graphics
plt.scatter(X, y, marker='*', color='orange', edgecolors='orange', label='training set')
plt.plot(test_x, test_y, color='black', label='Test Set')
plt.title('KNN Regressor')
plt.legend()
plt.show()

seaborn Library

Official web address:
https://seaborn.pydata.org/introduction.html
seaborn Overall Style
Seaborn offers five thematic styles, darkgrid, whitegrid, dark, white, and ticks.
Use set() and set_style() functions to control the overall style


Set Subgraph Style
Style different subgraphs with the keyword


seaborn Content Style
Set the contents of the diagram, including line color, thickness, scale, etc.

Tags: Python

Posted on Mon, 04 May 2020 20:49:17 -0400 by sandsquid