Data analysis Matplotlib basics

I want to learn python's Matplotlib to build graphs from my SEO analitics database to analyze data.

analize data graphic library with питон and Программирование|lower


VISUALISATION DATA

1. What libs we have in python? // Matplotlib and Seaborn are the most popular ones. Also ggplot, Bokeh, Plotly, Altair, Pygal.

 

MATPLOTLIB

1. Use numpy for x and y data arrays.

import numpy as np

x = np.array([0, 666])

y = np.array([0, 119])

2. To plot use:

import matplotlib.pyplot as plt

plt.plot(x, y)

plt.show()

3. I have to use points instead of continious line // plt.plot( x, y, 'o' ) or use scatter

3.1. Scatter:

plt.scatter(x, y)

plt.show()

4. To change point's size // plt.plot( x, y, 'o' , ms = 20)

5. To print two lines

plt.plot(x1,y1)

plt.plot(x2,y2)

plt.show()

6. To set color // plt.plot( x, y, 'o:r' , ms = 20)  or 'o:g', 'o:b' etc. But if to use two scatters they wil be automatically collored different.

7. Print multiple plots:

plt.subplot(number_of_rows, num_of_cols, which_plot_is_this)

plt.plot(x,y)

plt.title("current_title")

8. It is may be usefull to use transparency in case many dots may take the same place // plt.scatter(x, y, s=sizes, alpha=0.5)

9. Plots doesn't showned up. Warning: Matplotlib is currently using agg, which is a non-GUI backend.

9.1. Install tkinter $ sudo apt install python3-tk // Helped

 

 

USE YDF DECISION TREES

1. Create model // model = ydf.GradientBoostedTreesLearner(label="income").train(train_ds)

1.1. Analyse a model

a = model.analyze(test_ds)

a.to_file("/path/analysis.html")

1.2. I have to convert my Mongo data to pandas or csv.

I could make dataset:

mydataset = {

  'description': ["first descr", "second example", "something else"],

  'key': ["text one", "More text", "Additional key text"]

}

Then convert it to pandas dataframe:

myvar = pd.DataFrame(mydataset)

1.3. I have to convert label column data format from string to integer. // train_dsY.Ya = pd.to_numeric(train_dsY.Ya, errors='coerce')

1.4. I've got UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128) // try to delete comment column which contains a lot of ascii // doesn't help

1.4.1. Substitute in pandas all ascii column names to unicode. // df.rename(columns={'old1': 'new1', 'old2': 'new2'}, inplace=True)

1.4.2. Substitute 0 to 250.

for y in df.index:

if df.loc[y, "Ya"] == 0:

df.loc[y, "Ya"] = 250

1.4.2.1. i need to drop some useless columns // df = df.drop(columns=['Y№', 'G№'])

1.4.3. I have to divide dataset by label to two subsets. For example if places more then 10 and equal to zero then assign "BAD", otherwise "GOOD" to use classification model or try to use regression model.

model = ydf.RandomForestLearner(label="name_of_column", task=ydf.Task.CLASSIFICATION).train(train_ds)

 

 

 

!!!MAKE 3D VISUALISATION

1. For example x-axis - DescrVh, y-axis - KeyVh, z-axis Probability

2. Try Orange software for data visualization