[PYTHON] Use numpy's .flatten () [0] to retrieve the value

From extracting the value from the inner product of the vector

The inner product of vectors or the multiplication of the matrix of 1 row x column and x row 1 column is a value, but if you use it as an array, it should be in the form of an array [[]] or [].

import numpy as np
t1 = np.zeros(shape = (3,1))
y2 = np.arange(3).reshape(3,1)
print(t1)
print(y2)
print(t1-y2)
print((t1-y2).T)
print((t1-y2).T.dot(t1-y2))

Execution result

[[ 0.]
 [ 0.]
 [ 0.]]
y2
[[0]
 [1]
 [2]]
t1-y2
[[ 0.]
 [-1.]
 [-2.]]
(t1-y2).T
[[ 0. -1. -2.]]
(t1-y2).T.dot(t1-y2)
[[ 5.]]```

 I want to get the value from here, but it seems that .flatten () is not enough

#### **`print((t1-y2).T.dot(t1-y2).flatten())`**

[ 5.]



 Therefore, I will specify that it is the 0th element.

#### **`print((t1-y2).T.dot(t1-y2).flatten()[0])`**

->5.0```

Solved safely

Recommended Posts

Use numpy's .flatten () [0] to retrieve the value
How to retrieve the nth largest value in Python
How to use the generator
How to use the decorator
How to use the zip function
How to use the optparse module
How to use the ConfigParser module
How to use the Spark ML pipeline
[Linux] How to use the echo command
How to use the Linux grep command
LightGBM predict contributes to the predicted value
How to use the IPython debugger (ipdb)
3 best ways to use the less command
How to use MkDocs for the first time
Use the retry processing mode added to Boto3
Use the MediaWiki API to get Wiki information
Specify the Python executable to use with virtualenv
How to use the graph drawing library Bokeh
How to use the Google Cloud Translation API
How to use the NHK program guide API
The easiest way to use OpenCV with python
The trick to write flatten concisely in python
[Algorithm x Python] How to use the list
Use the Python framework "cocotb" to test Verilog.
Describe ec2 with boto3 and retrieve the value
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
Use pygogo to get the log in json.
I want to use the activation function Mish
Return one-hot encoded features to the original category value
How to use the Raspberry Pi relay module Python
I wanted to use the Python library from MATLAB
Linux user addition, how to use the useradd command
[Python] How to use the graph creation library Altair
How to use the grep command and frequent samples
How to use the exists clause in Django's queryset
Specify MinGW as the compiler to use with Python
[Introduction to Udemy Python3 + Application] 27. How to use the dictionary
[Introduction to Udemy Python3 + Application] 30. How to use the set
How to use argparse and the difference between optparse
How to use the model learned in Lobe in Python
How to use the Rubik's Cube solver library "kociemba"
(Remember quickly) How to use the LINUX command line
The programming language you want to be able to use
I want to use the R dataset in python
How to use xml.etree.ElementTree
How to use Python-shell
[C / C ++] Pass the value calculated in C / C ++ to a python function to execute the process, and use that value in C / C ++.
How to use tf.data
How to use virtualenv
How to use Seaboan
How to use image-match
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
Easy to use Flask
How to use partial
How to use Bio.Phylo
How to use SymPy
How to use WikiExtractor.py
How to use IPython