如果可能,if语句如何在数组中添加一个数组。
示例。
import numpy as np
import matplotlib.pyplot as plt #to plot visualizations
import pandas as pd
# Importing the dataset
df = pd.read_csv(<Your-dataset-path>)
# Assigning feature and target variables
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
# Use label encoders, if you have any categorical variable
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
X['<column-name>'] = labelencoder.fit_transform(X['<column-name>'])
from sklearn.preprocessing import OneHotEncoder
onehotencoder = OneHotEncoder(categorical_features = ['<index-value>'])
X = onehotencoder.fit_transform(X).toarray()
# Avoiding the dummy variable trap
X = X[:,1:] # Usually done by the algorithm itself
#Spliting the data into test and train set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, random_state = 0, test_size = 0.2)
# Fitting the model
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
# Predicting the test set results
y_pred = regressor.predict(X_test)
我收到未预期的'if'(T_IF)分析错误,期望为')'
答案 0 :(得分:1)
尝试一下。这会起作用。
$a = 1;
$x = 1;
$array = [];
$array[] = array(
'name' => 'name',
'type' => 'type',
'label' => 'label',
);
if( $a == $x ){
$array[] = array(
'name' => 'name',
'type' => 'type',
'label' => 'label',
);
}
$array[] = array(
'name' => 'name',
'type' => 'type',
'label' => 'label',
);
输出:
Array
(
[0] => Array
(
[name] => name
[type] => type
[label] => label
)
[1] => Array
(
[name] => name
[type] => type
[label] => label
)
[2] => Array
(
[name] => name
[type] => type
[label] => label
)
)
答案 1 :(得分:0)
希望得到帮助:
$a = 1;
$x = 1;
$array = array(
array(
'name' => 'name',
'type' => 'type',
'label' => 'label',
),
array(
'name' => 'name',
'type' => 'type',
'label' => 'label',
),
);
if ( $a === $x ) {
$array[] = array(
'name' => 'name',
'type' => 'type',
'label' => 'label',
);
}
print_r($array);