PYTHON I am having issues with this code everytime i tried to run it, it says th
ID: 3806486 • Letter: P
Question
PYTHON
I am having issues with this code everytime i tried to run it, it says that 'file' is not define can someone help me wth this. The file that I am using is call 'muscle1.csv' Thank you in advance
This is the information that the file contains:
BodyMass
WorkLevel
Heat Production
43.7
19
177
43.7
43
279
43.7
56
346
54.6
13
160
54.6
19
193
54.6
43
280
54.6
56
335
55.7
13
169
55.7
26
212
55.7
34.5
244
55.7
43
285
58.8
13
181
58.8
43
298
60.5
19
212
60.5
43
317
60.5
56
347
61.9
13
186
61.9
19
216
61.9
34.5
265
61.9
43
306
61.9
56
348
66.7
13
209
66.7
43
324
66.7
56
352
Executable Code:
#import numpy
import numpy as n
#import for printing
from __future__ import print_function
#import for regression
from sklearn import linear_model
#import for plotting axes
from mpl_toolkits.mplot3d import Axes3D
#import matplotlib
import matplotlib.pyplot as pl
#Create empty list bodyMass and workLevel
bodyMass, workLevel = [], []
#Create empty list heatProduction
heatProduction = []
# Read data from the file
def readFile(filePath):
#Open the file in read mode
file = open(filePath,"r")
#Read each line of the file
lines = file.readlines()
#Close the file
file.close()
#For each line in the list lines
for l in lines:
# Add to list BodyMass
bodyMass.append(float(l.strip().split()[0]))
#Add to list WorkLevel
workLevel.append(float(l.strip().split()[1]))
# Add to list HeatProduction
heatProduction.append(float(l.strip().split()[2]))
# Return array
return n.array([bodyMass, workLevel]).T, heatProduction
# Plot
def plotFigures(Number, elevation, azi, clf,c):
#Initialize figure
figur = pl.figure(Number, figsize=(4, 3))
pl.clf()
# 3D axes
axe = Axes3D(figur, elev=elevation, azim=azi)
# Scatter plot
axe.scatter(bodyMass, workLevel, heatProduction, c='k', marker='+')
# Plot X in a range
X = n.arange(55, 85, 0.5)
# Ploy Y in a range
Y = n.arange(90, 180, 0.5)
# Create a mesh grid with X, Y
X, Y = n.meshgrid(X, Y)
# Calculation for third axes
Z = c[0] + c[1]*X + c[2]*Y
#Plot X,Y,Z
axe.plot_surface(X, Y, Z,alpha=.5, antialiased=True,rstride=200, cstride=100, cmap=pl.cm.coolwarm)
# Set X label
axe.set_xlabel('BODYMASS', color='b')
# Set Y label
axe.set_ylabel('WORKLEVEL', color='b')
# Set Z label
axe.set_zlabel('HEATPRODUCTION', color='b')
# Set tick labels for X axis
axe.w_xaxis.set_ticklabels([])
# Set tick labels for Y axis
axe.w_yaxis.set_ticklabels([])
# Set tick labels for Z axis
axe.w_zaxis.set_ticklabels([])
#Set locator for Z axis
axe.zaxis.set_major_locator(pl.LinearLocator(10))
# X axis srtring formatter
axe.xaxis.set_major_formatter(pl.FormatStrFormatter('%.f'))
# Y axis srtring formatter
axe.yaxis.set_major_formatter(pl.FormatStrFormatter('%.f'))
# Z axis srtring formatter
axe.zaxis.set_major_formatter(pl.FormatStrFormatter('%.f'))
# Main function
if __name__ == '__main__':
# Read the file
a, b = readFile('in.dat')
# Find linear regression
linreg = linear_model.LinearRegression()
# Fit the model
linreg.fit(a, b)
# Calculated coefficients
print ('Linear Regrssion Coefficients: ',linreg.coef_)
# Print independent terms
print ('Linear Regression independent term: ',linreg.intercept_)
# Create three figures
c = [linreg.intercept_,linreg.coef_[0], linreg.coef_[1] ]
# Set Elevetaion
elev = 44.5
# Set axim
azim = -105
# Plot
plotFigures(1, elev, azim, lin_reg,c)
# Plot title
pl.title('Linear regression-1', color='r')
# Set Elevetaion
elev = -.6
# Set axim
azim = 0
# Plot
plotFigures(2, elev, azim, lin_reg,c)
# Plot title
pl.title('Linear regression-2', loc='left', color='r')
# Set Elevetaion
elev = -.6
# Set axim
azim = 90
# Plot
plotFigures(3, elev, azim, lin_reg,c)
# Plot title
pl.title('Linear regression-3', loc='right', color='r')
# Show the plot
pl.show()
BodyMass
WorkLevel
Heat Production
43.7
19
177
43.7
43
279
43.7
56
346
54.6
13
160
54.6
19
193
54.6
43
280
54.6
56
335
55.7
13
169
55.7
26
212
55.7
34.5
244
55.7
43
285
58.8
13
181
58.8
43
298
60.5
19
212
60.5
43
317
60.5
56
347
61.9
13
186
61.9
19
216
61.9
34.5
265
61.9
43
306
61.9
56
348
66.7
13
209
66.7
43
324
66.7
56
352
Explanation / Answer
Python is a language that works with no delimeter other then a colon and indentation.
If you are writing a line of code with a IF statement and the next line will come with a single space.
Same is with other conditions or statements which has a depending statement inside it.
So, in your case after defining Def Readfile(filepath), next line should have a single space. I assume this is happening because of indentation issue.
You can apply my suggestion on other parts of your code and provide proper spaces and colons.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.