Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use NumPy, TensorFlow and scikit-learn

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use NumPy, TensorFlow and scikit-learn". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use NumPy, TensorFlow and scikit-learn.

NumPy

As described in module 4, the core of NumPy is its N-dimensional array, which also provides functions such as linear algebra and Fourier transform. The NumPy array is a very common input value in machine learning library functions. Therefore, when you have a dataset in a particular format and have to convert it to another format, you can often use NumPy directly. Or you can use NumPy as the result of a library function call.

As long as dimensions are meaningful, NumPy arrays can be created directly from nested lists, nested tuples, or combinations of them, with an unlimited number of dimensions.

Import numpy as nparr = np.array ([[1,2,3], (4,5,6)]) print (arr [0,1])

Here, we name numpy np and import it.

In addition, (0,1) is the tuple used as the index.

The NumPy array has slices that allow you to get a row or column:

# returns the first row as an one-dimensional vectorprint (arr [0,:]) # returns the first column as an one-dimensional vectorprint (arr [:, 0])

The same syntax applies to more dimensions (although it is difficult to call "rows" and "columns" here):

Arr = np.array ([[[1,2,3], [4,5,6]], [[7,8,9], [10,11,12]]]) print (arr [:, 0]) # [1,4], [7,10]] print (arr [1,:, 0]) # [7,10]

NumPy indexing and slicing are more powerful than this. See Resources for a more complete overview.

NumPy arrays can be stacked horizontally or vertically using hstackand (if the dimensions are correct) vstack, both of which take array tuples as parameters (get the correct number of parentheses! ):

Arr1 = np.array ([[1,1], [1,1]]) arr2 = np.array ([[2,2], [2,2]]) print (np.hstack ((arr1, arr2) print (np.vstack ((arr1, arr2)

Reshape is a powerful method in NumPy. As the name implies, it changes the shape of the array. Here is an example:

Vector = np.array ([1,2,3,4,5,6,7,8,9]) matrix = vector.reshape ((3,3))

The parameter to reshape is the new shape, a tuple of the desired dimension. This is a fairly simple example, but you can also use it to readjust arrays with more dimensions. Elements are read from the original array in a specific index order and written to the new array in the same index order. Refer to the reshape documentation for more information about the indexing order.

TensorFlow

In order to use neural networks at a high level, we studied Keras in the introduction to Keras. In essence, TensorFlow is a library for tensor computation.

Tensor is a generalization of vector and multidimensional matrix:

0-tensor is a scalar

1-Tensor is a vector

2-Tensor is a matrix

A 3-Tensor is. It's just a 3-Tensor.

Wait.

Tensors can hold any type of data: integers, floating-point numbers, strings, and so on. Although you don't usually encounter these when working with advanced libraries such as Keras, it's interesting to look at them because they are the underlying building blocks of TensorFlow.

So, what's the difference between a NumPy array and a tensor? Two objects represent the same data more or less, but the tensor is immutable.

TensorFlow can perform various operations on tensors. This is an example that starts with three matrices, performs matrix multiplication on the first two matrices, then adds the third matrix, and then inverts the result.

Import tensorflow as tfa = tf.constant ([[0.6,0.1], [0.4,0.3]]) b = tf.constant ([[1.2,0.7], [0.9,1.1]]) c = tf.constant ([[- 0.1,0.2], [0.3,0.1]]) d = tf.matmul (a, b) e = tf.add (c) D) f = tf.linalg.inv (e) sess = tf.Session () result = sess.run (f) # a NumPy array

The operation is not performed immediately. The result is calculated only when the session is created and run. Before the session is created, the above code builds an operation diagram and then evaluates it.

Scikit- learning

Scikit-learn is an extensive library that provides many traditional machine learning methods (very roughly: everything but machine learning). You can install it using pip in the Jupyter Notebook unit:

Python copy code

! pip install scikit-learn here, I believe you have a better understanding of "how to use NumPy, TensorFlow and scikit-learn". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report