Tag Archives: numpy

dtype=np.int error: DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`.

There is a line of code c = np.fromfile(“b.dat”, dtype=np.int, sep=”,”) when running Error,The content is as follows:

DeprecationWarning: `np.int` is a deprecated alias for the builtin `int`. To silence this warning, use `int` by itself. Doing this will not modify any behavior and is safe. When replacing `np.int`, you may wish to use e.g. `np.int64` or `np.int32` to specify the precision. If you wish to review your current use, check the release note link for additional information.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20 .0-notes.html#deprecations
c = np.fromfile(“b.dat”, dtype=np.int, sep=”,”)

Reason: numpy1.20 has deprecated np.int, checked my version

The numpy version has reached 1.23.2

Solution: Change to np.int16/np.int32 directly to specific bytes or change to np.int_

Run again, problem-solved!

 

How to Solve M1 chip import numpy Error

Error report of M1 chip import numpy

Environmental installation method:

miniforge – python3. 9 numpy 1.21.1

Operation error:

## terminal/pycharm error
import numpy
import pandas 

Error Messages:
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

At the same time, the python version and numpy version are given, and many files in the miniforge folder no such file are mentioned repeatedly

Solution:

conda install openblas

Import again works normally

numpy.concatenate() 253rd; 38169Keyerror: 0 [How to Solve]

numpy.concatenate()

Official Documentation Link
Function Description.
Join a sequence of arrays along an existing axis.
Join a sequence of arrays along an existing axis.

Program error

KeyError Traceback (most recent call last)
F:\Anacondaaa\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
in
—-> 1 np.unique(np.concatenate(movie_type[‘类型’].map(lambda x: x.split(’/’))))
<array_function internals> in concatenate(*args, **kwargs)
F:\Anacondaaa\lib\site-packages\pandas\core\series.py in getitem(self, key)
851
852 elif key_is_scalar:
–> 853 return self._get_value(key)
854
855 if is_hashable(key):
F:\Anacondaaa\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
959
960 # Similar to Index.get_value, but we do not fall back to positional
–> 961 loc = self.index.get_loc(label)
962 return self.index._get_values_for_loc(self, loc, label)
963
F:\Anacondaaa\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 0


Reasons and Suggestions
keyerror means no data, because np.concatenate itself is connected with the original index, and it doesn’t work because the original index is no longer continuous in the data cleaning process.
Reset the contiguous indexes or use other methods to join the list to achieve this effect.

Error reporting: attributeerror: module ‘******* has no attribute’ ******* and other problems are solved

Error reported:

resolvent:

1、 Install the third-party library directly on the command page

pip install xxxxxx

  2、 Select interpreter setting in the lower left corner of pychart, click the plus sign, enter the name of the third-party library, and then click Install pakage in the lower left corner to install.

  3、 The 2. X version of tensorflow does not support many third-party libraries. I have tried many methods, but I still think it is the most direct and effective (simple and crude) to install the 1. X version directly. Correspondingly, you may need to downgrade numpy because the installed tensorflow version and numpy version do not match.

Uninstall tensorflow first:

pip uninstall tensorflow

  To install a new version:

pip install tensorflow1.xx

  Check the applicable version of pycharm, and then install:  

pip install tensorflow==1.14 -i https://pypi.douban.com/simple

  After running the code, an error is reported indicating that the numpy version does not match:

For example, I report an error: futurewarning: passing (type, 1) or ‘1type’ as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,))/'(1,)type’.  _ np_ qint32 = np.dtype([(“qint32”, np.int32, 1)])

Recommended installation: or other version

pip install numpy==1.16.0

  Normal operation after.

[Solved] RuntimeError: Numpy is not available (Associated Torch or Tensorflow)

Runtimeerror: numpy is not available solution

Problem Description:

Today, the old computer crashed (cried) and the new computer got started. I couldn’t wait to install the pytorch and check the operation of the previous project. As a result, there was an error running the model training

the key sentence is the sentence loading the model training data

for i, data in enumerate(train_loader):

The bottom layer of this statement is applied to the operation of converting the tensor variable to numpy, that is

import numpy
import torch
test_torch = torch.rand(100,1,28,28)
print(test_torch.numpy())
# will produce the same error

Problem root

The direct correlation between pytoch and tensorflow is ignored
if there is only torch but no tensorflow in the environment, the tensor variable used by torch will lose the tensor related operation function, such as torch. Numpy()

Solution:

Install tensorflow in the basic environment, whether CPU version or GPU version
in Anaconda environment, you can directly

conda install tensorflow

Otherwise pip

pip install --ignore-installed --upgrade tensorflow-gpu

To silence this warning, use `float` by itself. Doing this will not modify any behavior and is safe.

import numpy as np 
e = np.ones((3,3),np.float)# 3x3 floating-point 2-dimensional array, and initialize all elements to value 1

D:\projects\Numpy\array.py:25: DeprecationWarning:
np.float is a deprecated alias for the builtin float.
To silence this warning, use float by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use np.float64 here.
Deprecated in NumPy 1.20;
for more details and guidance:
https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

Change to:

import numpy as np 
e = np.ones((3,3),np.float64)# 3x3 floating-point 2-dimensional array, and initialize all elements to value 1

All right, we’re in the trough

For the problem of rejecting old usage errors after numpy is updated, modified in numpy 1.20; for more details and guidance

There’s something wrong with numpy

Because the latest version of numpy is updated today, the previous usage is invalid

So You need to use the latest usage

Originally, my return value is like this

return np.array(df).astype(np.float)

Error:

DeprecationWarning: np.float is a deprecated alias for the builtin
float. To silence this warning, use float by itself. Doing this
will not modify any behavior and is safe. If you specifically wanted
the numpy scalar type, use np.float64 here.

Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
return np.array(df).astype(np.float)

It seems to be because of the version of the problem

If you look at the picture
Modify to the following can be.

return np.array(df, dtype=float)

perhaps

return np.array(df).astype(np.float64)

So you won’t make a mistake

No module named numpy error in Python code

Generally, numpy is installed in “control panel + CMD”

Enter “PIP install numpy” in the command line window

The numpy installed at this time is not in the directory line of Python

No module named numpy will report an error, even if “import numpy as numpy” is imported at the beginning of the python program

2. Solutions:

(1) It is to directly find the python installation location in the DOS window and directly enter the “PIP install numpy” statement

Enter enter and wait for installation

(2) Is it mandatory to download the latest version after installation

Find the installation location of Python in DOS window, and input “PIP install numpy — ignore installed numpy” statement

Will be forced to reload in a new location, this is to install the numpy package into python

Note that the following statement is entered in the win system

If you want to build a Linux system, you should add more “sudo” fields in front of it

Namely: “sudo PIP install numpy — ignore installed numpy”

This chapter is my opinion, let’s make progress together

numpy.random.rand()

numpy.random.randn (d0, d1, … , DN) is to return one or more sample values from the standard normal distribution.  
numpy.random.rand (d0, d1, … , DN) in [0,1].   

 

numpy.random.rand (d0,d1,… ,dn)

The rand function generates data between [0,1] according to the given dimension, including 0 and excluding 1DN table. The return value of each dimension is the array of the specified dimension

np.random.rand(4,2)
array([[0.64959905, 0.14584702],
       [0.56862369, 0.5992007 ],
       [0.42512475, 0.83075541],
       [0.75685279, 0.00910825]])

np.random.rand(4,3,2) # shape: 4*3*2
array([[[0.07304796, 0.48810928],
        [0.59523586, 0.83281804],
        [0.47530734, 0.50402275]],

       [[0.63153869, 0.19636159],
        [0.93727986, 0.13564719],
        [0.11122609, 0.59646316]],

       [[0.17276155, 0.66621767],
        [0.81926792, 0.28781293],
        [0.20228714, 0.72412133]],

       [[0.29365696, 0.53956076],
        [0.19105394, 0.47044441],
        [0.85930046, 0.3867359 ]]])

 

ValueError: Object arrays cannot be loaded when allow_ Pickle = false solution

When numpy is used to load a large amount of binary data, such as. NPY file and. Pkl file, the default allow is set_ Pickle = false will cause the error and find the corresponding np.load (path)

np.load(path, allow_pickle=True)

Or reduce numpy to below 1.16:

pip install numpy==1.15.0 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

It can be solved.

Python Numpy.ndarray ValueError:assignment destination is read-only

reference resources: http://stackoverflow.com/questions/13572448/change-values-in-a-numpy-array

###################################################################3

Get the video stream from the raspberry pie camera and convert it to opencv format:

http://blog.csdn.net/u012005313/article/details/51482994

At the same time, you want to operate on each frame, but there is an error:

ValueError:assignment destination is read-only

Images cannot be manipulated because they are read-only.

Find a way on stackhover: because in Python, the opencv image format is Numpy.ndarray You can modify the properties of ndarray by:

img.flags.writeable = True

####################################################################

Numpy.ndarray It has the following attributes:

C_ CONTIGUOUS(c_ contiguous)

F_ CONTIGUOUS(f_ contiguous)

OWNDATA(owndata)

WRITEABLE(writeable)

ALIGNED(aligned)

UPDATEIFCOPY(updateifcopy)

import numpy as np
help(np.ndarray.flags)

The flags property is information about the array memory layout

Among them, the flags attribute can be modified in the form of a dictionary, for example:

a.flags['WRITEABLE'] = True

You can also use lowercase attribute names:

a.flags.writeable = True

Abbreviations (‘c ‘/’f’, etc.) are only used in dictionary form

Only the attributes updateifcopy, writeable and aligned can be modified by users in three ways

1. Direct assignment:

a.flags.writeable = True

2. Dictionary input:

a.flags['WRITEABLE'] = True

3. Use function ndarray.setflags :

help(np.ndarray.setflags)