背景:我有1680张图像:7位用户,每位用户提供10倍手语24个英文字母之一的图像。
我正在尝试将图像的规格从字母更改为每个图像的大小为24的数组,在字母的位置包含一个1
。例如,如果字母为A
,则第一个索引为1
,其他23个索引为0
。
我正在尝试避免这种循环:
def one_hot_encoding(images_array, user_array):
letters_to_ones = np.zeros((len(user_array), len(images_array[0]), number_of_classes))
for letter in range(number_of_classes):
letters_to_ones[0:len(user_array)][(10*letter) : (10*letter + 9)][letter] = 1
注意:之前定义的类数,等于24。
我得到一个错误:
索引1超出了尺寸0的轴0的范围
答案 0 :(得分:0)
尝试一下
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: StreamBuilder(
stream: _genres(),
builder:
(BuildContext context, AsyncSnapshot<List<String>> snapshot) {
print("snap: ${snapshot.data}");
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
final title = snapshot.data[index];
return StreamBuilder(
stream: _moviemap(index + 1),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) return Text('No data');
List<Widget> items = [];
for (String name in snapshot.data) {
items.add(ListTile(
title: Text(name),
));
}
return ExpansionTile(
title: Text(title),
children: <Widget>[
Column(
children: items,
),
],
);
},
);
},
);
} else {
return Container();
}
}),
),
);
}
Stream<List<String>> _genres() async* {
yield ['Horror', 'Comedy', 'Drama'];
}
Stream<List<String>> _moviemap(int which) async* {
if (which == 1) {
yield ['Saw', 'Scary Movie'];
} else if (which == 2) {
yield ['Grown ups', 'Grown ups 2', 'Paul Blart'];
} else {
yield ['Green Onions', 'Spring Breakers'];
}
}
}
不要通过执行number_of_classes = 24
n_users = 7
n_images = 1624
letters_to_ones = np.zeros((n_users, n_images, number_of_classes))
for letter in range(number_of_classes):
letters_to_ones[:, (10*letter) : (10*letter + 9), letter] = 1
重新索引数组。而是使用多个索引,如我上面所述:a[1:2][3:4][5:6]
。您的第一个切片索引没有按照您的想象做,实际上,您的第二个切片也在第一个维度上索引。这是我的意思
a[1:2, 3:4, 5:6]
请注意,如果我们添加更多的切片,例如a = np.zeros((4,3,2))
res = a[:4]
print(res.shape)
# => (4, 3, 2)
,第二个切片仍会索引第一个维度。