Python: i cant do substitution - python-2.7

I'm using python2.7
I ran this code
print r.data
print r[0][action-1].data
then it throws this.
[[ 0.34642464 0.39359313 -1.24270797 -0.89923799 0.11451679 -0.49929592]]
-0.499295920134
then, i tried to substitution "r" like this
r[0][action-1].data = 1
but i couldn't.
print r[0][action-1].data
-0.499295920134
what's my problem?
i imported this.
import numpy as np        
import matplotlib.pyplot as plt
import cv2
import random
import chainer
from chainer import cuda
from chainer import serializers
import chainer.functions as F
from chainer import optimizers
print(type(r)) is
class 'chainer.variable.Variable'
print (type(r[0])
is also
class 'chainer.variable.Variable'
  

Related

I am trying to import gensim, pandas and numpy in my django project but getting import error

I am getting an import error as i try to import gensim, pandas and numpy in django views.py . It works fine in shell.
How can I import them in views.py. I have installed the libraries in virtual environment.

How to solve KeyError?

I was trying to create some steps in the Abaqus by using the following python code. Unfortunately having this error. Anybody, please help me...
KeyError:model_name
Python Code:
from abaqus import *
from abaqusConstants import *
import __main__
import section
import regionToolset
import displayGroupMdbToolset as dgm
import part
import material
import assembly
import step
import interaction
import load
import mesh
import optimization
import job
import sketch
import visualization
import xyPlot
import displayGroupOdbToolset as dgo
import connectorBehavior
def create_step(model_name, new_step, previous_step):
mdb.models['model_name'].StaticStep(name='new_step', previous='previous_step', initialInc=0.025,
maxInc=0.025)
session.viewports['Viewport: 1'].assemblyDisplay.setValues(step='new_step')
model_name = 'Model-' + str(0)
new_step = 'C4'
previous_step = 'C3'
create_step(model_name, new_step, previous_step)
Replace mdb.models['model_name'].Stat... with mdb.models[model_name].Stat...
def create_step(model_name, new_step, previous_step):
mdb.models['model_name'].StaticStep(name='new_step', previous='previous_step', initialInc=0.025,
maxInc=0.025)
session.viewports['Viewport: 1'].assemblyDisplay.setValues(step='new_step')
2nd line should be,
mdb.models[model_name].StaticStep(name='new_step', previous='previous_step', initialInc=0.025,
maxInc=0.025)

Trying to plot multivariate function in 3D matplotlib; returns empty figure

I am trying to plot a function F(x1,x2) in 3D matplotlib, follwoing a tutorial from here:
http://glowingpython.blogspot.com/2012/01/how-to-plot-two-variable-functions-with.html
Once I try to run the code, the figure turns out to be empty, not even the axes output is seen. I was wondering if anyone could figure out the resaon behind this behavior. I am using python 2.7
from __future__ import division
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
import math
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
from matplotlib import pylab
from numpy import arange,array,ones
from scipy import stats
import numpy
import matplotlib.ticker as mtick
import sys
import os
# the function that I'm going to plot
def z_func(x1,x2):
return exp(-(1-x1)**2 - 100*((x2-x1**2)**2))
x1 = arange(5.0,-5.0,-0.01)
x2 = arange(-5.0,5.0,0.01)
X1,X2 = meshgrid(x1, x2) # grid of point
Z = z_func(X1, X2) # evaluation of the function on the grid
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(X1, X2, Z, rstride=1, cstride=1, cmap=cm.RdBu,linewidth=0, antialiased=False)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')
ax.set_zlabel('z-axis')
ax.view_init(elev=25, azim=-120)
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
Your code works for me. I just needed to wait till the computer will finish the computation. The long computation is because of the size of x1 and x2. Try to change these lines:
x1 = arange(5.0,-5.0,-0.01)
x2 = arange(-5.0,5.0,0.01)
to the following lines:
x1 = arange(5.0,-5.0,-0.1)
x2 = arange(-5.0,5.0,0.1)
p.s. I advise you to arrange your imports. You only need the following:
from numpy import exp, arange
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter
from pylab import meshgrid
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm

py2exe compiled .exe won't start

I compiled my prototype Application with py2exe to check its function as an exe, and run into 0 errors until I go to start it. Nothing happens. A process starts with my app name, it thinks for a few seconds, then nothing. No log file is generated. The app works great when run in python environment, but not in the compiled exe. I've given my setup code below. Any ideas? :
from distutils.core import setup
import py2exe, sys, os
import matplotlib
import FileDialog
import dateutil
sys.argv.append('py2exe')
setup( windows=['ATLAS.pyw'], data_files=matplotlib.get_py2exe_datafiles(),
options = {"py2exe": {
"includes": "decimal, datetime",
"packages": ["FileDialog", "dateutil"],
'bundle_files': 2,
'compressed': True}
},
zipfile = None
)
Hooks utilized in the Application:
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from pandas.sandbox.qtpandas import DataFrameWidget
from matplotlib.widgets import LassoSelector
from tkFileDialog import askopenfilename
from matplotlib.figure import Figure
import matplotlib.image as mpimg
from PySide import QtGui, QtCore
from matplotlib.path import Path
import pandas.io.sql as psql
from numpy import nonzero
import tkMessageBox as mb
from pylab import *
import pyodbc
import sys
import ttk
SOLVED:
So, using quick fingers (and compiling it with PyInstaller with the --debug option) I screen-capped the quickly-closing console window that contained the Traceback:
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\path\\dateutil\\zoneinfo/*.*'
The zoneinfo file was being saved in pytz instead of dateutil. A quick rename solved the problem.
Only issue though, if you want to compile with -F or --onefile it will not work due to the initial improper naming convention. Not quite sure how to fix that though.

cannot import name DjangoServiceBase

I have a problem. spyne example dosn't work.
>>> django.get_version()
'1.6.5'
from spyne.util.django import DjangoComplexModel, DjangoServiceBase
ImportError: cannot import name DjangoServiceBase