Why import numpy doesn't automatically include matlib - numpy

I'm trying to repeat a numpy array x horizontally using a=numpy.matlib.repmat(x,1,3). However, directly typing this results in error. I must add import numpy.matlib in order for a=numpy.matlib.repmat(x,1,3) to work.
My question is, why do I need to explicitly import numpy.matlib? I thought import numpy should automatically import everything that appears in the form numpy.* just like numpy.zeros, numpy.array and numpy.mean.

Related

How can i sort my dataframe with the most recent value at the bottom?

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df=pd.read_csv("BTC-USD.csv")
df=df.drop(["Date","Adj Close","Volume","Low","Close"],axis=1)
x=df["Open"]
y=df["High"]
Here is my dataframe
In my data frame , newest value is at the top. What i wanna do here is putting newest value to bottom and oldest value to top.
I understand that you just want to inverse line order without considering values in columns.
Thus, you need to use pandas.DataFrame.reindex function as follow:
reordered_df = df.reindex(df.index[::-1])
The documentation is here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.reindex.html

Why isn't this appending to a numpy file?

I'm trying to write as I go using numpy with multiple writes to savez_compressed - however, as you can see below, only the last write, b in this case, is preserved. Can savez_compressed not be appended to?
import numpy as np
with open("f.npz",'wb') as f:
np.savez_compressed(f, a=[1,2,3])
np.savez_compressed(f, b=['q', 'r', 's'])
b=np.load('f.npz')
print(b.files)

reading arrays from netCDF, why I get a size of (1,1,n)

I am trying to read and later on to plot data from a netcdf file. Some of the arrays contained at the .nc file that I am trying to store as variables, are created as a (1,1,n) size variable. When printing them i see [[[ numbers, numbers,....]]]. Why are these three [[[ are created? How can I read these variables as a simple (n,1) array?
Here is my code
import pandas as pd
import netCDF4 as nc
import matplotlib.pyplot as plt
from tkinter import filedialog
import numpy as np
file_path=filedialog.askopenfilename(title = "Select files", filetypes = (("all files","*.*"),("txt files","*.txt")))
file=nc.Dataset(file_path)
print(file.variables.keys()) # get all variable names
read_alt=file.variables['altitude'][:]
alt=np.array(read_alt)
read_b355=file.variables['backscatter'][:]
read_error_b355=file.variables['error_backscatter'][:]
b355=np.array(read_b355)
error_b355=np.array(read_error_b355)
the variable alt is fine, for the other two I have the aforementioned problem.
Is it possible that your variables - altitude, backscatter and error_backscatter - have more than one dimensions? Whenever you load that kind of data, the number of dimensions is kept by the netCDF library.
Nevertheless, what I usually do, is that I remove the dimensions that I do not need from the arrays by squeezing them:
read_alt = np.squeeze(file.variables['altitude'][:])
read_b355 = np.squeeze(file.variables['backscatter'][:]);
read_error_b355 = np.squeeze(file.variables['error_backscatter'][:]);

Pygraphviz and fixed node positions

How do I force pygraphviz to maintain fixed positions for my nodes. Assume that you have the following code
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
import pygraphviz as pgv
from _operator import pos
A=pgv.AGraph()
A.add_node(1,color='red',pos="0,1")
A.add_node(2,color='blue',pos="1,10")
A.add_node(3,color='yellow'pos="2,2")
A.add_edge(1,2,color='green')
A.add_edge(2,3)
A.add_edge(2,2,"1")
A.add_edge(1,3)
A.graph_attr['epsilon']='0.001'
print(A.string()) # print dot file to standard output
A.layout('dot') # layout with dot
A.draw('foo.pdf') # write to file
How do I force the nodes to show up at predetermined positions (0.1), (1,10 and respective (2,2)
Looking at http://pygraphviz.github.io/documentation/pygraphviz-1.4rc1/reference/agraph.html and the signature for the draw method, it looks as if layout is only needed if pos is not present.
"
If prog is not specified and the graph has positions (see layout()) then no additional graph positioning will be performed."
In your case you could just try without using A.layout(). just A.draw('foo.pdf')

Importing .py converted ipython notebook without changing pylab syntax

In an Ipython notebook, I have defined a function to plot data. I would like to import the function in my current notebook to use it.
I tried to convert the mynotebook.ipynb to a mynotebook.py file and then just do
from mynotebook import myfunction
But I get an error.
The problem is that I wrote the original notebook with starting ipython with the option pylab.
That means that to make it work, instead of writing something like
x = arange(1,10)
y = arange(1,10)
scatter(x, y)
I should change it to the more explicit:
import numpy as np
import matplotlib as plt
x = np.arange(1,10)
y = np.arange(1,10)
plt.scatter(x, y)
In my case my file is quite complex and changing it will be a pain...
Is there a way to import my notebook without modifying the .py file line by line???
from matplotlib.pylab import *
will take care of all of your imports, but some of the interactive stuff (show etc) might not work correctly.