简体   繁体   中英

How I can fix the error in: "Fit the PCA transformer on the training data and transform the data line" and "model = svm.SVC(kernel='linear')"

I have create this Support Vector machine model but I get errors in "Fit the PCA transformer on the training data and transform the data line" and "model = svm.SVC(kernel='linear')"

The first error is:

NameError: name 'x_train' is not defined

The second error is:

NameError: name 'svm' is not defined
import pandas as pd
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import seaborn as sns
import seaborn as sns # for data visualization

train_df = pd.read_csv('diabetes_data_upload.csv')
train_df.head()

# checking total of rows and columns
train_df.shape

# Transforming the Gender into 0 and 1
train_df["Gender"] = train_df["Gender"].map({"Male": 0, "Female": 1}).astype(int)
#Rounding the Age
train_df["Age"] = train_df.Age.round()

# Separating the data to predict the missing ages
X_train = train_df[train_df.Age.notnull()][['Age','Gender','weakness','Obesity', 'class']]
X_test = train_df[train_df.Age.isnull()][['Age','Gender','weakness','Obesity', 'class']]
y = train_df.Age.dropna()


# Just confirming if there is no more ages missing
train_df.Age.isnull().sum()

# Taking only the features that is important for now
X = train_df[['Gender', 'Age', 'weakness']]

# Taking the labels
Y = train_df['class']

# Spliting into 80% for training set and 20% for testing set so we can see our accuracy
X_train, x_test, Y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

# Declaring the SVC with no tunning
print(X_train.shape)
print(Y_train.shape)


from sklearn.decomposition import PCA

# Create a PCA transformer with 3 components
pca = PCA(n_components=3)

# Fit the PCA transformer on the training data and transform the data
x_train_pca = pca.fit_transform(x_train)

# Transform the test data using the PCA transformer fitted on the training data
x_test_pca = pca.transform(x_test)

# Fit the classifier on the transformed training data
classifier.fit(x_train_pca, y_train)

# Predict the labels for the transformed test data
predictions = classifier.predict(x_test_pca)

# Calculate the accuracy of the model
accuracy = classifier.score(x_test_pca, y_test)
print("Accuracy:", accuracy)


#make it binary classification problem
X = X[np.logical_or(Y==0,Y==1)]
Y = Y[np.logical_or(Y==0,Y==1)]
model = svm.SVC(kernel='linear')
clf = model.fit(X, Y)
# The equation of the separating plane is given by all x so that np.dot(svc.coef_[0], x) + b = 0.
# Solve for w3 (z)
z = lambda x,y: (-clf.intercept_[0]-clf.coef_[0][0]*x -clf.coef_[0][1]*y) / clf.coef_[0][2]


tmp = np.linspace(-5,5,30)
x,y = np.meshgrid(tmp,tmp)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot3D(X[Y==0,0], X[Y==0,1], X[Y==0,2],'ob')
ax.plot3D(X[Y==1,0], X[Y==1,1], X[Y==1,2],'sr')
ax.plot_surface(x, y, z(x,y))
ax.view_init(30, 60)
plt.show()

The two errors you mention can be solved by the following -

1. NameError: name 'x_train' is not defined

This is because you are using x_train instead of the X_train variable you have defined right above. Remember, variables names are case sensitive.

x_train_pca = pca.fit_transform(x_train)  # your code

x_train_pca = pca.fit_transform(X_train)  # change it to this

2. NameError: name 'svm' is not defined

This is because you are already importing the SVC class using from svm import SVC . But while trying to instantiating the class the model you are using svm.SVC .

model = svm.SVC(kernel='linear') # your code

model = SVC(kernel='linear')     # change it to this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM