This article is participating in “Python Theme Month”

It’s important to learn Python, but it’s even more important to learn PIP after you’ve learned the basics!

As easy as installing an app on your phone, the student committee takes you to install PIP on your host, virualenv, and tensorFlow, a deep learning framework! A lot of knowledge, patience to see.



Student committee wants to say, Python is just like plain boiled water, not so difficult, grasp, the above things to play!

You already knew Python but didn’t find any interesting libraries, so you ended up playing games in the middle, or learning Java/C++ in the middle. What a waste of talent! Ha, ha, ha

(For the record, besides Chinese, Java is the favorite language of the school committee. If you are friends with Java, please give me a three-line.)

What is the PIP

Let’s start with the Python library, which is like a mobile App

Xiao Bai can be understood as a Python library, just like an App in the phone, a normal application.

A Python library is a collection of Python code that is organized into a folder or multiple code libraries that are distributed as a single unit.

Many developers make a good library and then publish it on the Pypi website (below), which is the basis of open source Python.

And PIP, it’s like App Treasure

For xiao Bai, PIP is like the App market App in your Huawei phone or something like Tencent App Bao App.

It allows you to search for and download other apps. PIP is a tool for installing Python libraries, similar to NodeJS ‘NPM.

Developers can contribute their code to Pypi and use PIP to download a Python module from Pypi.

This allows us to introduce modules from other python libraries into the project.

The requests module, for example, can introduce links for accessing HTTP (s).

Here are some helpful tips for using PIP:

Let’s install the app on our phone

That’s right. Install PIP on our cloud host.

Check the version number first, many servers install Python 2.7 by default (there are many lazy people who don’t upgrade Python, and there are still many projects that use Python2).

Python -v # Check the version numberCopy the code

PIP for PYTHon2

If you have a Linux operating system (cloud/virtual machine), execute the following script to install PIP.

#! /bin/sh
# PIP for PYTHon3 install PIP for PYTHon3
#python3
#getPip=https://bootstrap.pypa.io/get-pip.py
#python2GetPip pipFile = = https://bootstrap.pypa.io/pip/2.7/get-pip.py. / get - PIP. Pyif [ ! -e ${pipFile}];then 
    echo "download pip"
    wget ${getPip} -O ${pipFile}
fi

python ${pipFile}
Copy the code

Installation effect diagram:



Well, at this point, you’ve installed your first Python application, and you’re ready to triple.

Try PIP to install the library

View version: pip-v



In fact, PIP itself is one of the most popular applications in the Python community.

It is also a Python library in its own right.

# PIP search requests for this library
pip search requests
# PIP installs the requests library
pip install requests
Copy the code

Configure a domestic Pypi mirror warehouse

Because the host of the school committee is from Tencent cloud, it is equipped with Tencent’s warehouse! (If Tencent Cloud is reading this, they want to give more discounts to bloggers, hahaha)

[global]
index-url = http://mirrors.tencentyun.com/pypi/simple
trusted-host = mirrors.tencentyun.com
Copy the code

Some Pypi mirrors in the country

  • Ali cloud mirrors.aliyun.com/pypi/simple…
  • Tsinghua university pypi.tuna.tsinghua.edu.cn/simple/

Verify whether the configuration takes effect:

Check your installed application – PIP check the local library

Like the following command:

Check the python library installed locally
pip list
For example, if you have TensorFlow installed, you can see the details of this library, version dependencies, etc
pip show tensorflow
Copy the code



Check out the TensorFlow deep learning library for information

Install other ‘apps’ with PIP

Install virtualenv

Virtualenv is an environment that provides a separate project environment that isolates python projects from non-official releases of python dependencies such as site-packages.

Of course, it’s also a Python Module. With it, you can let each of your projects independently manage the Python libraries they depend on.

Virtualenv is, in a nutshell, the equivalent of giving you a backup phone. You can install other apps on it, and any random installation will not affect the use of the main phone. Similarly, you can use Virtualenv to create multiple alternate mobile environments.

The following is more around the formal statement (small white can not see).

With VirtualEnv, you can manage a single project’s (own) library without mixing it with the system Python public library path! Multiple Python projects can also use different versions of the Python library independently of each other.

Virtualenv can be used to isolate python projects. (In fact, this tip should be given to three!)

See here, go ahead and install

pip install virtualenv
Copy the code

PIP installs the Virtualenv library and installs it on the PC. This is the app market, just like downloading and installing an app!

Play with the deep learning framework TensorFlow

Use virtualenv to create a project and go to the directory to activate switch to the virtual environment

virtualenv tensorflow-demo 
cd  tensorflow-demo
source bin/activate
Copy the code

So let’s see how it works

A total of 5 iterations were performed to train the model, and the model accuracy was assessed at 0.9768 (6 percentage points higher than the first generation 0.9147).

Specific operations and project configuration

Install tensorflow

pip install tensorflow
Copy the code

Lei school committee suggested that everyone first collection, lazy people directly copy the code:


import tensorflow as tf

# http://yann.lecun.com/exdb/mnist/ - mnist dataset, which is convenient machine learning beginners of some professional to sort out some training data
mnist = tf.keras.datasets.mnist

# Load training data (60K samples) and test data (10K samples)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

# Define a model
model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28.28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')])Compile the optimizer, loss function, and test index for the configuration model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Start training and perform 5 iterations
model.fit(x_train, y_train, epochs=5)
# Evaluation model, where verbose=2 indicates that each generation is processed as a line of output
model.evaluate(x_test,  y_test, verbose=2)
Copy the code

This model recognizes 60K picture samples and conducts 5 rounds of iterative model training. (Official 60K, next post for everyone to count)

Finally, the test data set is used to evaluate the trained model.

Is the above code not understand? Come and see the following people talk.

This process is equivalent to the entire font library of 100 characters, you take 90 characters to copy, copy five times brush characters, each time to see the standard font fit rate (accuracy).

Finally throw another 10 words to you to write, write to see your writing accuracy, write enough standard. You’re like a model that’s being trained, the word bank/test word bank is data for training and if you train enough of it, you’ll be able to write better looking and more standard words, okay? The same goes for learning knowledge.

All right, the focus of this article is to install master PIP while playing with the first DL application.

Now look at this picture. Do you get it? Remember this picture!



I’m going to leave it there. Congratulations, see here has been worth it, a few minutes to kung fu, learn so much, to a three consecutive not too much.

Continuous learning and continuous development, I am the Lei School committee! Programming is fun, but the key is to get the technology straight. Welcome to follow, and feel free to like the collection!

Refer to the link

Python Shared central repository VirtualEnv TensorFlow beginners quick start